From 5c422b33c6e4fdcde83f16b488a4584893162b43 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 | 9 ++++----- 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 | 4 ++-- 11 files changed, 95 insertions(+), 10 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..a3b27141a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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'; } diff --git a/docs/06-custom-rules.md b/docs/06-custom-rules.md index bc0cbe507..696df9f9a 100644 --- a/docs/06-custom-rules.md +++ b/docs/06-custom-rules.md @@ -10,11 +10,11 @@ validate method will be executed. Here's how the class should look: ```php namespace My\Validation\Rules; -use Respect\Validation\Rules\AbstractRule; +use Respect\Validation\Rules\Core\Simple; -final class Something extends AbstractRule +final class Something extends Simple { - public function validate($input): bool + public function isValid(mixed $input): bool { // Do something here with the $input and return a boolean value } diff --git a/library/Rules/AbstractComposite.php b/library/Rules/AbstractComposite.php index 1eca00f7b..b0e14a684 100644 --- a/library/Rules/AbstractComposite.php +++ b/library/Rules/AbstractComposite.php @@ -22,6 +22,8 @@ * @author Alexandre Gomes Gaigalas * @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 1cdd365ff..f6a0b11d9 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 9784c26e7..d2668bdb8 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 bc89af295..ac67ea9ca 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 423ac8068..2e2ccba6b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -32,8 +32,8 @@ parameters: path: library/Rules/AbstractAge.php # Why: Deprecations of version 3.0 - - - message: '/Using rule exceptions directly is deprecated, and will be removed in the next major version\./' + - 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 Validator::.+\(\)} instead./'