From 903ef8e249010d1fd12fe27973f4312f5238d6ee Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Jan 2021 18:24:14 +0100 Subject: [PATCH] Normalized some filtering --- module/Core/src/Entity/ShortUrl.php | 11 ++++++++--- module/Core/src/Model/ShortUrlEdit.php | 1 - module/Core/src/Model/ShortUrlMeta.php | 18 ++++++++++++++---- .../src/Validation/ShortUrlMetaInputFilter.php | 7 ++++++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 3c076585..76df360e 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -40,6 +40,10 @@ class ShortUrl extends AbstractEntity private ?string $importOriginalShortCode = null; private ?ApiKey $authorApiKey = null; + private function __construct() + { + } + public static function createEmpty(): self { return self::fromMeta(ShortUrlMeta::createEmpty()); @@ -219,9 +223,10 @@ class ShortUrl extends AbstractEntity public function toString(array $domainConfig): string { - return (string) (new Uri())->withPath($this->shortCode) - ->withScheme($domainConfig['schema'] ?? 'http') - ->withHost($this->resolveDomain($domainConfig['hostname'] ?? '')); + return (new Uri())->withPath($this->shortCode) + ->withScheme($domainConfig['schema'] ?? 'http') + ->withHost($this->resolveDomain($domainConfig['hostname'] ?? '')) + ->__toString(); } private function resolveDomain(string $fallback = ''): string diff --git a/module/Core/src/Model/ShortUrlEdit.php b/module/Core/src/Model/ShortUrlEdit.php index 67300682..bb2aab33 100644 --- a/module/Core/src/Model/ShortUrlEdit.php +++ b/module/Core/src/Model/ShortUrlEdit.php @@ -25,7 +25,6 @@ final class ShortUrlEdit private ?int $maxVisits = null; private ?bool $validateUrl = null; - // Enforce named constructors private function __construct() { } diff --git a/module/Core/src/Model/ShortUrlMeta.php b/module/Core/src/Model/ShortUrlMeta.php index 9291a5e6..1286043b 100644 --- a/module/Core/src/Model/ShortUrlMeta.php +++ b/module/Core/src/Model/ShortUrlMeta.php @@ -27,18 +27,18 @@ final class ShortUrlMeta private int $shortCodeLength = 5; private ?bool $validateUrl = null; private ?ApiKey $apiKey = null; + private array $tags = []; - // Enforce named constructors private function __construct() { } public static function createEmpty(): self { - $meta = new self(); - $meta->longUrl = ''; + $instance = new self(); + $instance->longUrl = ''; - return $meta; + return $instance; } /** @@ -48,6 +48,7 @@ final class ShortUrlMeta { $instance = new self(); $instance->validateAndInit($data); + return $instance; } @@ -74,6 +75,7 @@ final class ShortUrlMeta ShortUrlMetaInputFilter::SHORT_CODE_LENGTH, ) ?? DEFAULT_SHORT_CODES_LENGTH; $this->apiKey = $inputFilter->getValue(ShortUrlMetaInputFilter::API_KEY); + $this->tags = $inputFilter->getValue(ShortUrlMetaInputFilter::TAGS); } public function getLongUrl(): string @@ -150,4 +152,12 @@ final class ShortUrlMeta { return $this->apiKey; } + + /** + * @return string[] + */ + public function getTags(): array + { + return $this->tags; + } } diff --git a/module/Core/src/Validation/ShortUrlMetaInputFilter.php b/module/Core/src/Validation/ShortUrlMetaInputFilter.php index ba095c03..1131110d 100644 --- a/module/Core/src/Validation/ShortUrlMetaInputFilter.php +++ b/module/Core/src/Validation/ShortUrlMetaInputFilter.php @@ -13,6 +13,8 @@ use Shlinkio\Shlink\Common\Validation; use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge; use Shlinkio\Shlink\Rest\Entity\ApiKey; +use function is_numeric; + use const Shlinkio\Shlink\Core\CUSTOM_SLUGS_REGEXP; use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH; @@ -30,6 +32,7 @@ class ShortUrlMetaInputFilter extends InputFilter public const LONG_URL = 'longUrl'; public const VALIDATE_URL = 'validateUrl'; public const API_KEY = 'apiKey'; + public const TAGS = 'tags'; private bool $requireLongUrl; @@ -90,12 +93,14 @@ class ShortUrlMetaInputFilter extends InputFilter ->setRequired(false) ->getValidatorChain()->attach(new Validator\IsInstanceOf(['className' => ApiKey::class])); $this->add($apiKeyInput); + + $this->add($this->createArrayInput(self::TAGS, false)); } private function createPositiveNumberInput(string $name, int $min = 1): Input { $input = $this->createInput($name, false); - $input->getValidatorChain()->attach(new Validator\Digits()) + $input->getValidatorChain()->attach(new Validator\Callback(fn ($value) => is_numeric($value))) ->attach(new Validator\GreaterThan(['min' => $min, 'inclusive' => true])); return $input;