From 1c2721d61cf2e8ee9bc625914655ddac7edcae8d Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 28 Nov 2023 19:16:33 +0600 Subject: [PATCH 1/7] Move out RuleInterface::getName() --- src/Helper/RulesDumper.php | 2 +- src/Rule/AtLeast.php | 2 +- src/Rule/BooleanValue.php | 2 +- src/Rule/Callback.php | 2 +- src/Rule/Compare.php | 2 +- src/Rule/Composite.php | 2 +- src/Rule/Count.php | 2 +- src/Rule/Each.php | 2 +- src/Rule/Email.php | 2 +- src/Rule/Equal.php | 2 +- src/Rule/GreaterThan.php | 2 +- src/Rule/GreaterThanOrEqual.php | 2 +- src/Rule/In.php | 2 +- src/Rule/Integer.php | 2 +- src/Rule/Ip.php | 2 +- src/Rule/Json.php | 2 +- src/Rule/Length.php | 2 +- src/Rule/LessThan.php | 2 +- src/Rule/LessThanOrEqual.php | 2 +- src/Rule/Nested.php | 2 +- src/Rule/NotEqual.php | 2 +- src/Rule/Number.php | 2 +- src/Rule/OneOf.php | 2 +- src/Rule/Regex.php | 2 +- src/Rule/Required.php | 2 +- src/Rule/StopOnError.php | 2 +- src/Rule/StringValue.php | 2 +- src/Rule/Subset.php | 2 +- src/Rule/TrueValue.php | 2 +- src/Rule/Url.php | 2 +- src/RuleInterface.php | 14 +------ src/RuleWithOptionsInterface.php | 16 ++++++-- tests/Helper/RulesDumperTest.php | 4 +- tests/Helper/RulesNormalizerTest.php | 20 +++++----- tests/Rule/AtLeastTest.php | 2 +- tests/Rule/BooleanValueTest.php | 2 +- tests/Rule/CallbackTest.php | 2 +- tests/Rule/CompareTest.php | 2 +- tests/Rule/CompositeTest.php | 13 ++++--- tests/Rule/CountTest.php | 2 +- tests/Rule/EachTest.php | 8 ++-- tests/Rule/EmailTest.php | 2 +- tests/Rule/EqualTest.php | 2 +- tests/Rule/GreaterThanOrEqualTest.php | 2 +- tests/Rule/GreaterThanTest.php | 2 +- tests/Rule/InTest.php | 2 +- tests/Rule/IpTest.php | 2 +- tests/Rule/JsonTest.php | 2 +- tests/Rule/LengthTest.php | 2 +- tests/Rule/LessThanOrEqualTest.php | 2 +- tests/Rule/LessThanTest.php | 2 +- tests/Rule/NestedTest.php | 41 ++++++++++---------- tests/Rule/NotEqualTest.php | 2 +- tests/Rule/NumberTest.php | 2 +- tests/Rule/OneOfTest.php | 2 +- tests/Rule/RegexTest.php | 2 +- tests/Rule/RequiredTest.php | 2 +- tests/Rule/RuleWithBuiltInHandler.php | 5 --- tests/Rule/StopOnErrorTest.php | 4 +- tests/Rule/StringValueTest.php | 2 +- tests/Rule/SubsetTest.php | 2 +- tests/Rule/TrueValueTest.php | 2 +- tests/Rule/UrlTest.php | 2 +- tests/Support/Rule/RuleWithCustomHandler.php | 5 --- 64 files changed, 113 insertions(+), 125 deletions(-) diff --git a/src/Helper/RulesDumper.php b/src/Helper/RulesDumper.php index bdc51571f..0ffb5d405 100644 --- a/src/Helper/RulesDumper.php +++ b/src/Helper/RulesDumper.php @@ -98,7 +98,7 @@ private static function fetchOptions(iterable $rules): array $options = self::fetchOptions($rule); } elseif ($rule instanceof RuleWithOptionsInterface) { $options = array_merge([$rule->getName()], $rule->getOptions()); - } elseif ($rule instanceof RuleInterface) { + } elseif ($rule instanceof RuleInterface) { $options = [$rule->getName()]; } else { throw new InvalidArgumentException(sprintf( diff --git a/src/Rule/AtLeast.php b/src/Rule/AtLeast.php index bf9c2c82d..e6016925b 100644 --- a/src/Rule/AtLeast.php +++ b/src/Rule/AtLeast.php @@ -74,7 +74,7 @@ public function __construct( public function getName(): string { - return 'atLeast'; + return self::class; } /** diff --git a/src/Rule/BooleanValue.php b/src/Rule/BooleanValue.php index c467b4322..447fd7115 100644 --- a/src/Rule/BooleanValue.php +++ b/src/Rule/BooleanValue.php @@ -91,7 +91,7 @@ public function __construct( public function getName(): string { - return 'boolean'; + return self::class; } /** diff --git a/src/Rule/Callback.php b/src/Rule/Callback.php index 2809b622d..16c40d2b4 100644 --- a/src/Rule/Callback.php +++ b/src/Rule/Callback.php @@ -71,7 +71,7 @@ public function __construct( public function getName(): string { - return 'callback'; + return self::class; } /** diff --git a/src/Rule/Compare.php b/src/Rule/Compare.php index 198003cc1..a7c3e1a4d 100644 --- a/src/Rule/Compare.php +++ b/src/Rule/Compare.php @@ -32,6 +32,6 @@ final class Compare extends AbstractCompare { public function getName(): string { - return 'compare'; + return self::class; } } diff --git a/src/Rule/Composite.php b/src/Rule/Composite.php index 4c311f992..625698c4b 100644 --- a/src/Rule/Composite.php +++ b/src/Rule/Composite.php @@ -136,7 +136,7 @@ public function __construct( public function getName(): string { - return 'composite'; + return self::class; } #[ArrayShape([ diff --git a/src/Rule/Count.php b/src/Rule/Count.php index 9f59ec388..e3ef3e52f 100644 --- a/src/Rule/Count.php +++ b/src/Rule/Count.php @@ -109,7 +109,7 @@ public function __construct( public function getName(): string { - return 'count'; + return self::class; } /** diff --git a/src/Rule/Each.php b/src/Rule/Each.php index c46e06fe6..64f719e68 100644 --- a/src/Rule/Each.php +++ b/src/Rule/Each.php @@ -123,7 +123,7 @@ public function __construct( public function getName(): string { - return 'each'; + return self::class; } public function propagateOptions(): void diff --git a/src/Rule/Email.php b/src/Rule/Email.php index 185d2a335..7c9d90600 100644 --- a/src/Rule/Email.php +++ b/src/Rule/Email.php @@ -126,7 +126,7 @@ public function __construct( public function getName(): string { - return 'email'; + return self::class; } /** diff --git a/src/Rule/Equal.php b/src/Rule/Equal.php index 32bea378c..0f009b26f 100644 --- a/src/Rule/Equal.php +++ b/src/Rule/Equal.php @@ -111,6 +111,6 @@ public function __construct( public function getName(): string { - return 'equal'; + return self::class; } } diff --git a/src/Rule/GreaterThan.php b/src/Rule/GreaterThan.php index 9317987e2..679490486 100644 --- a/src/Rule/GreaterThan.php +++ b/src/Rule/GreaterThan.php @@ -108,6 +108,6 @@ public function __construct( public function getName(): string { - return 'greaterThan'; + return self::class; } } diff --git a/src/Rule/GreaterThanOrEqual.php b/src/Rule/GreaterThanOrEqual.php index 455211ec4..f59ebacd3 100644 --- a/src/Rule/GreaterThanOrEqual.php +++ b/src/Rule/GreaterThanOrEqual.php @@ -109,6 +109,6 @@ public function __construct( public function getName(): string { - return 'greaterThanOrEqual'; + return self::class; } } diff --git a/src/Rule/In.php b/src/Rule/In.php index 0d115025c..63478f2df 100644 --- a/src/Rule/In.php +++ b/src/Rule/In.php @@ -80,7 +80,7 @@ public function __construct( public function getName(): string { - return 'inRange'; + return self::class; } /** diff --git a/src/Rule/Integer.php b/src/Rule/Integer.php index b79573a20..21aee90fd 100644 --- a/src/Rule/Integer.php +++ b/src/Rule/Integer.php @@ -91,6 +91,6 @@ public function __construct( public function getName(): string { - return 'integer'; + return self::class; } } diff --git a/src/Rule/Ip.php b/src/Rule/Ip.php index 792e7cdaa..b55af749c 100644 --- a/src/Rule/Ip.php +++ b/src/Rule/Ip.php @@ -219,7 +219,7 @@ public function __construct( public function getName(): string { - return 'ip'; + return self::class; } /** diff --git a/src/Rule/Json.php b/src/Rule/Json.php index 378c943da..d382c8203 100644 --- a/src/Rule/Json.php +++ b/src/Rule/Json.php @@ -64,7 +64,7 @@ public function __construct( public function getName(): string { - return 'json'; + return self::class; } /** diff --git a/src/Rule/Length.php b/src/Rule/Length.php index b1eab167d..4c71106c2 100644 --- a/src/Rule/Length.php +++ b/src/Rule/Length.php @@ -111,7 +111,7 @@ public function __construct( public function getName(): string { - return 'length'; + return self::class; } /** diff --git a/src/Rule/LessThan.php b/src/Rule/LessThan.php index 78aff5034..1896aa6ad 100644 --- a/src/Rule/LessThan.php +++ b/src/Rule/LessThan.php @@ -108,6 +108,6 @@ public function __construct( public function getName(): string { - return 'lessThan'; + return self::class; } } diff --git a/src/Rule/LessThanOrEqual.php b/src/Rule/LessThanOrEqual.php index 07d67a5e5..3f3033a64 100644 --- a/src/Rule/LessThanOrEqual.php +++ b/src/Rule/LessThanOrEqual.php @@ -108,6 +108,6 @@ public function __construct( public function getName(): string { - return 'lessThanOrEqual'; + return self::class; } } diff --git a/src/Rule/Nested.php b/src/Rule/Nested.php index 05d2eb5d2..e1bafde50 100644 --- a/src/Rule/Nested.php +++ b/src/Rule/Nested.php @@ -228,7 +228,7 @@ public function __construct( public function getName(): string { - return 'nested'; + return self::class; } /** diff --git a/src/Rule/NotEqual.php b/src/Rule/NotEqual.php index b71a4af74..a6504ef8e 100644 --- a/src/Rule/NotEqual.php +++ b/src/Rule/NotEqual.php @@ -111,6 +111,6 @@ public function __construct( public function getName(): string { - return 'notEqual'; + return self::class; } } diff --git a/src/Rule/Number.php b/src/Rule/Number.php index 8c809a118..14e01f758 100644 --- a/src/Rule/Number.php +++ b/src/Rule/Number.php @@ -91,6 +91,6 @@ public function __construct( public function getName(): string { - return 'number'; + return self::class; } } diff --git a/src/Rule/OneOf.php b/src/Rule/OneOf.php index 3efa07985..b8cb21526 100644 --- a/src/Rule/OneOf.php +++ b/src/Rule/OneOf.php @@ -64,7 +64,7 @@ public function __construct( public function getName(): string { - return 'oneOf'; + return self::class; } /** diff --git a/src/Rule/Regex.php b/src/Rule/Regex.php index 78b7926b2..fd8fcf01c 100644 --- a/src/Rule/Regex.php +++ b/src/Rule/Regex.php @@ -82,7 +82,7 @@ public function __construct( public function getName(): string { - return 'regex'; + return self::class; } /** diff --git a/src/Rule/Required.php b/src/Rule/Required.php index ddd03fff7..9409eef31 100644 --- a/src/Rule/Required.php +++ b/src/Rule/Required.php @@ -87,7 +87,7 @@ public function __construct( public function getName(): string { - return 'required'; + return self::class; } /** diff --git a/src/Rule/StopOnError.php b/src/Rule/StopOnError.php index 60fb6fcc9..766e516f3 100644 --- a/src/Rule/StopOnError.php +++ b/src/Rule/StopOnError.php @@ -90,7 +90,7 @@ public function __construct( public function getName(): string { - return 'stopOnError'; + return self::class; } /** diff --git a/src/Rule/StringValue.php b/src/Rule/StringValue.php index 25db251e0..d536e9850 100644 --- a/src/Rule/StringValue.php +++ b/src/Rule/StringValue.php @@ -62,7 +62,7 @@ public function __construct( public function getName(): string { - return 'string'; + return self::class; } /** diff --git a/src/Rule/Subset.php b/src/Rule/Subset.php index 711414bd0..acc724311 100644 --- a/src/Rule/Subset.php +++ b/src/Rule/Subset.php @@ -82,7 +82,7 @@ public function __construct( public function getName(): string { - return 'subset'; + return self::class; } /** diff --git a/src/Rule/TrueValue.php b/src/Rule/TrueValue.php index 9368b9179..14971b6a3 100644 --- a/src/Rule/TrueValue.php +++ b/src/Rule/TrueValue.php @@ -86,7 +86,7 @@ public function __construct( public function getName(): string { - return 'isTrue'; + return self::class; } /** diff --git a/src/Rule/Url.php b/src/Rule/Url.php index 591415e98..39a8ad884 100644 --- a/src/Rule/Url.php +++ b/src/Rule/Url.php @@ -105,7 +105,7 @@ public function __construct( public function getName(): string { - return 'url'; + return self::class; } /** diff --git a/src/RuleInterface.php b/src/RuleInterface.php index d9e0f9175..da1d86d41 100644 --- a/src/RuleInterface.php +++ b/src/RuleInterface.php @@ -6,23 +6,11 @@ /** * A main interface for rules to implement. A rule contains a set of constraint configuration options to apply when - * validating data. If you want to include a rule options in addition to a rule name during conversion to array, use + * validating data. If you want to include a rule options and customize its name during conversion to array, use * extended version of it - {@see RuleWithOptionsInterface}. */ interface RuleInterface { - /** - * Returns the name of a rule used during conversion to array. It's used for identification on the frontend with - * further implementing of client-side validation. This is explicitly specified for optimization and readability - * purposes. - * - * All packages' rule names use class name written in camelCase, so for `AtLeast` rule the name will be `atLeast. - * For custom rules you can choose different naming scheme because it doesn't affect logic in any way. - * - * @return string A rule name. - */ - public function getName(): string; - /** * A matching handler name or an instance used for processing this rule. * diff --git a/src/RuleWithOptionsInterface.php b/src/RuleWithOptionsInterface.php index 0f7130bce..889e3a41d 100644 --- a/src/RuleWithOptionsInterface.php +++ b/src/RuleWithOptionsInterface.php @@ -5,12 +5,20 @@ namespace Yiisoft\Validator; /** - * An extended version of {@see RuleInterface} which allows exporting a rule options in addition to a rule name. It's - * useful for passing to frontend for further identification and implementing client-side validation. If you don't need - * that (for example for REST API), use {@see RuleInterface} instead. + * An extended version of {@see RuleInterface} which allows exporting a rule options and name customization. It's useful + * for passing to frontend for further identification and implementing client-side validation. If you don't need that + * (for example for REST API), use {@see RuleInterface} instead. */ interface RuleWithOptionsInterface extends RuleInterface { + /** + * Returns the name of a rule used during conversion to array. It's used for identification on the frontend with + * further implementing of client-side validation. + * + * @return string A rule name. + */ + public function getName(): string; + /** * Gets a rule options as associative array. It's used for identification on the frontend with further implementing * of client-side validation. Usually it's just a mapping between rule property names and values. @@ -46,7 +54,7 @@ interface RuleWithOptionsInterface extends RuleInterface * ``` * * Note that the values that can't be serialized to frontend such as callable must be excluded because they will be - * useless on frontend. No exception will be thrown, so it's on the conscience of developer. + * useless on frontend. No exceptions are thrown in such cases. * * @return array A rule options. */ diff --git a/tests/Helper/RulesDumperTest.php b/tests/Helper/RulesDumperTest.php index ba6dde011..860e96064 100644 --- a/tests/Helper/RulesDumperTest.php +++ b/tests/Helper/RulesDumperTest.php @@ -34,7 +34,7 @@ public function asArrayDataProvider(): array [ 'attributeName' => [ $dump = [ - 'integer', + Integer::class, 'min' => 10, 'max' => 100, 'incorrectInputMessage' => [ @@ -102,7 +102,7 @@ public function testRuleWithoutOptions(): void ]; $expectedRules = [ [ - 'boolean', + BooleanValue::class, 'trueValue' => '1', 'falseValue' => '0', 'strict' => false, diff --git a/tests/Helper/RulesNormalizerTest.php b/tests/Helper/RulesNormalizerTest.php index eeee2ec38..d3dfc39bd 100644 --- a/tests/Helper/RulesNormalizerTest.php +++ b/tests/Helper/RulesNormalizerTest.php @@ -7,7 +7,9 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Validator\Helper\RulesNormalizer; use Yiisoft\Validator\Result; +use Yiisoft\Validator\Rule\Callback; use Yiisoft\Validator\Rule\Number; +use Yiisoft\Validator\Rule\Required; use Yiisoft\Validator\RuleInterface; use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility; @@ -19,17 +21,17 @@ public function dataNormalize(): array 'null' => [[], null], 'object' => [ [ - 'name' => ['required'], - 'age' => ['number'], - 'number' => ['number'], + 'name' => [Required::class], + 'age' => [Number::class], + 'number' => [Number::class], ], new ObjectWithDifferentPropertyVisibility(), ], 'class-string' => [ [ - 'name' => ['required'], - 'age' => ['number'], - 'number' => ['number'], + 'name' => [Required::class], + 'age' => [Number::class], + 'number' => [Number::class], ], ObjectWithDifferentPropertyVisibility::class, ], @@ -74,15 +76,15 @@ public function dataNormalizeList(): array [], ], [ - ['callback'], + [Callback::class], static fn () => new Result(), ], [ - ['number'], + [Number::class], new Number(), ], [ - ['number', 'callback'], + [Number::class, Callback::class], [new Number(), static fn () => new Result()], ], ]; diff --git a/tests/Rule/AtLeastTest.php b/tests/Rule/AtLeastTest.php index 0483e3ffe..ebf9a55c5 100644 --- a/tests/Rule/AtLeastTest.php +++ b/tests/Rule/AtLeastTest.php @@ -32,7 +32,7 @@ public function testMinGreaterThanAttributesCount(): void public function testGetName(): void { $rule = new AtLeast(['attr']); - $this->assertSame('atLeast', $rule->getName()); + $this->assertSame(AtLeast::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/BooleanValueTest.php b/tests/Rule/BooleanValueTest.php index a8355f0fc..688d061c5 100644 --- a/tests/Rule/BooleanValueTest.php +++ b/tests/Rule/BooleanValueTest.php @@ -22,7 +22,7 @@ final class BooleanValueTest extends RuleTestCase public function testGetName(): void { $rule = new BooleanValue(); - $this->assertSame('boolean', $rule->getName()); + $this->assertSame(BooleanValue::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/CallbackTest.php b/tests/Rule/CallbackTest.php index 703ce2b50..24efb6e22 100644 --- a/tests/Rule/CallbackTest.php +++ b/tests/Rule/CallbackTest.php @@ -46,7 +46,7 @@ public function testInitWithBothCallbackAndMethodException(): void public function testGetName(): void { $rule = new Callback(callback: static fn (): Result => new Result()); - $this->assertSame('callback', $rule->getName()); + $this->assertSame(Callback::class, $rule->getName()); } public function testGetMethod(): void diff --git a/tests/Rule/CompareTest.php b/tests/Rule/CompareTest.php index d20b304ed..7693d5761 100644 --- a/tests/Rule/CompareTest.php +++ b/tests/Rule/CompareTest.php @@ -51,7 +51,7 @@ public function testInitWithWrongOperator(): void public function testGetName(): void { $rule = new Compare(); - $this->assertSame('compare', $rule->getName()); + $this->assertSame(Compare::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/CompositeTest.php b/tests/Rule/CompositeTest.php index 9d82a0830..f4d4c4b4a 100644 --- a/tests/Rule/CompositeTest.php +++ b/tests/Rule/CompositeTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Validator\Tests\Rule; use Yiisoft\Validator\Result; +use Yiisoft\Validator\Rule\Callback; use Yiisoft\Validator\Rule\Composite; use Yiisoft\Validator\Rule\CompositeHandler; use Yiisoft\Validator\Rule\Equal; @@ -30,7 +31,7 @@ final class CompositeTest extends RuleTestCase public function testGetName(): void { $rule = new Composite([]); - $this->assertSame('composite', $rule->getName()); + $this->assertSame(Composite::class, $rule->getName()); } public function dataOptions(): array @@ -46,7 +47,7 @@ public function dataOptions(): array 'skipOnError' => false, 'rules' => [ [ - 'number', + Number::class, 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ @@ -70,7 +71,7 @@ public function dataOptions(): array 'pattern' => '/1/', ], [ - 'number', + Number::class, 'min' => null, 'max' => 14, 'incorrectInputMessage' => [ @@ -106,7 +107,7 @@ public function dataOptions(): array 'skipOnError' => false, 'rules' => [ [ - 'number', + Number::class, 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ @@ -148,7 +149,7 @@ public function dataOptions(): array 'skipOnError' => false, 'rules' => [ [ - 'callback', + Callback::class, 'method' => null, 'skipOnEmpty' => false, 'skipOnError' => false, @@ -177,7 +178,7 @@ public function getOptions(): array 'specific-key' => 42, 'rules' => [ [ - 'required', + Required::class, 'message' => [ 'template' => 'Value cannot be blank.', 'parameters' => [], diff --git a/tests/Rule/CountTest.php b/tests/Rule/CountTest.php index 1924761fb..12ebe2418 100644 --- a/tests/Rule/CountTest.php +++ b/tests/Rule/CountTest.php @@ -29,7 +29,7 @@ final class CountTest extends RuleTestCase public function testGetName(): void { $rule = new Count(min: 3); - $this->assertSame('count', $rule->getName()); + $this->assertSame(Count::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/EachTest.php b/tests/Rule/EachTest.php index 33e1b2e81..247bcc098 100644 --- a/tests/Rule/EachTest.php +++ b/tests/Rule/EachTest.php @@ -30,7 +30,7 @@ final class EachTest extends RuleTestCase public function testGetName(): void { $rule = new Each(); - $this->assertSame('each', $rule->getName()); + $this->assertSame(Each::class, $rule->getName()); } public function dataOptions(): array @@ -55,7 +55,7 @@ public function dataOptions(): array 'rules' => [ [ [ - 'number', + Number::class, 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ @@ -81,7 +81,7 @@ public function dataOptions(): array ], [ [ - 'number', + Number::class, 'min' => null, 'max' => 14, 'incorrectInputMessage' => [ @@ -127,7 +127,7 @@ public function dataOptions(): array 'rules' => [ [ [ - 'number', + Number::class, 'min' => null, 'max' => 13, 'incorrectInputMessage' => [ diff --git a/tests/Rule/EmailTest.php b/tests/Rule/EmailTest.php index 1d457a886..2525339ed 100644 --- a/tests/Rule/EmailTest.php +++ b/tests/Rule/EmailTest.php @@ -42,7 +42,7 @@ public function testinvalidConfiguration(array $arguments, string $expectedExcep public function testGetName(): void { $rule = new Email(); - $this->assertSame('email', $rule->getName()); + $this->assertSame(Email::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/EqualTest.php b/tests/Rule/EqualTest.php index cdedc8cf6..a5b4602ea 100644 --- a/tests/Rule/EqualTest.php +++ b/tests/Rule/EqualTest.php @@ -21,7 +21,7 @@ final class EqualTest extends RuleTestCase public function testGetName(): void { $rule = new Equal(1); - $this->assertSame('equal', $rule->getName()); + $this->assertSame(Equal::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/GreaterThanOrEqualTest.php b/tests/Rule/GreaterThanOrEqualTest.php index 5d5752c77..6fefe827c 100644 --- a/tests/Rule/GreaterThanOrEqualTest.php +++ b/tests/Rule/GreaterThanOrEqualTest.php @@ -21,7 +21,7 @@ final class GreaterThanOrEqualTest extends RuleTestCase public function testGetName(): void { $rule = new GreaterThanOrEqual(1); - $this->assertSame('greaterThanOrEqual', $rule->getName()); + $this->assertSame(GreaterThanOrEqual::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/GreaterThanTest.php b/tests/Rule/GreaterThanTest.php index 334373ccc..e974d1ee6 100644 --- a/tests/Rule/GreaterThanTest.php +++ b/tests/Rule/GreaterThanTest.php @@ -21,7 +21,7 @@ final class GreaterThanTest extends RuleTestCase public function testGetName(): void { $rule = new GreaterThan(1); - $this->assertSame('greaterThan', $rule->getName()); + $this->assertSame(GreaterThan::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/InTest.php b/tests/Rule/InTest.php index 62992658f..d012d9730 100644 --- a/tests/Rule/InTest.php +++ b/tests/Rule/InTest.php @@ -23,7 +23,7 @@ final class InTest extends RuleTestCase public function testGetName(): void { $rule = new In(range(1, 10)); - $this->assertSame('inRange', $rule->getName()); + $this->assertSame(In::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/IpTest.php b/tests/Rule/IpTest.php index a71d61610..775792192 100644 --- a/tests/Rule/IpTest.php +++ b/tests/Rule/IpTest.php @@ -23,7 +23,7 @@ final class IpTest extends RuleTestCase public function testGetName(): void { $rule = new Ip(); - $this->assertSame('ip', $rule->getName()); + $this->assertSame(Ip::class, $rule->getName()); } public function getNetworksData(): array diff --git a/tests/Rule/JsonTest.php b/tests/Rule/JsonTest.php index 851947e8b..07341cbd0 100644 --- a/tests/Rule/JsonTest.php +++ b/tests/Rule/JsonTest.php @@ -22,7 +22,7 @@ final class JsonTest extends RuleTestCase public function testGetName(): void { $rule = new Json(); - $this->assertSame('json', $rule->getName()); + $this->assertSame(Json::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/LengthTest.php b/tests/Rule/LengthTest.php index dc3c3882b..3afd0f217 100644 --- a/tests/Rule/LengthTest.php +++ b/tests/Rule/LengthTest.php @@ -26,7 +26,7 @@ final class LengthTest extends RuleTestCase public function testGetName(): void { $rule = new Length(min: 3); - $this->assertSame('length', $rule->getName()); + $this->assertSame(Length::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/LessThanOrEqualTest.php b/tests/Rule/LessThanOrEqualTest.php index 352be5435..856825e60 100644 --- a/tests/Rule/LessThanOrEqualTest.php +++ b/tests/Rule/LessThanOrEqualTest.php @@ -21,7 +21,7 @@ final class LessThanOrEqualTest extends RuleTestCase public function testGetName(): void { $rule = new LessThanOrEqual(1); - $this->assertSame('lessThanOrEqual', $rule->getName()); + $this->assertSame(LessThanOrEqual::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/LessThanTest.php b/tests/Rule/LessThanTest.php index edbab0d81..9d0b5aa22 100644 --- a/tests/Rule/LessThanTest.php +++ b/tests/Rule/LessThanTest.php @@ -21,7 +21,7 @@ final class LessThanTest extends RuleTestCase public function testGetName(): void { $rule = new LessThan(1); - $this->assertSame('lessThan', $rule->getName()); + $this->assertSame(LessThan::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/NestedTest.php b/tests/Rule/NestedTest.php index 5d80aa154..595d27e01 100644 --- a/tests/Rule/NestedTest.php +++ b/tests/Rule/NestedTest.php @@ -56,8 +56,7 @@ final class NestedTest extends RuleTestCase public function testGetName(): void { $rule = new Nested(); - - $this->assertSame('nested', $rule->getName()); + $this->assertSame(Nested::class, $rule->getName()); } public function testDefaultValues(): void @@ -117,7 +116,7 @@ public function dataOptions(): array 'skipOnError' => false, 'rules' => [ [ - 'number', + Number::class, 'min' => null, 'max' => null, 'incorrectInputMessage' => [ @@ -168,7 +167,7 @@ public function dataOptions(): array 'rules' => [ 'user.age' => [ [ - 'number', + Number::class, 'min' => null, 'max' => null, 'incorrectInputMessage' => [ @@ -330,7 +329,7 @@ public function dataHandler(): array private array $array = [ 'name' => 'hello', 'age' => 17, - 'number' => 500, + Number::class => 500, ]; public function getRules(): iterable @@ -474,56 +473,56 @@ public function dataPropagateOptions(): array ), [ [ - 'nested', + Nested::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ 'posts' => [ [ - 'each', + Each::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ [ [ - 'nested', + Nested::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ 'title' => [ [ - 'length', + Length::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], ], 'authors' => [ [ - 'each', + Each::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ [ [ - 'nested', + Nested::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ 'data.name' => [ [ - 'length', + Length::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], ], 'data.age' => [ [ - 'number', + Number::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], [ - 'number', + Number::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], @@ -542,7 +541,7 @@ public function dataPropagateOptions(): array ], 'meta' => [ [ - 'length', + Length::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], @@ -555,7 +554,7 @@ public function dataPropagateOptions(): array new Nested(propagateOptions: true), [ [ - 'nested', + Nested::class, 'skipOnEmpty' => false, 'skipOnError' => false, 'rules' => null, @@ -571,12 +570,12 @@ public function dataPropagateOptions(): array ), [ [ - 'nested', + Nested::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ [ - 'atLeast', + AtLeast::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], @@ -595,19 +594,19 @@ public function dataPropagateOptions(): array ), [ [ - 'nested', + Nested::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ 'numbers' => [ [ - 'each', + Each::class, 'skipOnEmpty' => true, 'skipOnError' => true, 'rules' => [ [ [ - 'number', + Number::class, 'skipOnEmpty' => true, 'skipOnError' => true, ], diff --git a/tests/Rule/NotEqualTest.php b/tests/Rule/NotEqualTest.php index 76e06d7f3..b0ab7694b 100644 --- a/tests/Rule/NotEqualTest.php +++ b/tests/Rule/NotEqualTest.php @@ -21,7 +21,7 @@ final class NotEqualTest extends RuleTestCase public function testGetName(): void { $rule = new NotEqual(1); - $this->assertSame('notEqual', $rule->getName()); + $this->assertSame(NotEqual::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/NumberTest.php b/tests/Rule/NumberTest.php index f7984ddb8..448393f29 100644 --- a/tests/Rule/NumberTest.php +++ b/tests/Rule/NumberTest.php @@ -36,7 +36,7 @@ public function testIntegerEmptyPattern(): void public function testGetName(): void { $rule = new Number(); - $this->assertSame('number', $rule->getName()); + $this->assertSame(Number::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/OneOfTest.php b/tests/Rule/OneOfTest.php index 17f7f2df7..98c1deb13 100644 --- a/tests/Rule/OneOfTest.php +++ b/tests/Rule/OneOfTest.php @@ -24,7 +24,7 @@ final class OneOfTest extends RuleTestCase public function testGetName(): void { $rule = new OneOf([]); - $this->assertSame('oneOf', $rule->getName()); + $this->assertSame(OneOf::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/RegexTest.php b/tests/Rule/RegexTest.php index eaed987e9..0c38a0b85 100644 --- a/tests/Rule/RegexTest.php +++ b/tests/Rule/RegexTest.php @@ -31,7 +31,7 @@ public function testNumberEmptyPattern(): void public function testGetName(): void { $rule = new Regex('//'); - $this->assertSame('regex', $rule->getName()); + $this->assertSame(Regex::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/RequiredTest.php b/tests/Rule/RequiredTest.php index 31ca76751..761800136 100644 --- a/tests/Rule/RequiredTest.php +++ b/tests/Rule/RequiredTest.php @@ -30,7 +30,7 @@ public function testDefaultValues(): void $this->assertNull($rule->getEmptyCondition()); $this->assertSame(RequiredHandler::class, $rule->getHandler()); $this->assertSame('Value cannot be blank.', $rule->getMessage()); - $this->assertSame('required', $rule->getName()); + $this->assertSame(Required::class, $rule->getName()); $this->assertSame('Value not passed.', $rule->getNotPassedMessage()); $this->assertNull($rule->getWhen()); $this->assertFalse($rule->shouldSkipOnError()); diff --git a/tests/Rule/RuleWithBuiltInHandler.php b/tests/Rule/RuleWithBuiltInHandler.php index eb8ba6b56..d8e4ad332 100644 --- a/tests/Rule/RuleWithBuiltInHandler.php +++ b/tests/Rule/RuleWithBuiltInHandler.php @@ -22,11 +22,6 @@ public function validate(mixed $value, object $rule, ValidationContext $context) return $result; } - public function getName(): string - { - return 'is42'; - } - public function getHandler(): string|RuleHandlerInterface { return $this; diff --git a/tests/Rule/StopOnErrorTest.php b/tests/Rule/StopOnErrorTest.php index d38aa23ca..68495dc26 100644 --- a/tests/Rule/StopOnErrorTest.php +++ b/tests/Rule/StopOnErrorTest.php @@ -26,7 +26,7 @@ final class StopOnErrorTest extends RuleTestCase public function testGetName(): void { $rule = new StopOnError([new Length(min: 10)]); - $this->assertSame('stopOnError', $rule->getName()); + $this->assertSame(StopOnError::class, $rule->getName()); } public function dataOptions(): array @@ -39,7 +39,7 @@ public function dataOptions(): array 'skipOnError' => false, 'rules' => [ [ - 'length', + Length::class, 'min' => 10, 'max' => null, 'exactly' => null, diff --git a/tests/Rule/StringValueTest.php b/tests/Rule/StringValueTest.php index 4d6aa1c09..e744027c2 100644 --- a/tests/Rule/StringValueTest.php +++ b/tests/Rule/StringValueTest.php @@ -27,7 +27,7 @@ final class StringValueTest extends RuleTestCase public function testGetName(): void { $rule = new StringValue(); - $this->assertSame('string', $rule->getName()); + $this->assertSame(StringValue::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/SubsetTest.php b/tests/Rule/SubsetTest.php index 83cb247a3..6df92bf1b 100644 --- a/tests/Rule/SubsetTest.php +++ b/tests/Rule/SubsetTest.php @@ -24,7 +24,7 @@ final class SubsetTest extends RuleTestCase public function testGetName(): void { $rule = new Subset([]); - $this->assertSame('subset', $rule->getName()); + $this->assertSame(Subset::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/TrueValueTest.php b/tests/Rule/TrueValueTest.php index 166b4f9dc..99212397f 100644 --- a/tests/Rule/TrueValueTest.php +++ b/tests/Rule/TrueValueTest.php @@ -22,7 +22,7 @@ final class TrueValueTest extends RuleTestCase public function testGetName(): void { $rule = new TrueValue(); - $this->assertSame('isTrue', $rule->getName()); + $this->assertSame(TrueValue::class, $rule->getName()); } public function dataOptions(): array diff --git a/tests/Rule/UrlTest.php b/tests/Rule/UrlTest.php index 9c9fd7cba..75bb83e22 100644 --- a/tests/Rule/UrlTest.php +++ b/tests/Rule/UrlTest.php @@ -32,7 +32,7 @@ public function testDefaultValues(): void { $rule = new Url(); - $this->assertSame('url', $rule->getName()); + $this->assertSame(Url::class, $rule->getName()); $this->assertSame(['http', 'https'], $rule->getValidSchemes()); } diff --git a/tests/Support/Rule/RuleWithCustomHandler.php b/tests/Support/Rule/RuleWithCustomHandler.php index fc20e0ef9..6e4418d15 100644 --- a/tests/Support/Rule/RuleWithCustomHandler.php +++ b/tests/Support/Rule/RuleWithCustomHandler.php @@ -13,11 +13,6 @@ public function __construct( ) { } - public function getName(): string - { - return 'rule-with-custom-handler'; - } - public function getHandler(): string { return $this->handlerClassName; From ef42fb3a12f3fdf7f839b8656b133d39c3624125 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 28 Nov 2023 19:26:02 +0600 Subject: [PATCH 2/7] Adjust docs and rename interface --- docs/guide/en/client-side-validation.md | 2 +- .../en/configuring-rules-via-php-attributes.md | 5 ----- docs/guide/en/creating-custom-rules.md | 17 +---------------- ...onsInterface.php => DumpedRuleInterface.php} | 2 +- src/Helper/RulesDumper.php | 6 +++--- src/Rule/AbstractCompare.php | 4 ++-- src/Rule/AbstractNumber.php | 4 ++-- src/Rule/AtLeast.php | 4 ++-- src/Rule/BooleanValue.php | 4 ++-- src/Rule/Callback.php | 4 ++-- src/Rule/Composite.php | 4 ++-- src/Rule/Count.php | 4 ++-- src/Rule/Each.php | 4 ++-- src/Rule/Email.php | 4 ++-- src/Rule/In.php | 4 ++-- src/Rule/Ip.php | 4 ++-- src/Rule/Json.php | 4 ++-- src/Rule/Length.php | 4 ++-- src/Rule/Nested.php | 4 ++-- src/Rule/OneOf.php | 4 ++-- src/Rule/Regex.php | 4 ++-- src/Rule/Required.php | 4 ++-- src/Rule/StopOnError.php | 4 ++-- src/Rule/StringValue.php | 4 ++-- src/Rule/Subset.php | 4 ++-- src/Rule/Trait/CountableLimitTrait.php | 2 +- src/Rule/Trait/SkipOnEmptyTrait.php | 4 ++-- src/Rule/TrueValue.php | 4 ++-- src/Rule/Url.php | 4 ++-- src/RuleInterface.php | 2 +- tests/Rule/Base/RuleWithOptionsTestTrait.php | 4 ++-- tests/Rule/Base/RuleWithProvidedRulesTrait.php | 4 ++-- tests/Rule/NestedTest.php | 10 +++++----- ...ubRuleWithOptions.php => StubDumpedRule.php} | 4 ++-- tests/ValidatorTest.php | 4 ++-- 35 files changed, 67 insertions(+), 87 deletions(-) rename src/{RuleWithOptionsInterface.php => DumpedRuleInterface.php} (97%) rename tests/Support/Rule/StubRule/{StubRuleWithOptions.php => StubDumpedRule.php} (79%) diff --git a/docs/guide/en/client-side-validation.md b/docs/guide/en/client-side-validation.md index d522ebfd4..a96fa34b5 100644 --- a/docs/guide/en/client-side-validation.md +++ b/docs/guide/en/client-side-validation.md @@ -31,7 +31,7 @@ the output will be: [ 'name' => [ [ - 'length', + 'Yiisoft\Validator\Rule\Length', 'min' => 4, 'max' => 10, 'exactly' => null, diff --git a/docs/guide/en/configuring-rules-via-php-attributes.md b/docs/guide/en/configuring-rules-via-php-attributes.md index 1317191ee..8cdb876df 100644 --- a/docs/guide/en/configuring-rules-via-php-attributes.md +++ b/docs/guide/en/configuring-rules-via-php-attributes.md @@ -289,11 +289,6 @@ final class Yaml implements RuleInterface ) { } - public function getName(): string - { - return 'yaml'; - } - public function getHandler(): string { return YamlHandler::class; diff --git a/docs/guide/en/creating-custom-rules.md b/docs/guide/en/creating-custom-rules.md index 69c2a548f..8012b65aa 100644 --- a/docs/guide/en/creating-custom-rules.md +++ b/docs/guide/en/creating-custom-rules.md @@ -31,12 +31,7 @@ final class RgbColor implements RuleInterface public function __construct( public readonly string $message = 'Invalid RGB color value.', ) { - } - - public function getName(): string - { - return 'rgbColor'; - } + } public function getHandler(): string { @@ -118,11 +113,6 @@ final class RgbColor implements RuleInterface ) { } - public function getName(): string - { - return 'rgbColor'; - } - public function getHandler(): string { return RgbColorHandler::class; @@ -330,11 +320,6 @@ final class Yaml implements RuleInterface ) { } - public function getName(): string - { - return 'yaml'; - } - public function getHandler(): string { return YamlHandler::class; diff --git a/src/RuleWithOptionsInterface.php b/src/DumpedRuleInterface.php similarity index 97% rename from src/RuleWithOptionsInterface.php rename to src/DumpedRuleInterface.php index 889e3a41d..70f51e139 100644 --- a/src/RuleWithOptionsInterface.php +++ b/src/DumpedRuleInterface.php @@ -9,7 +9,7 @@ * for passing to frontend for further identification and implementing client-side validation. If you don't need that * (for example for REST API), use {@see RuleInterface} instead. */ -interface RuleWithOptionsInterface extends RuleInterface +interface DumpedRuleInterface extends RuleInterface { /** * Returns the name of a rule used during conversion to array. It's used for identification on the frontend with diff --git a/src/Helper/RulesDumper.php b/src/Helper/RulesDumper.php index 0ffb5d405..2c0e7cf96 100644 --- a/src/Helper/RulesDumper.php +++ b/src/Helper/RulesDumper.php @@ -6,7 +6,7 @@ use InvalidArgumentException; use Yiisoft\Validator\RuleInterface; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use function is_int; use function is_string; @@ -16,7 +16,7 @@ * The array is usually passed to the client to use it in client-side validation. * * @see RuleInterface - * @see RuleWithOptionsInterface + * @see DumpedRuleInterface */ final class RulesDumper { @@ -96,7 +96,7 @@ private static function fetchOptions(iterable $rules): array if (is_iterable($rule)) { $options = self::fetchOptions($rule); - } elseif ($rule instanceof RuleWithOptionsInterface) { + } elseif ($rule instanceof DumpedRuleInterface) { $options = array_merge([$rule->getName()], $rule->getOptions()); } elseif ($rule instanceof RuleInterface) { $options = [$rule->getName()]; diff --git a/src/Rule/AbstractCompare.php b/src/Rule/AbstractCompare.php index 62fd12e3a..8aee11010 100644 --- a/src/Rule/AbstractCompare.php +++ b/src/Rule/AbstractCompare.php @@ -11,7 +11,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -34,7 +34,7 @@ * @psalm-import-type WhenType from WhenInterface */ abstract class AbstractCompare implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface diff --git a/src/Rule/AbstractNumber.php b/src/Rule/AbstractNumber.php index 1b856e772..ad9b1319f 100644 --- a/src/Rule/AbstractNumber.php +++ b/src/Rule/AbstractNumber.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -26,7 +26,7 @@ * @psalm-import-type WhenType from WhenInterface */ abstract class AbstractNumber implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface diff --git a/src/Rule/AtLeast.php b/src/Rule/AtLeast.php index e6016925b..8ffead4dd 100644 --- a/src/Rule/AtLeast.php +++ b/src/Rule/AtLeast.php @@ -10,7 +10,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class AtLeast implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class AtLeast implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/BooleanValue.php b/src/Rule/BooleanValue.php index 447fd7115..76eb9edb5 100644 --- a/src/Rule/BooleanValue.php +++ b/src/Rule/BooleanValue.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class BooleanValue implements RuleWithOptionsInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface +final class BooleanValue implements DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Callback.php b/src/Rule/Callback.php index 16c40d2b4..f8d9dedf7 100644 --- a/src/Rule/Callback.php +++ b/src/Rule/Callback.php @@ -12,7 +12,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -26,7 +26,7 @@ */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class Callback implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface, diff --git a/src/Rule/Composite.php b/src/Rule/Composite.php index 625698c4b..6349fb0d4 100644 --- a/src/Rule/Composite.php +++ b/src/Rule/Composite.php @@ -13,7 +13,7 @@ use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; use Yiisoft\Validator\Rule\Trait\WhenTrait; use Yiisoft\Validator\Helper\RulesDumper; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\ValidatorInterface; @@ -74,7 +74,7 @@ */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] class Composite implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface, diff --git a/src/Rule/Count.php b/src/Rule/Count.php index e3ef3e52f..57316dab3 100644 --- a/src/Rule/Count.php +++ b/src/Rule/Count.php @@ -12,7 +12,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class Count implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface, diff --git a/src/Rule/Each.php b/src/Rule/Each.php index 64f719e68..d0be9421f 100644 --- a/src/Rule/Each.php +++ b/src/Rule/Each.php @@ -16,7 +16,7 @@ use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; use Yiisoft\Validator\Rule\Trait\WhenTrait; use Yiisoft\Validator\Helper\RulesDumper; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\ValidatorInterface; @@ -63,7 +63,7 @@ */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class Each implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface, diff --git a/src/Rule/Email.php b/src/Rule/Email.php index 7c9d90600..3fe3e1317 100644 --- a/src/Rule/Email.php +++ b/src/Rule/Email.php @@ -11,7 +11,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -26,7 +26,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Email implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class Email implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/In.php b/src/Rule/In.php index 63478f2df..93e103e8d 100644 --- a/src/Rule/In.php +++ b/src/Rule/In.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -31,7 +31,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class In implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class In implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Ip.php b/src/Rule/Ip.php index b55af749c..1ecb6d3d5 100644 --- a/src/Rule/Ip.php +++ b/src/Rule/Ip.php @@ -11,7 +11,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ * @see IpHandler */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Ip implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class Ip implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Json.php b/src/Rule/Json.php index d382c8203..d7dcc6105 100644 --- a/src/Rule/Json.php +++ b/src/Rule/Json.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -24,7 +24,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Json implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class Json implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Length.php b/src/Rule/Length.php index 4c71106c2..9dfbf0301 100644 --- a/src/Rule/Length.php +++ b/src/Rule/Length.php @@ -11,7 +11,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class Length implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface, diff --git a/src/Rule/Nested.php b/src/Rule/Nested.php index e1bafde50..952b1b223 100644 --- a/src/Rule/Nested.php +++ b/src/Rule/Nested.php @@ -22,7 +22,7 @@ use Yiisoft\Validator\Helper\RulesDumper; use Yiisoft\Validator\RulesProvider\AttributesRulesProvider; use Yiisoft\Validator\RulesProviderInterface; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\Tests\Rule\NestedTest; @@ -101,7 +101,7 @@ */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class Nested implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface, diff --git a/src/Rule/OneOf.php b/src/Rule/OneOf.php index b8cb21526..a36bcd0fd 100644 --- a/src/Rule/OneOf.php +++ b/src/Rule/OneOf.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -24,7 +24,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class OneOf implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class OneOf implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Regex.php b/src/Rule/Regex.php index fd8fcf01c..1cb8c6623 100644 --- a/src/Rule/Regex.php +++ b/src/Rule/Regex.php @@ -11,7 +11,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -26,7 +26,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Regex implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class Regex implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Required.php b/src/Rule/Required.php index 9409eef31..04da4829d 100644 --- a/src/Rule/Required.php +++ b/src/Rule/Required.php @@ -8,7 +8,7 @@ use Closure; use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; use Yiisoft\Validator\Rule\Trait\WhenTrait; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -34,7 +34,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Required implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface +final class Required implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface { use SkipOnErrorTrait; use WhenTrait; diff --git a/src/Rule/StopOnError.php b/src/Rule/StopOnError.php index 766e516f3..9447025b7 100644 --- a/src/Rule/StopOnError.php +++ b/src/Rule/StopOnError.php @@ -13,7 +13,7 @@ use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; use Yiisoft\Validator\Rule\Trait\WhenTrait; use Yiisoft\Validator\Helper\RulesDumper; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\ValidatorInterface; @@ -47,7 +47,7 @@ */ #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class StopOnError implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface, diff --git a/src/Rule/StringValue.php b/src/Rule/StringValue.php index d536e9850..ffb9c2fca 100644 --- a/src/Rule/StringValue.php +++ b/src/Rule/StringValue.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class StringValue implements - RuleWithOptionsInterface, + DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface diff --git a/src/Rule/Subset.php b/src/Rule/Subset.php index acc724311..324fdcad8 100644 --- a/src/Rule/Subset.php +++ b/src/Rule/Subset.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -28,7 +28,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Subset implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class Subset implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Trait/CountableLimitTrait.php b/src/Rule/Trait/CountableLimitTrait.php index 87cf5e65f..933a88145 100644 --- a/src/Rule/Trait/CountableLimitTrait.php +++ b/src/Rule/Trait/CountableLimitTrait.php @@ -35,7 +35,7 @@ * } * ``` * - * Also, if a rule implements {@see RuleWithOptionsInterface}, you can merge limit related options instead of adding it + * Also, if a rule implements {@see DumpedRuleInterface}, you can merge limit related options instead of adding it * manually: * * ```php diff --git a/src/Rule/Trait/SkipOnEmptyTrait.php b/src/Rule/Trait/SkipOnEmptyTrait.php index 2bae0da55..0b5e66f7a 100644 --- a/src/Rule/Trait/SkipOnEmptyTrait.php +++ b/src/Rule/Trait/SkipOnEmptyTrait.php @@ -4,7 +4,7 @@ namespace Yiisoft\Validator\Rule\Trait; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use function is_bool; @@ -50,7 +50,7 @@ public function getSkipOnEmpty(): bool|callable|null /** * A special method used to cast `$skipOnEmpty` property for serialization to be possible. Used when building - * {@see RuleWithOptionsInterface::getOptions()}. The missing details need to be recreated separately on the client + * {@see DumpedRuleInterface::getOptions()}. The missing details need to be recreated separately on the client * side. * * @return bool|null A casted value: diff --git a/src/Rule/TrueValue.php b/src/Rule/TrueValue.php index 14971b6a3..33cff99d2 100644 --- a/src/Rule/TrueValue.php +++ b/src/Rule/TrueValue.php @@ -9,7 +9,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -27,7 +27,7 @@ * @psalm-import-type WhenType from WhenInterface */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class TrueValue implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class TrueValue implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/Rule/Url.php b/src/Rule/Url.php index 39a8ad884..943d32900 100644 --- a/src/Rule/Url.php +++ b/src/Rule/Url.php @@ -11,7 +11,7 @@ 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\DumpedRuleInterface; use Yiisoft\Validator\SkipOnEmptyInterface; use Yiisoft\Validator\SkipOnErrorInterface; use Yiisoft\Validator\WhenInterface; @@ -29,7 +29,7 @@ * @see UrlHandler */ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Url implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface +final class Url implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface { use SkipOnEmptyTrait; use SkipOnErrorTrait; diff --git a/src/RuleInterface.php b/src/RuleInterface.php index da1d86d41..cd39cdc17 100644 --- a/src/RuleInterface.php +++ b/src/RuleInterface.php @@ -7,7 +7,7 @@ /** * A main interface for rules to implement. A rule contains a set of constraint configuration options to apply when * validating data. If you want to include a rule options and customize its name during conversion to array, use - * extended version of it - {@see RuleWithOptionsInterface}. + * extended version of it - {@see DumpedRuleInterface}. */ interface RuleInterface { diff --git a/tests/Rule/Base/RuleWithOptionsTestTrait.php b/tests/Rule/Base/RuleWithOptionsTestTrait.php index 6a5eac788..b88ebaa5a 100644 --- a/tests/Rule/Base/RuleWithOptionsTestTrait.php +++ b/tests/Rule/Base/RuleWithOptionsTestTrait.php @@ -4,7 +4,7 @@ namespace Yiisoft\Validator\Tests\Rule\Base; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; trait RuleWithOptionsTestTrait { @@ -13,7 +13,7 @@ abstract public function dataOptions(): array; /** * @dataProvider dataOptions */ - public function testOptions(RuleWithOptionsInterface $rule, array $expectedOptions): void + public function testOptions(DumpedRuleInterface $rule, array $expectedOptions): void { $options = $rule->getOptions(); $this->assertSame($expectedOptions, $options); diff --git a/tests/Rule/Base/RuleWithProvidedRulesTrait.php b/tests/Rule/Base/RuleWithProvidedRulesTrait.php index 32de0c2ff..50f2403bc 100644 --- a/tests/Rule/Base/RuleWithProvidedRulesTrait.php +++ b/tests/Rule/Base/RuleWithProvidedRulesTrait.php @@ -8,7 +8,7 @@ use Yiisoft\Validator\Rule\Number; use Yiisoft\Validator\Rule\Required; use Yiisoft\Validator\RuleInterface; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; trait RuleWithProvidedRulesTrait { @@ -22,7 +22,7 @@ private function testGetOptionsWithNotRuleInternal($ruleClassName): void }, new Number(min: 1), ]); - $this->assertInstanceOf(RuleWithOptionsInterface::class, $rule); + $this->assertInstanceOf(DumpedRuleInterface::class, $rule); $this->expectException(InvalidArgumentException::class); diff --git a/tests/Rule/NestedTest.php b/tests/Rule/NestedTest.php index 595d27e01..096a56f05 100644 --- a/tests/Rule/NestedTest.php +++ b/tests/Rule/NestedTest.php @@ -39,7 +39,7 @@ use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility; use Yiisoft\Validator\Tests\Support\Data\ObjectWithNestedObject; use Yiisoft\Validator\Tests\Support\Helper\OptionsHelper; -use Yiisoft\Validator\Tests\Support\Rule\StubRule\StubRuleWithOptions; +use Yiisoft\Validator\Tests\Support\Rule\StubRule\StubDumpedRule; use Yiisoft\Validator\Tests\Support\RulesProvider\SimpleRulesProvider; use Yiisoft\Validator\ValidationContext; use Yiisoft\Validator\Validator; @@ -196,8 +196,8 @@ public function dataOptions(): array ], [ new Nested([ - 'author.name' => new StubRuleWithOptions('author-name', ['key' => 'name']), - 'author.age' => new StubRuleWithOptions('author-age', ['key' => 'age']), + 'author.name' => new StubDumpedRule('author-name', ['key' => 'name']), + 'author.age' => new StubDumpedRule('author-age', ['key' => 'age']), ]), [ 'noRulesWithNoObjectMessage' => [ @@ -228,8 +228,8 @@ public function dataOptions(): array [ new Nested([ 'author' => [ - 'name' => new StubRuleWithOptions('author-name', ['key' => 'name']), - 'age' => new StubRuleWithOptions('author-age', ['key' => 'age']), + 'name' => new StubDumpedRule('author-name', ['key' => 'name']), + 'age' => new StubDumpedRule('author-age', ['key' => 'age']), ], ]), [ diff --git a/tests/Support/Rule/StubRule/StubRuleWithOptions.php b/tests/Support/Rule/StubRule/StubDumpedRule.php similarity index 79% rename from tests/Support/Rule/StubRule/StubRuleWithOptions.php rename to tests/Support/Rule/StubRule/StubDumpedRule.php index 3048a9504..0f83317d1 100644 --- a/tests/Support/Rule/StubRule/StubRuleWithOptions.php +++ b/tests/Support/Rule/StubRule/StubDumpedRule.php @@ -4,9 +4,9 @@ namespace Yiisoft\Validator\Tests\Support\Rule\StubRule; -use Yiisoft\Validator\RuleWithOptionsInterface; +use Yiisoft\Validator\DumpedRuleInterface; -final class StubRuleWithOptions implements RuleWithOptionsInterface +final class StubDumpedRule implements DumpedRuleInterface { public function __construct(private string $name, private array $options) { diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index b8b164c5f..5b69a0030 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -42,7 +42,7 @@ use Yiisoft\Validator\Tests\Support\Data\SimpleDto; use Yiisoft\Validator\Tests\Support\Data\SimpleForm; use Yiisoft\Validator\Tests\Support\Rule\NotNullRule\NotNull; -use Yiisoft\Validator\Tests\Support\Rule\StubRule\StubRuleWithOptions; +use Yiisoft\Validator\Tests\Support\Rule\StubRule\StubDumpedRule; use Yiisoft\Validator\ValidationContext; use Yiisoft\Validator\Validator; use Yiisoft\Validator\ValidatorInterface; @@ -1084,7 +1084,7 @@ public function skipOnEmptyDataProvider(): array } /** - * @param StubRuleWithOptions[] $rules + * @param StubDumpedRule[] $rules * @param Error[] $expectedErrors * * @dataProvider skipOnEmptyDataProvider From 7064df5b8a6aae97d4fa63add955d6bacd62261b Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 28 Nov 2023 19:31:39 +0600 Subject: [PATCH 3/7] Update CHANGELOG [skip ci] --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6a2c6fbd..8ff48336c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ - Bug #632: Fix property name usage in error messages of rules in `Nested` rule (@vjik) - Enh #636: Improve psalm annotations in `Result` class (@vjik) - Enh #637: Add German translation (@took) +- Chg #634: Move `getName()` method from `RuleInterface` to `RuleWithOptionsInterface` (@arogachev) +- Chg #634: Rename `RuleWithOptionsInterface` to `DumpedRuleInterface` (@arogachev) +- Chg #634: Use FQCN as a name for built-in rules during export with `RulesDumper` (@arogachev) +- Chg #634: Use FQCN as a name for rules not implementing `DumpedRuleInterface` during export with `RulesDumper` + (@arogachev) ## 1.1.0 April 06, 2023 From 317cfcab6e392a0e77d64cd93068b16952c69df3 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 28 Nov 2023 19:50:56 +0600 Subject: [PATCH 4/7] Fix Psalm and tests --- src/Helper/RulesDumper.php | 4 ++-- tests/Helper/RulesDumperTest.php | 8 +++++++- tests/Rule/CompositeTest.php | 2 +- tests/Rule/EachTest.php | 2 +- tests/Support/Rule/NotNullRule/NotNull.php | 5 ----- tests/Support/Rule/RuleWithoutOptions.php | 5 ----- 6 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Helper/RulesDumper.php b/src/Helper/RulesDumper.php index 2c0e7cf96..28e5460f6 100644 --- a/src/Helper/RulesDumper.php +++ b/src/Helper/RulesDumper.php @@ -98,8 +98,8 @@ private static function fetchOptions(iterable $rules): array $options = self::fetchOptions($rule); } elseif ($rule instanceof DumpedRuleInterface) { $options = array_merge([$rule->getName()], $rule->getOptions()); - } elseif ($rule instanceof RuleInterface) { - $options = [$rule->getName()]; + } elseif ($rule instanceof RuleInterface) { + $options = [$rule::class]; } else { throw new InvalidArgumentException(sprintf( 'Every rule must implement "%s". Type "%s" given.', diff --git a/tests/Helper/RulesDumperTest.php b/tests/Helper/RulesDumperTest.php index 860e96064..9324161c1 100644 --- a/tests/Helper/RulesDumperTest.php +++ b/tests/Helper/RulesDumperTest.php @@ -10,7 +10,9 @@ use Yiisoft\Validator\Rule\Integer; use Yiisoft\Validator\Helper\RulesDumper; use Yiisoft\Validator\Tests\Support\Data\IteratorWithBooleanKey; +use Yiisoft\Validator\Tests\Support\Rule\NotNullRule\NotNull; use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions; +use Yiisoft\Validator\Tests\Support\Rule\StubRule\StubDumpedRule; final class RulesDumperTest extends TestCase { @@ -64,6 +66,10 @@ public function asArrayDataProvider(): array ], ], ], + [ + ['attributeName' => [new RuleWithoutOptions(), new StubDumpedRule('name', [])]], + ['attributeName' => [[RuleWithoutOptions::class], ['name']]], + ], ]; } @@ -124,7 +130,7 @@ public function testRuleWithoutOptions(): void 'skipOnError' => false, ], [ - 'test', + RuleWithoutOptions::class, ], ]; diff --git a/tests/Rule/CompositeTest.php b/tests/Rule/CompositeTest.php index f4d4c4b4a..8cc93d8ec 100644 --- a/tests/Rule/CompositeTest.php +++ b/tests/Rule/CompositeTest.php @@ -135,7 +135,7 @@ public function dataOptions(): array 'pattern' => '/1/', ], [ - 'test', + RuleWithoutOptions::class, ], ], ], diff --git a/tests/Rule/EachTest.php b/tests/Rule/EachTest.php index 247bcc098..d88f3315a 100644 --- a/tests/Rule/EachTest.php +++ b/tests/Rule/EachTest.php @@ -153,7 +153,7 @@ public function dataOptions(): array ], [ [ - 'test', + RuleWithoutOptions::class, ], ], ], diff --git a/tests/Support/Rule/NotNullRule/NotNull.php b/tests/Support/Rule/NotNullRule/NotNull.php index 637f7be30..c7b4c7f5f 100644 --- a/tests/Support/Rule/NotNullRule/NotNull.php +++ b/tests/Support/Rule/NotNullRule/NotNull.php @@ -10,11 +10,6 @@ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class NotNull implements RuleInterface { - public function getName(): string - { - return 'notNull'; - } - public function getHandler(): string { return NotNullHandler::class; diff --git a/tests/Support/Rule/RuleWithoutOptions.php b/tests/Support/Rule/RuleWithoutOptions.php index d6dd59d18..2d5896094 100644 --- a/tests/Support/Rule/RuleWithoutOptions.php +++ b/tests/Support/Rule/RuleWithoutOptions.php @@ -9,11 +9,6 @@ final class RuleWithoutOptions implements RuleInterface { - public function getName(): string - { - return 'test'; - } - public function getHandler(): string { return StubRuleHandler::class; From 93d1c65378a3d03b54c61ca2324503c26acbdec9 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 28 Nov 2023 13:51:10 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI --- tests/Helper/RulesDumperTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Helper/RulesDumperTest.php b/tests/Helper/RulesDumperTest.php index 9324161c1..3b52c8ec8 100644 --- a/tests/Helper/RulesDumperTest.php +++ b/tests/Helper/RulesDumperTest.php @@ -10,7 +10,6 @@ use Yiisoft\Validator\Rule\Integer; use Yiisoft\Validator\Helper\RulesDumper; use Yiisoft\Validator\Tests\Support\Data\IteratorWithBooleanKey; -use Yiisoft\Validator\Tests\Support\Rule\NotNullRule\NotNull; use Yiisoft\Validator\Tests\Support\Rule\RuleWithoutOptions; use Yiisoft\Validator\Tests\Support\Rule\StubRule\StubDumpedRule; From 75e7f0f3dcbf8ff53605fa3ca46be8cda7e4f276 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 28 Nov 2023 19:56:43 +0600 Subject: [PATCH 6/7] More removing --- tests/Support/Rule/CustomUrlRuleSet.php | 5 ----- tests/ValidatorTest.php | 10 ---------- 2 files changed, 15 deletions(-) diff --git a/tests/Support/Rule/CustomUrlRuleSet.php b/tests/Support/Rule/CustomUrlRuleSet.php index 1adc34cc0..232b3360a 100644 --- a/tests/Support/Rule/CustomUrlRuleSet.php +++ b/tests/Support/Rule/CustomUrlRuleSet.php @@ -17,9 +17,4 @@ public function getRules(): iterable yield new Url(enableIdn: true); yield new Length(max: 20); } - - public function getName(): string - { - return 'customUrlRule'; - } } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 5b69a0030..ec11d492a 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -326,11 +326,6 @@ public function __construct(private $ruleHandler) { } - public function getName(): string - { - return 'test'; - } - public function getHandler(): string { return $this->ruleHandler::class; @@ -348,11 +343,6 @@ public function testRuleWithoutHandler(): void $validator->validate(new ArrayDataSet(['property' => '']), [ 'property' => [ new class () implements RuleInterface { - public function getName(): string - { - return 'test'; - } - public function getHandler(): string { return 'NonExistClass'; From 76fb3ff897e06f5f05e97686d3b75855a9e5ebd0 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Thu, 30 Nov 2023 18:02:47 +0600 Subject: [PATCH 7/7] Simplify getName() for abstract classes --- src/Rule/AbstractCompare.php | 5 +++++ src/Rule/AbstractNumber.php | 5 +++++ src/Rule/Compare.php | 4 ---- src/Rule/Equal.php | 5 ----- src/Rule/GreaterThan.php | 5 ----- src/Rule/GreaterThanOrEqual.php | 5 ----- src/Rule/Integer.php | 5 ----- src/Rule/LessThan.php | 5 ----- src/Rule/LessThanOrEqual.php | 5 ----- src/Rule/NotEqual.php | 5 ----- src/Rule/Number.php | 5 ----- 11 files changed, 10 insertions(+), 44 deletions(-) diff --git a/src/Rule/AbstractCompare.php b/src/Rule/AbstractCompare.php index 8aee11010..f956c4073 100644 --- a/src/Rule/AbstractCompare.php +++ b/src/Rule/AbstractCompare.php @@ -169,6 +169,11 @@ public function __construct( } } + public function getName(): string + { + return self::class; + } + /** * Get value to be compared with. * diff --git a/src/Rule/AbstractNumber.php b/src/Rule/AbstractNumber.php index ad9b1319f..ab60a2302 100644 --- a/src/Rule/AbstractNumber.php +++ b/src/Rule/AbstractNumber.php @@ -113,6 +113,11 @@ public function __construct( $this->pattern = $pattern; } + public function getName(): string + { + return self::class; + } + /** * Get lower limit of the number. `null` means no lower limit. * diff --git a/src/Rule/Compare.php b/src/Rule/Compare.php index a7c3e1a4d..4802e7314 100644 --- a/src/Rule/Compare.php +++ b/src/Rule/Compare.php @@ -30,8 +30,4 @@ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class Compare extends AbstractCompare { - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/Equal.php b/src/Rule/Equal.php index 0f009b26f..e4e5038c1 100644 --- a/src/Rule/Equal.php +++ b/src/Rule/Equal.php @@ -108,9 +108,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/GreaterThan.php b/src/Rule/GreaterThan.php index 679490486..cf6886f67 100644 --- a/src/Rule/GreaterThan.php +++ b/src/Rule/GreaterThan.php @@ -105,9 +105,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/GreaterThanOrEqual.php b/src/Rule/GreaterThanOrEqual.php index f59ebacd3..d64235796 100644 --- a/src/Rule/GreaterThanOrEqual.php +++ b/src/Rule/GreaterThanOrEqual.php @@ -106,9 +106,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/Integer.php b/src/Rule/Integer.php index 21aee90fd..a04f5d3a2 100644 --- a/src/Rule/Integer.php +++ b/src/Rule/Integer.php @@ -88,9 +88,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/LessThan.php b/src/Rule/LessThan.php index 1896aa6ad..3c7803e2e 100644 --- a/src/Rule/LessThan.php +++ b/src/Rule/LessThan.php @@ -105,9 +105,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/LessThanOrEqual.php b/src/Rule/LessThanOrEqual.php index 3f3033a64..ebf036bb3 100644 --- a/src/Rule/LessThanOrEqual.php +++ b/src/Rule/LessThanOrEqual.php @@ -105,9 +105,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/NotEqual.php b/src/Rule/NotEqual.php index a6504ef8e..33534cd70 100644 --- a/src/Rule/NotEqual.php +++ b/src/Rule/NotEqual.php @@ -108,9 +108,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } } diff --git a/src/Rule/Number.php b/src/Rule/Number.php index 14e01f758..5a225059f 100644 --- a/src/Rule/Number.php +++ b/src/Rule/Number.php @@ -88,9 +88,4 @@ public function __construct( when: $when, ); } - - public function getName(): string - { - return self::class; - } }