mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Created new findIfExists meta param
This commit is contained in:
parent
49668547d7
commit
594e7da256
4 changed files with 58 additions and 29 deletions
35
module/Common/src/Validation/InputFactoryTrait.php
Normal file
35
module/Common/src/Validation/InputFactoryTrait.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Validation;
|
||||
|
||||
use Zend\Filter;
|
||||
use Zend\InputFilter\Input;
|
||||
use Zend\Validator;
|
||||
|
||||
trait InputFactoryTrait
|
||||
{
|
||||
private function createInput($name, $required = true): Input
|
||||
{
|
||||
$input = new Input($name);
|
||||
$input->setRequired($required)
|
||||
->getFilterChain()->attach(new Filter\StripTags())
|
||||
->attach(new Filter\StringTrim());
|
||||
return $input;
|
||||
}
|
||||
|
||||
private function createBooleanInput(string $name, bool $required = true): Input
|
||||
{
|
||||
$input = $this->createInput($name, $required);
|
||||
$input->getFilterChain()->attach(new Filter\Boolean());
|
||||
$input->getValidatorChain()->attach(new Validator\NotEmpty(['type' => [
|
||||
Validator\NotEmpty::OBJECT,
|
||||
Validator\NotEmpty::SPACE,
|
||||
Validator\NotEmpty::NULL,
|
||||
Validator\NotEmpty::EMPTY_ARRAY,
|
||||
Validator\NotEmpty::STRING,
|
||||
]]));
|
||||
|
||||
return $input;
|
||||
}
|
||||
}
|
|
@ -18,6 +18,8 @@ final class ShortUrlMeta
|
|||
private $customSlug;
|
||||
/** @var int|null */
|
||||
private $maxVisits;
|
||||
/** @var bool */
|
||||
private $findIfExists;
|
||||
|
||||
// Force named constructors
|
||||
private function __construct()
|
||||
|
@ -45,21 +47,25 @@ final class ShortUrlMeta
|
|||
* @param string|Chronos|null $validUntil
|
||||
* @param string|null $customSlug
|
||||
* @param int|null $maxVisits
|
||||
* @param bool|null $findIfExists
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public static function createFromParams(
|
||||
$validSince = null,
|
||||
$validUntil = null,
|
||||
$customSlug = null,
|
||||
$maxVisits = null
|
||||
$maxVisits = null,
|
||||
$findIfExists = null
|
||||
): self {
|
||||
// We do not type hint the arguments because that will be done by the validation process
|
||||
// We do not type hint the arguments because that will be done by the validation process and we would get a
|
||||
// type error if any of them do not match
|
||||
$instance = new self();
|
||||
$instance->validate([
|
||||
ShortUrlMetaInputFilter::VALID_SINCE => $validSince,
|
||||
ShortUrlMetaInputFilter::VALID_UNTIL => $validUntil,
|
||||
ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug,
|
||||
ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits,
|
||||
ShortUrlMetaInputFilter::FIND_IF_EXISTS => $findIfExists,
|
||||
]);
|
||||
return $instance;
|
||||
}
|
||||
|
@ -80,6 +86,7 @@ final class ShortUrlMeta
|
|||
$this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG);
|
||||
$this->maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS);
|
||||
$this->maxVisits = $this->maxVisits !== null ? (int) $this->maxVisits : null;
|
||||
$this->findIfExists = (bool) $inputFilter->getValue(ShortUrlMetaInputFilter::FIND_IF_EXISTS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,6 +146,11 @@ final class ShortUrlMeta
|
|||
return $this->maxVisits !== null;
|
||||
}
|
||||
|
||||
public function findIfExists(): bool
|
||||
{
|
||||
return $this->findIfExists;
|
||||
}
|
||||
|
||||
public function withCustomSlug(string $customSlug): self
|
||||
{
|
||||
$clone = clone $this;
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Validation;
|
||||
|
||||
use Zend\Filter\StringTrim;
|
||||
use Zend\Filter\StripTags;
|
||||
use Zend\InputFilter\Input;
|
||||
|
||||
trait InputFactoryTrait
|
||||
{
|
||||
private function createInput($name, $required = true): Input
|
||||
{
|
||||
$input = new Input($name);
|
||||
$input->setRequired($required)
|
||||
->getFilterChain()->attach(new StripTags())
|
||||
->attach(new StringTrim());
|
||||
return $input;
|
||||
}
|
||||
}
|
|
@ -4,10 +4,9 @@ declare(strict_types=1);
|
|||
namespace Shlinkio\Shlink\Core\Validation;
|
||||
|
||||
use DateTime;
|
||||
use Zend\I18n\Validator\IsInt;
|
||||
use Shlinkio\Shlink\Common\Validation\InputFactoryTrait;
|
||||
use Zend\InputFilter\InputFilter;
|
||||
use Zend\Validator\Date;
|
||||
use Zend\Validator\GreaterThan;
|
||||
use Zend\Validator;
|
||||
|
||||
class ShortUrlMetaInputFilter extends InputFilter
|
||||
{
|
||||
|
@ -17,6 +16,7 @@ class ShortUrlMetaInputFilter extends InputFilter
|
|||
public const VALID_UNTIL = 'validUntil';
|
||||
public const CUSTOM_SLUG = 'customSlug';
|
||||
public const MAX_VISITS = 'maxVisits';
|
||||
public const FIND_IF_EXISTS = 'findIfExists';
|
||||
|
||||
public function __construct(?array $data = null)
|
||||
{
|
||||
|
@ -29,18 +29,20 @@ class ShortUrlMetaInputFilter extends InputFilter
|
|||
private function initialize(): void
|
||||
{
|
||||
$validSince = $this->createInput(self::VALID_SINCE, false);
|
||||
$validSince->getValidatorChain()->attach(new Date(['format' => DateTime::ATOM]));
|
||||
$validSince->getValidatorChain()->attach(new Validator\Date(['format' => DateTime::ATOM]));
|
||||
$this->add($validSince);
|
||||
|
||||
$validUntil = $this->createInput(self::VALID_UNTIL, false);
|
||||
$validUntil->getValidatorChain()->attach(new Date(['format' => DateTime::ATOM]));
|
||||
$validUntil->getValidatorChain()->attach(new Validator\Date(['format' => DateTime::ATOM]));
|
||||
$this->add($validUntil);
|
||||
|
||||
$this->add($this->createInput(self::CUSTOM_SLUG, false));
|
||||
|
||||
$maxVisits = $this->createInput(self::MAX_VISITS, false);
|
||||
$maxVisits->getValidatorChain()->attach(new IsInt())
|
||||
->attach(new GreaterThan(['min' => 1, 'inclusive' => true]));
|
||||
$maxVisits->getValidatorChain()->attach(new Validator\Digits())
|
||||
->attach(new Validator\GreaterThan(['min' => 1, 'inclusive' => true]));
|
||||
$this->add($maxVisits);
|
||||
|
||||
$this->add($this->createBooleanInput(self::FIND_IF_EXISTS, false));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue