From 8d989a7bdbe0d4ae2d8ecd397761a7ede302379d Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Tue, 7 Jan 2025 00:34:55 +0100 Subject: [PATCH] Create a few classes the will be present in the next version This commit will deprecate some classes in favor of a couple of news ones. If there are no customisations to `assert()`, `check()`, and other methods, the migration should be pretty easy on users. --- CONTRIBUTING.md | 15 +++++++-------- docs/06-custom-rules.md | 6 +++--- library/Rules/AbstractComposite.php | 2 ++ library/Rules/AbstractEnvelope.php | 2 ++ library/Rules/AbstractRule.php | 2 ++ library/Rules/AbstractWrapper.php | 2 ++ library/Rules/Core/Composite.php | 21 +++++++++++++++++++++ library/Rules/Core/Envelope.php | 16 ++++++++++++++++ library/Rules/Core/Simple.php | 25 +++++++++++++++++++++++++ library/Rules/Core/Wrapper.php | 16 ++++++++++++++++ phpstan.neon.dist | 2 ++ 11 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 library/Rules/Core/Composite.php create mode 100644 library/Rules/Core/Envelope.php create mode 100644 library/Rules/Core/Simple.php create mode 100644 library/Rules/Core/Wrapper.php diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9ec12d8c2..7538a4668 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -44,7 +44,7 @@ create a validator that validates if a string is equal to "Hello World". The rule itself needs to implement the `Validatable` interface but, it is convenient to just extend the `AbstractRule` class. -Doing that, you'll only need to declare one method: `validate($input)`. +Doing that, you'll only need to declare one method: `isValid(mixed $input): bool`. This method must return `true` or `false`. If your validator class is `HelloWorld`, it will be available as `v::helloWorld()` @@ -62,17 +62,16 @@ declare(strict_types=1); namespace Respect\Validation\Rules; +use Respect\Validation\Rules\Core\Simple; + /** * Explain in one sentence what this rule does. * * @author Your Name */ -final class HelloWorld extends AbstractRule +final class HelloWorld extends Simple { - /** - * {@inheritDoc} - */ - public function validate($input): bool + public function isValid(mixed $input): bool { return $input === 'Hello World'; } @@ -90,8 +89,8 @@ are able to use any methods of it. By extending `RuleTestCase` you should implement two methods that should return a [data provider][] with the rule as first item of the arrays: -- `providerForValidInput`: Will test when `validate()` should return `true` -- `providerForInvalidInput`: Will test when `validate()` should return `false` +- `providerForValidInput`: Will test when `isValid()` should return `true` +- `providerForInvalidInput`: Will test when `isValid()` should return `false` ```php * @author Henrique Moody * @author Wojciech Frącz + * + * @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Composite} instead. */ abstract class AbstractComposite extends AbstractRule { diff --git a/library/Rules/AbstractEnvelope.php b/library/Rules/AbstractEnvelope.php index e05644218..2c4613cc9 100644 --- a/library/Rules/AbstractEnvelope.php +++ b/library/Rules/AbstractEnvelope.php @@ -19,6 +19,8 @@ * having an custom message. * * @author Henrique Moody + * + * @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Envelop} instead. */ abstract class AbstractEnvelope extends AbstractRule { diff --git a/library/Rules/AbstractRule.php b/library/Rules/AbstractRule.php index 861b02039..01c6f084f 100644 --- a/library/Rules/AbstractRule.php +++ b/library/Rules/AbstractRule.php @@ -18,6 +18,8 @@ * @author Henrique Moody * @author Nick Lombard * @author Vicente Mendoza + * + * @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Simple} instead. */ abstract class AbstractRule implements Validatable { diff --git a/library/Rules/AbstractWrapper.php b/library/Rules/AbstractWrapper.php index a26bed1bf..9b3d77194 100644 --- a/library/Rules/AbstractWrapper.php +++ b/library/Rules/AbstractWrapper.php @@ -16,6 +16,8 @@ * * @author Alasdair North * @author Henrique Moody + * + * @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Wrapper} instead. */ abstract class AbstractWrapper extends AbstractRule { diff --git a/library/Rules/Core/Composite.php b/library/Rules/Core/Composite.php new file mode 100644 index 000000000..761cdb668 --- /dev/null +++ b/library/Rules/Core/Composite.php @@ -0,0 +1,21 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules\Core; + +use Respect\Validation\Rules\AbstractComposite; +use Respect\Validation\Validatable; + +abstract class Composite extends AbstractComposite +{ + public function __construct(Validatable $rule1, Validatable $rule2, Validatable ...$rules) + { + parent::__construct($rule1, $rule2, ...$rules); + } +} diff --git a/library/Rules/Core/Envelope.php b/library/Rules/Core/Envelope.php new file mode 100644 index 000000000..16585722c --- /dev/null +++ b/library/Rules/Core/Envelope.php @@ -0,0 +1,16 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules\Core; + +use Respect\Validation\Rules\AbstractEnvelope; + +abstract class Envelope extends AbstractEnvelope +{ +} diff --git a/library/Rules/Core/Simple.php b/library/Rules/Core/Simple.php new file mode 100644 index 000000000..7dbed49e0 --- /dev/null +++ b/library/Rules/Core/Simple.php @@ -0,0 +1,25 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules\Core; + +use Respect\Validation\Rules\AbstractRule; + +abstract class Simple extends AbstractRule +{ + abstract public function isValid(mixed $input): bool; + + /** + * @deprecated Calling `validate()` directly from rules is deprecated. Please use {@see Validator::isValid()} instead. + */ + public function validate($input): bool + { + return $this->isValid($input); + } +} diff --git a/library/Rules/Core/Wrapper.php b/library/Rules/Core/Wrapper.php new file mode 100644 index 000000000..d5e9d63a1 --- /dev/null +++ b/library/Rules/Core/Wrapper.php @@ -0,0 +1,16 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules\Core; + +use Respect\Validation\Rules\AbstractWrapper; + +abstract class Wrapper extends AbstractWrapper +{ +} diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 1ca800ece..1456a1e10 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -34,6 +34,8 @@ parameters: # Why: Deprecations of version 3.0 - message: '/Using rule exceptions directly is deprecated, and will be removed in the next major version\./' + - message: '/This class is deprecated, and will be removed in the next major version\./' + - message: '/Calling `.+\(\)` directly from rules is deprecated. Please use {@see \\Respect\\Validation\\Validator::.+\(\)} instead./' path: tests/unit/Rules