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 ?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

View file

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

View file

@ -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;
}
}

View file

@ -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;