Skip to content

Commit

Permalink
Merge fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed May 22, 2024
1 parent ff32860 commit 92c51a9
Show file tree
Hide file tree
Showing 27 changed files with 57 additions and 87 deletions.
10 changes: 0 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@
- Chg #613: Change type of `$escape` argument in `Error::getValuePath()` from `bool|string|null` to `string|null`
(@arogachev)

## 1.3.0 April 04, 2024

- New #665: Add methods `addErrorWithFormatOnly()` and `addErrorWithoutPostProcessing()` to `Result` object (@vjik)
- New #670, #677, #680: Add `Image` validation rule (@vjik, @arogachev)
- New #678: Add `Date`, `DateTime` and `Time` validation rules (@vjik)
- Enh #668: Clarify psalm types in `Result` (@vjik)
## 1.4.1 under development

- no changes in this release.

## 1.4.0 May 22, 2024

- New #649: Add `getFirstErrorMessagesIndexedByPath()` and `getFirstErrorMessagesIndexedByAttribute()` methods to
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"maglnet/composer-require-checker": "^4.3",
"phpbench/phpbench": "^1.2",
"phpunit/phpunit": "^9.5",
"rector/rector": "^1.0",
"rector/rector": "1.0.*",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.22",
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/en/creating-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ final class YamlHandler implements RuleHandlerInterface
> **Note:** Using [yaml_parse] additionally requires `yaml` PHP extension.
> **Note:** Processing untrusted user input with `yaml_parse()` can be dangerous with certain settings. Please refer to
> [yaml_parse] docs for more details.
> [`yaml_parse()` docs](https://www.php.net/manual/en/function.yaml-parse.php) for more details.
### Wrapping validation

Expand Down
5 changes: 4 additions & 1 deletion messages/ru/yii-validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
'{Attribute} must be an IP address with specified subnet.' => '{Attribute} должен быть IP адресом с подсетью.',
'{Attribute} must not be a subnet.' => '{Attribute} не должно быть подсетью.',
'{Attribute} is not in the allowed range.' => '{Attribute} не входит в список разрешенных диапазонов адресов.',
/** @see Integer */
/**
* @see IntegerType
* @see Integer
*/
'{Attribute} must be an integer.' => '{Attribute} должно быть целым числом.',
/** @see Json */
'{Attribute} is not JSON.' => '{Attribute} не является строкой JSON.',
Expand Down
2 changes: 0 additions & 2 deletions src/Rule/AbstractCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ abstract class AbstractCompare implements
];

/**
* @param mixed $targetValue The value to be compared with. When both this property and {@see $targetAttribute} are
* set, this property takes precedence.
* @param string|null $targetAttribute The name of the attribute to be compared with. When both this property and
* {@see $targetValue} are set, the {@see $targetValue} takes precedence.
* @param string $incorrectInputMessage A message used when the input is incorrect.
Expand Down
9 changes: 5 additions & 4 deletions src/Rule/AnyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use Closure;
use JetBrains\PhpStorm\ArrayShape;
use Yiisoft\Validator\AfterInitAttributeEventInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\Helper\RulesNormalizer;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\Helper\RulesDumper;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\ValidatorInterface;
Expand Down Expand Up @@ -44,7 +44,7 @@
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class AnyRule implements
RuleWithOptionsInterface,
DumpedRuleInterface,
SkipOnEmptyInterface,
SkipOnErrorInterface,
WhenInterface,
Expand Down Expand Up @@ -83,16 +83,17 @@ final class AnyRule implements
public function __construct(
iterable $rules,
private string $message = 'At least one of the inner rules must pass the validation.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->rules = RulesNormalizer::normalizeList($rules);
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
{
return 'any';
return self::class;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/AnyRuleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

/**
Expand All @@ -15,7 +16,7 @@
*/
final class AnyRuleHandler implements RuleHandlerInterface
{
public function validate(mixed $value, object $rule, ValidationContext $context): Result
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof AnyRule) {
throw new UnexpectedRuleException(AnyRule::class, $rule);
Expand Down
19 changes: 0 additions & 19 deletions src/Rule/CompareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c
*
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
*
* @param mixed $value The validated value.
*
* @return bool `true` if value is correct and `false` otherwise.
*/
private function isInputCorrect(string $type, mixed $value): bool
Expand All @@ -97,8 +95,6 @@ private function isInputCorrect(string $type, mixed $value): bool
* Checks whether the validated value is allowed for types that require type casting - {@see CompareType::NUMBER}
* and {@see CompareType::STRING}.
*
* @param mixed $value The Validated value.
*
* @return bool `true` if value is allowed and `false` otherwise.
*/
private function isValueAllowedForTypeCasting(mixed $value): bool
Expand All @@ -112,8 +108,6 @@ private function isValueAllowedForTypeCasting(mixed $value): bool
/**
* Gets representation of the value for using with error parameter.
*
* @param mixed $value The validated value.
*
* @return scalar|null Formatted value.
*/
private function getFormattedValue(mixed $value): int|float|string|bool|null
Expand Down Expand Up @@ -141,9 +135,6 @@ private function getFormattedValue(mixed $value): int|float|string|bool|null
*
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
*
* @param mixed $value The validated value.
* @param mixed $targetValue "Target" value set in rule options.
*
* @return bool Whether the result of comparison using the specified operator is true.
*/
private function compareValues(string $type, string $operator, mixed $value, mixed $targetValue): bool
Expand Down Expand Up @@ -179,8 +170,6 @@ private function compareValues(string $type, string $operator, mixed $value, mix
*
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
*
* @param mixed $value The validated value.
* @param mixed $targetValue "Target" value set in rule options.
* @param bool $strict Whether the values must be equal (when set to `false`, default) / strictly equal (when set to
* `true`).
*
Expand Down Expand Up @@ -228,8 +217,6 @@ private function checkFloatsAreEqual(float $value, float $targetValue): bool
*
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
*
* @param mixed $value One of the compared values. Both validated and target value can be used.
*
* @return mixed Normalized value ready for comparison.
*/
private function normalizeValue(string $type, mixed $value): mixed
Expand All @@ -244,9 +231,6 @@ private function normalizeValue(string $type, mixed $value): mixed
/**
* Normalizes number that might be stored in a different type to float number.
*
* @param mixed $number Raw number. Can be within an object implementing {@see Stringable} /
* {@see DateTimeInterface} or other primitive type, such as `int`, `float`, `string`.
*
* @return float Float number ready for comparison.
*/
private function normalizeNumber(mixed $number): float
Expand All @@ -263,9 +247,6 @@ private function normalizeNumber(mixed $number): float
/**
* Normalizes string that might be stored in a different type to simple string.
*
* @param mixed $string Raw string. Can be within an object implementing {@see DateTimeInterface} or other primitive
* type, such as `int`, `float`, `string`.
*
* @return string String ready for comparison.
*/
private function normalizeString(mixed $string): string
Expand Down
9 changes: 5 additions & 4 deletions src/Rule/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Attribute;
use Closure;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -26,7 +26,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class BooleanType implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
final class BooleanType implements DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand All @@ -51,15 +51,16 @@ final class BooleanType implements RuleWithOptionsInterface, SkipOnEmptyInterfac
*/
public function __construct(
private string $message = 'Value must be a boolean.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
{
return 'booleanType';
return self::class;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Type/BooleanTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

use function is_bool;
Expand All @@ -17,7 +18,7 @@
*/
final class BooleanTypeHandler implements RuleHandlerInterface
{
public function validate(mixed $value, object $rule, ValidationContext $context): Result
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof BooleanType) {
throw new UnexpectedRuleException(BooleanType::class, $rule);
Expand Down
9 changes: 5 additions & 4 deletions src/Rule/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Attribute;
use Closure;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -25,7 +25,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class FloatType implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
final class FloatType implements DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand All @@ -50,15 +50,16 @@ final class FloatType implements RuleWithOptionsInterface, SkipOnEmptyInterface,
*/
public function __construct(
private string $message = 'Value must be a float.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
{
return 'floatType';
return self::class;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Type/FloatTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

use function is_float;
Expand All @@ -17,7 +18,7 @@
*/
final class FloatTypeHandler implements RuleHandlerInterface
{
public function validate(mixed $value, object $rule, ValidationContext $context): Result
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof FloatType) {
throw new UnexpectedRuleException(FloatType::class, $rule);
Expand Down
9 changes: 5 additions & 4 deletions src/Rule/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Attribute;
use Closure;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
use Yiisoft\Validator\RuleWithOptionsInterface;
use Yiisoft\Validator\SkipOnEmptyInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;
Expand All @@ -25,7 +25,7 @@
* @psalm-import-type WhenType from WhenInterface
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class IntegerType implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
final class IntegerType implements DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface
{
use SkipOnEmptyTrait;
use SkipOnErrorTrait;
Expand All @@ -50,15 +50,16 @@ final class IntegerType implements RuleWithOptionsInterface, SkipOnEmptyInterfac
*/
public function __construct(
private string $message = 'Value must be an integer.',
private mixed $skipOnEmpty = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private Closure|null $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}

public function getName(): string
{
return 'integerType';
return self::class;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/Type/IntegerTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

use function is_int;
Expand All @@ -17,7 +18,7 @@
*/
final class IntegerTypeHandler implements RuleHandlerInterface
{
public function validate(mixed $value, object $rule, ValidationContext $context): Result
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof IntegerType) {
throw new UnexpectedRuleException(IntegerType::class, $rule);
Expand Down
Loading

0 comments on commit 92c51a9

Please sign in to comment.