| @@ 7-39 (lines=33) @@ | ||
| 4 | ||
| 5 | namespace Linio\Component\Input\Constraint; |
|
| 6 | ||
| 7 | class Range extends Constraint |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var int |
|
| 11 | */ |
|
| 12 | protected $min; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var int |
|
| 16 | */ |
|
| 17 | protected $max; |
|
| 18 | ||
| 19 | public function __construct(int $min, int $max = PHP_INT_MAX, string $errorMessage = null) |
|
| 20 | { |
|
| 21 | $this->min = $min; |
|
| 22 | $this->max = $max; |
|
| 23 | ||
| 24 | $this->setErrorMessage($errorMessage ?? sprintf('Value is not between %d and %d', $this->min, $this->max)); |
|
| 25 | } |
|
| 26 | ||
| 27 | public function validate($content): bool |
|
| 28 | { |
|
| 29 | if (!is_scalar($content)) { |
|
| 30 | return false; |
|
| 31 | } |
|
| 32 | ||
| 33 | if ($content === null) { |
|
| 34 | return false; |
|
| 35 | } |
|
| 36 | ||
| 37 | return $content >= $this->min && $content <= $this->max; |
|
| 38 | } |
|
| 39 | } |
|
| 40 | ||
| @@ 7-43 (lines=37) @@ | ||
| 4 | ||
| 5 | namespace Linio\Component\Input\Constraint; |
|
| 6 | ||
| 7 | class StringSize extends Constraint |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var int |
|
| 11 | */ |
|
| 12 | protected $minSize; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var int |
|
| 16 | */ |
|
| 17 | protected $maxSize; |
|
| 18 | ||
| 19 | public function __construct(int $minSize, int $maxSize = PHP_INT_MAX, string $errorMessage = null) |
|
| 20 | { |
|
| 21 | $this->minSize = $minSize; |
|
| 22 | $this->maxSize = $maxSize; |
|
| 23 | ||
| 24 | $this->setErrorMessage( |
|
| 25 | $errorMessage ?? sprintf('Content out of min/max limit sizes [%s, %s]', $this->minSize, $this->maxSize) |
|
| 26 | ); |
|
| 27 | } |
|
| 28 | ||
| 29 | public function validate($content): bool |
|
| 30 | { |
|
| 31 | if (!is_scalar($content)) { |
|
| 32 | return false; |
|
| 33 | } |
|
| 34 | ||
| 35 | if ($content === null) { |
|
| 36 | return false; |
|
| 37 | } |
|
| 38 | ||
| 39 | $size = strlen($content); |
|
| 40 | ||
| 41 | return $size >= $this->minSize && $size <= $this->maxSize; |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||