diff --git a/docs/guide/en/result.md b/docs/guide/en/result.md index 6f04be8e..bac5a77f 100644 --- a/docs/guide/en/result.md +++ b/docs/guide/en/result.md @@ -287,4 +287,4 @@ $result->getAttributeErrors('email'); ``` [Using keys containing separator / shortcut]: built-in-rules-nested.md#using-keys-containing-separator--shortcut -[will cast keys to the int type]: https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties +[will cast keys to the int type]: https://www.php.net/manual/en/language.types.array.php diff --git a/src/Exception/UnexpectedRuleException.php b/src/Exception/UnexpectedRuleException.php index eb36ee71..c7360e86 100644 --- a/src/Exception/UnexpectedRuleException.php +++ b/src/Exception/UnexpectedRuleException.php @@ -15,6 +15,7 @@ * use Yiisoft\Validator\Exception\UnexpectedRuleException; * use Yiisoft\Validator\Result; * use Yiisoft\Validator\RuleHandlerInterface; + * use Yiisoft\Validator\RuleInterface; * use Yiisoft\Validator\ValidationContext; * * final class MyRuleHandler implements RuleHandlerInterface diff --git a/src/Rule/DateTime.php b/src/Rule/DateTime.php deleted file mode 100644 index 56d85d4c..00000000 --- a/src/Rule/DateTime.php +++ /dev/null @@ -1,160 +0,0 @@ - [ - * new DateTime(format: 'Y-m-d'), - * ], - * ]; - * ``` - * In the example above, the PHP attributes equivalent will be: - * - * ```php - * use Yiisoft\Validator\Validator; - * use Yiisoft\Validator\Rule\DateTime; - * - * final class User - * { - * public function __construct( - * #[DateTime(format: 'Y-m-d')] - * public string $date, - * ){ - * } - * } - * - * $user = new User( date: '2022-01-01' ); - * - * $validator = (new Validator())->validate($user); - * - * ``` - * - * @see DateTimeHandler - * - * @psalm-import-type SkipOnEmptyValue from SkipOnEmptyInterface - * @psalm-import-type WhenType from WhenInterface - */ -#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class DateTime implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface -{ - use SkipOnEmptyTrait; - use SkipOnErrorTrait; - use WhenTrait; - - /** - * @link https://www.php.net/manual/en/datetimeimmutable.createfromformat.php - * @psalm-var non-empty-string - * @var string The allowed date formats. - */ - private string $format; - - /** - * @param string $format The format of the date. See {@see $format} - * @param string $message A message used when the value is not valid. - * You may use the following placeholders in the message: - * - `{attribute}`: the translated label of the attribute being validated. - * - `{value}`: the value of the attribute being validated. - * @param string $incorrectInputMessage A message used when the input is incorrect. - * You may use the following placeholders in the message: - * - `{attribute}`: the translated label of the attribute being validated. - * - `{type}`: the type of the value being validated. - * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. See {@see SkipOnEmptyInterface}. - * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See {@see SkipOnErrorInterface}. - * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}. - * - * @psalm-param SkipOnEmptyValue $skipOnEmpty - * @psalm-param WhenType $when - */ - public function __construct( - string $format = 'Y-m-d', - private string $incorrectInputMessage = '{Attribute} must be a date.', - private string $message = '{Attribute} is not a valid date.', - bool|callable|null $skipOnEmpty = null, - private bool $skipOnError = false, - private ?Closure $when = null, - ) { - if ($format === '') { - throw new InvalidArgumentException('Format can\'t be empty.'); - } - - $this->format = $format; - $this->skipOnEmpty = $skipOnEmpty; - } - - /** - * The date format. - * - * @return string The format. See {@see $format} - * @psalm-return non-empty-string - * - * @see $format - */ - public function getFormat(): string - { - return $this->format; - } - - /** - * Get a message used when the input is incorrect. - * - * @return string A message used when the input is incorrect. - * @see $incorrectInputMessage - */ - public function getIncorrectInputMessage(): string - { - return $this->incorrectInputMessage; - } - - public function getOptions(): array - { - return [ - 'format' => $this->format, - 'incorrectInputMessage' => [ - 'template' => $this->incorrectInputMessage, - 'parameters' => [], - ], - 'message' => [ - 'template' => $this->message, - 'parameters' => [], - ], - 'skipOnEmpty' => $this->getSkipOnEmptyOption(), - 'skipOnError' => $this->skipOnError, - ]; - } - - public function getMessage(): string - { - return $this->message; - } - - public function getName(): string - { - return self::class; - } - - public function getHandler(): string|RuleHandlerInterface - { - return DateTimeHandler::class; - } -} diff --git a/src/Rule/DateTimeHandler.php b/src/Rule/DateTimeHandler.php deleted file mode 100644 index 49f05cf2..00000000 --- a/src/Rule/DateTimeHandler.php +++ /dev/null @@ -1,57 +0,0 @@ -addError($rule->getIncorrectInputMessage(), [ - 'attribute' => $context->getTranslatedAttribute(), - 'Attribute' => $context->getCapitalizedTranslatedAttribute(), - 'type' => get_debug_type($value), - ]); - } - \DateTime::createFromFormat($rule->getFormat(), (string)$value); - - /** - * @psalm-var array{error_count:non-negative-int,warning_count:non-negative-int}|false $errors - */ - $errors = \DateTime::getLastErrors(); - - // Before PHP 8.2 may return array instead of false (see https://github.com/php/php-src/issues/9431). - $hasError = is_array($errors) - ? $errors['error_count'] !== 0 || $errors['warning_count'] !== 0 - : $errors; - - if ($hasError) { - $result->addError($rule->getMessage(), [ - 'attribute' => $context->getTranslatedAttribute(), - 'Attribute' => $context->getCapitalizedTranslatedAttribute(), - 'value' => $value, - ]); - } - - return $result; - } -} diff --git a/src/Rule/NestedHandler.php b/src/Rule/NestedHandler.php index 4dbb3396..5270428b 100644 --- a/src/Rule/NestedHandler.php +++ b/src/Rule/NestedHandler.php @@ -13,6 +13,7 @@ use Yiisoft\Validator\RuleInterface; use Yiisoft\Validator\ValidationContext; +use function array_slice; use function is_array; use function is_int; use function is_object; diff --git a/tests/Rule/AtLeastTest.php b/tests/Rule/AtLeastTest.php index 68f82b55..66f78f1f 100644 --- a/tests/Rule/AtLeastTest.php +++ b/tests/Rule/AtLeastTest.php @@ -275,7 +275,7 @@ public function dataValidationFailed(): array ['data' => new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attributes - {attributes}, min - {min}.')], ['data' => ['Attributes - "attr1", "attr2", min - 2.']], ], - 'class attribute, tranlation' => [ + 'class attribute, translation' => [ new AtLeastDto(), null, ['' => ['At least 1 attribute from this list must be filled: "A", "B", "C".']],