Normalized some filtering

This commit is contained in:
Alejandro Celaya 2021-01-30 18:24:14 +01:00
parent 07b12fac3c
commit 903ef8e249
4 changed files with 28 additions and 9 deletions

View File

@ -40,6 +40,10 @@ class ShortUrl extends AbstractEntity
private ?string $importOriginalShortCode = null; private ?string $importOriginalShortCode = null;
private ?ApiKey $authorApiKey = null; private ?ApiKey $authorApiKey = null;
private function __construct()
{
}
public static function createEmpty(): self public static function createEmpty(): self
{ {
return self::fromMeta(ShortUrlMeta::createEmpty()); return self::fromMeta(ShortUrlMeta::createEmpty());
@ -219,9 +223,10 @@ class ShortUrl extends AbstractEntity
public function toString(array $domainConfig): string public function toString(array $domainConfig): string
{ {
return (string) (new Uri())->withPath($this->shortCode) return (new Uri())->withPath($this->shortCode)
->withScheme($domainConfig['schema'] ?? 'http') ->withScheme($domainConfig['schema'] ?? 'http')
->withHost($this->resolveDomain($domainConfig['hostname'] ?? '')); ->withHost($this->resolveDomain($domainConfig['hostname'] ?? ''))
->__toString();
} }
private function resolveDomain(string $fallback = ''): string private function resolveDomain(string $fallback = ''): string

View File

@ -25,7 +25,6 @@ final class ShortUrlEdit
private ?int $maxVisits = null; private ?int $maxVisits = null;
private ?bool $validateUrl = null; private ?bool $validateUrl = null;
// Enforce named constructors
private function __construct() private function __construct()
{ {
} }

View File

@ -27,18 +27,18 @@ final class ShortUrlMeta
private int $shortCodeLength = 5; private int $shortCodeLength = 5;
private ?bool $validateUrl = null; private ?bool $validateUrl = null;
private ?ApiKey $apiKey = null; private ?ApiKey $apiKey = null;
private array $tags = [];
// Enforce named constructors
private function __construct() private function __construct()
{ {
} }
public static function createEmpty(): self public static function createEmpty(): self
{ {
$meta = new self(); $instance = new self();
$meta->longUrl = ''; $instance->longUrl = '';
return $meta; return $instance;
} }
/** /**
@ -48,6 +48,7 @@ final class ShortUrlMeta
{ {
$instance = new self(); $instance = new self();
$instance->validateAndInit($data); $instance->validateAndInit($data);
return $instance; return $instance;
} }
@ -74,6 +75,7 @@ final class ShortUrlMeta
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH, ShortUrlMetaInputFilter::SHORT_CODE_LENGTH,
) ?? DEFAULT_SHORT_CODES_LENGTH; ) ?? DEFAULT_SHORT_CODES_LENGTH;
$this->apiKey = $inputFilter->getValue(ShortUrlMetaInputFilter::API_KEY); $this->apiKey = $inputFilter->getValue(ShortUrlMetaInputFilter::API_KEY);
$this->tags = $inputFilter->getValue(ShortUrlMetaInputFilter::TAGS);
} }
public function getLongUrl(): string public function getLongUrl(): string
@ -150,4 +152,12 @@ final class ShortUrlMeta
{ {
return $this->apiKey; return $this->apiKey;
} }
/**
* @return string[]
*/
public function getTags(): array
{
return $this->tags;
}
} }

View File

@ -13,6 +13,8 @@ use Shlinkio\Shlink\Common\Validation;
use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge; use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
use function is_numeric;
use const Shlinkio\Shlink\Core\CUSTOM_SLUGS_REGEXP; use const Shlinkio\Shlink\Core\CUSTOM_SLUGS_REGEXP;
use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH; use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH;
@ -30,6 +32,7 @@ class ShortUrlMetaInputFilter extends InputFilter
public const LONG_URL = 'longUrl'; public const LONG_URL = 'longUrl';
public const VALIDATE_URL = 'validateUrl'; public const VALIDATE_URL = 'validateUrl';
public const API_KEY = 'apiKey'; public const API_KEY = 'apiKey';
public const TAGS = 'tags';
private bool $requireLongUrl; private bool $requireLongUrl;
@ -90,12 +93,14 @@ class ShortUrlMetaInputFilter extends InputFilter
->setRequired(false) ->setRequired(false)
->getValidatorChain()->attach(new Validator\IsInstanceOf(['className' => ApiKey::class])); ->getValidatorChain()->attach(new Validator\IsInstanceOf(['className' => ApiKey::class]));
$this->add($apiKeyInput); $this->add($apiKeyInput);
$this->add($this->createArrayInput(self::TAGS, false));
} }
private function createPositiveNumberInput(string $name, int $min = 1): Input private function createPositiveNumberInput(string $name, int $min = 1): Input
{ {
$input = $this->createInput($name, false); $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])); ->attach(new Validator\GreaterThan(['min' => $min, 'inclusive' => true]));
return $input; return $input;