respect-validation/library/Rules/Length.php
Henrique Moody dd896bb12d
Move template definitions to the rules
It's easier to identify the reason for choosing a specific message in
the rule than in the exception. The same goes for the key we use to
determine the templates.

This change will simplify the `ValidationException` because it will
already receive the template it needs to use. As a consequence, the
`Factory` also becomes more straightforward.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:17:27 +01:00

124 lines
3 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Countable as CountableInterface;
use Respect\Validation\Exceptions\ComponentException;
use function count;
use function get_object_vars;
use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function mb_strlen;
use function sprintf;
final class Length extends AbstractRule
{
public const TEMPLATE_LOWER = 'lower';
public const TEMPLATE_GREATER = 'greater';
public const TEMPLATE_GREATER_INCLUSIVE = 'greater_inclusive';
public const TEMPLATE_EXACT = 'exact';
public const TEMPLATE_LOWER_INCLUSIVE = 'lower_inclusive';
public const TEMPLATE_BOTH = 'both';
/**
* @throws ComponentException
*/
public function __construct(
private readonly ?int $minValue = null,
private readonly ?int $maxValue = null,
private readonly bool $inclusive = true
) {
if ($maxValue !== null && $minValue > $maxValue) {
throw new ComponentException(sprintf('%d cannot be less than %d for validation', $minValue, $maxValue));
}
}
public function validate(mixed $input): bool
{
$length = $this->extractLength($input);
if ($length === null) {
return false;
}
return $this->validateMin($length) && $this->validateMax($length);
}
public function getTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if (!$this->minValue) {
return $this->inclusive === true ? self::TEMPLATE_GREATER_INCLUSIVE : self::TEMPLATE_GREATER;
}
if (!$this->maxValue) {
return $this->inclusive === true ? self::TEMPLATE_LOWER_INCLUSIVE : self::TEMPLATE_LOWER;
}
if ($this->minValue == $this->maxValue) {
return self::TEMPLATE_EXACT;
}
return self::TEMPLATE_BOTH;
}
private function extractLength(mixed $input): ?int
{
if (is_string($input)) {
return (int) mb_strlen($input);
}
if (is_array($input) || $input instanceof CountableInterface) {
return count($input);
}
if (is_object($input)) {
return $this->extractLength(get_object_vars($input));
}
if (is_int($input)) {
return $this->extractLength((string) $input);
}
return null;
}
private function validateMin(int $length): bool
{
if ($this->minValue === null) {
return true;
}
if ($this->inclusive) {
return $length >= $this->minValue;
}
return $length > $this->minValue;
}
private function validateMax(int $length): bool
{
if ($this->maxValue === null) {
return true;
}
if ($this->inclusive) {
return $length <= $this->maxValue;
}
return $length < $this->maxValue;
}
}