Used input factory methods from shlink-common when possible

This commit is contained in:
Alejandro Celaya 2021-01-30 18:55:25 +01:00
parent 903ef8e249
commit 3f2bd657e1
3 changed files with 7 additions and 36 deletions

View File

@ -47,7 +47,7 @@
"predis/predis": "^1.1",
"pugx/shortid-php": "^0.7",
"ramsey/uuid": "^3.9",
"shlinkio/shlink-common": "dev-main#cab9f39 as 3.5",
"shlinkio/shlink-common": "dev-main#b889f5d as 3.5",
"shlinkio/shlink-config": "^1.0",
"shlinkio/shlink-event-dispatcher": "^2.0",
"shlinkio/shlink-importer": "^2.1",

View File

@ -13,8 +13,6 @@ 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;
@ -77,8 +75,8 @@ class ShortUrlMetaInputFilter extends InputFilter
]));
$this->add($customSlug);
$this->add($this->createPositiveNumberInput(self::MAX_VISITS));
$this->add($this->createPositiveNumberInput(self::SHORT_CODE_LENGTH, MIN_SHORT_CODES_LENGTH));
$this->add($this->createNumericInput(self::MAX_VISITS, false));
$this->add($this->createNumericInput(self::SHORT_CODE_LENGTH, false, MIN_SHORT_CODES_LENGTH));
$this->add($this->createBooleanInput(self::FIND_IF_EXISTS, false));
@ -94,15 +92,6 @@ class ShortUrlMetaInputFilter extends InputFilter
->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\Callback(fn ($value) => is_numeric($value)))
->attach(new Validator\GreaterThan(['min' => $min, 'inclusive' => true]));
return $input;
$this->add($this->createTagsInput(self::TAGS, false));
}
}

View File

@ -4,14 +4,9 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Validation;
use Laminas\Filter;
use Laminas\InputFilter\Input;
use Laminas\InputFilter\InputFilter;
use Laminas\Validator;
use Shlinkio\Shlink\Common\Validation;
use function is_numeric;
class ShortUrlsParamsInputFilter extends InputFilter
{
use Validation\InputFactoryTrait;
@ -36,22 +31,9 @@ class ShortUrlsParamsInputFilter extends InputFilter
$this->add($this->createInput(self::SEARCH_TERM, false));
$this->add($this->createNumericInput(self::PAGE, 1));
$this->add($this->createNumericInput(self::PAGE, false));
$this->add($this->createNumericInput(self::ITEMS_PER_PAGE, false, -1));
$tags = $this->createArrayInput(self::TAGS, false);
$tags->getFilterChain()->attach(new Filter\StringToLower())
->attach(new Filter\PregReplace(['pattern' => '/ /', 'replacement' => '-']));
$this->add($tags);
$this->add($this->createNumericInput(self::ITEMS_PER_PAGE, -1));
}
private function createNumericInput(string $name, int $min): Input
{
$input = $this->createInput($name, false);
$input->getValidatorChain()->attach(new Validator\Callback(fn ($value) => is_numeric($value)))
->attach(new Validator\GreaterThan(['min' => $min, 'inclusive' => true]));
return $input;
$this->add($this->createTagsInput(self::TAGS, false));
}
}