diff --git a/docs/migrating-from-v2-to-v3.md b/docs/migrating-from-v2-to-v3.md index bb98869d..e23ab03d 100644 --- a/docs/migrating-from-v2-to-v3.md +++ b/docs/migrating-from-v2-to-v3.md @@ -556,7 +556,6 @@ final class Custom implements Validator Base classes available in `Respect\Validation\Validators\Core`: - `Simple` - For validators with simple boolean logic -- `Wrapper` - For validators that wrap another validator - `Composite` - For validators that combine multiple validators - `Envelope` - For validators that modify how another validator works diff --git a/src/ValidatorBuilder.php b/src/ValidatorBuilder.php index 03eca0e5..85b3a786 100644 --- a/src/ValidatorBuilder.php +++ b/src/ValidatorBuilder.php @@ -26,7 +26,7 @@ use function is_callable; use function is_string; /** @mixin Builder */ -final readonly class ValidatorBuilder implements Validator, Nameable +final readonly class ValidatorBuilder implements Nameable { /** @var array */ private array $validators; diff --git a/src/Validators/Core/FilteredArray.php b/src/Validators/Core/FilteredArray.php index c55ddbcd..4307dcf4 100644 --- a/src/Validators/Core/FilteredArray.php +++ b/src/Validators/Core/FilteredArray.php @@ -12,13 +12,19 @@ declare(strict_types=1); namespace Respect\Validation\Validators\Core; use Respect\Validation\Result; +use Respect\Validation\Validator; use Respect\Validation\Validators\IterableType; use function is_array; use function iterator_to_array; -abstract class FilteredArray extends Wrapper +abstract class FilteredArray implements Validator { + public function __construct( + protected Validator $validator, + ) { + } + public function evaluate(mixed $input): Result { $iterableResult = (new IterableType())->evaluate($input); diff --git a/src/Validators/Core/Nameable.php b/src/Validators/Core/Nameable.php index 878fe4be..90420b2c 100644 --- a/src/Validators/Core/Nameable.php +++ b/src/Validators/Core/Nameable.php @@ -15,8 +15,9 @@ declare(strict_types=1); namespace Respect\Validation\Validators\Core; use Respect\Validation\Name; +use Respect\Validation\Validator; -interface Nameable +interface Nameable extends Validator { public function getName(): Name|null; } diff --git a/src/Validators/Core/Reducer.php b/src/Validators/Core/Reducer.php index da372212..f61297d4 100644 --- a/src/Validators/Core/Reducer.php +++ b/src/Validators/Core/Reducer.php @@ -10,13 +10,21 @@ declare(strict_types=1); namespace Respect\Validation\Validators\Core; +use Respect\Validation\Result; use Respect\Validation\Validator; use Respect\Validation\Validators\AllOf; -final class Reducer extends Wrapper +final readonly class Reducer implements Validator { + private Validator $validator; + public function __construct(Validator $validator1, Validator ...$validators) { - parent::__construct($validators === [] ? $validator1 : new AllOf($validator1, ...$validators)); + $this->validator = $validators === [] ? $validator1 : new AllOf($validator1, ...$validators); + } + + public function evaluate(mixed $input): Result + { + return $this->validator->evaluate($input); } } diff --git a/src/Validators/Core/Wrapper.php b/src/Validators/Core/Wrapper.php deleted file mode 100644 index 0af44fb8..00000000 --- a/src/Validators/Core/Wrapper.php +++ /dev/null @@ -1,32 +0,0 @@ - - */ - -declare(strict_types=1); - -namespace Respect\Validation\Validators\Core; - -use Respect\Validation\Result; -use Respect\Validation\Validator; - -abstract class Wrapper implements Validator -{ - public function __construct( - protected readonly Validator $validator, - ) { - } - - public function evaluate(mixed $input): Result - { - return $this->validator->evaluate($input); - } - - public function getValidator(): Validator - { - return $this->validator; - } -} diff --git a/src/Validators/Key.php b/src/Validators/Key.php index 659c646b..c52f40b6 100644 --- a/src/Validators/Key.php +++ b/src/Validators/Key.php @@ -20,16 +20,14 @@ use Respect\Validation\Path; use Respect\Validation\Result; use Respect\Validation\Validator; use Respect\Validation\Validators\Core\KeyRelated; -use Respect\Validation\Validators\Core\Wrapper; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Key extends Wrapper implements KeyRelated +final readonly class Key implements KeyRelated { public function __construct( - private readonly int|string $key, - Validator $validator, + private int|string $key, + private Validator $validator, ) { - parent::__construct($validator); } public function getKey(): int|string diff --git a/src/Validators/KeyOptional.php b/src/Validators/KeyOptional.php index 099a102a..81a7a7a9 100644 --- a/src/Validators/KeyOptional.php +++ b/src/Validators/KeyOptional.php @@ -15,16 +15,14 @@ use Attribute; use Respect\Validation\Result; use Respect\Validation\Validator; use Respect\Validation\Validators\Core\KeyRelated; -use Respect\Validation\Validators\Core\Wrapper; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class KeyOptional extends Wrapper implements KeyRelated +final readonly class KeyOptional implements KeyRelated { public function __construct( - private readonly int|string $key, - Validator $validator, + private int|string $key, + private Validator $validator, ) { - parent::__construct($validator); } public function getKey(): int|string diff --git a/src/Validators/Length.php b/src/Validators/Length.php index 92bb6e91..e107dd2b 100644 --- a/src/Validators/Length.php +++ b/src/Validators/Length.php @@ -22,7 +22,7 @@ use Attribute; use Countable as PhpCountable; use Respect\Validation\Message\Template; use Respect\Validation\Result; -use Respect\Validation\Validators\Core\Wrapper; +use Respect\Validation\Validator; use function count; use function is_array; @@ -40,10 +40,15 @@ use function mb_strlen; '{{subject}} must not be a countable value or a string', self::TEMPLATE_WRONG_TYPE, )] -final class Length extends Wrapper +final readonly class Length implements Validator { public const string TEMPLATE_WRONG_TYPE = '__wrong_type__'; + public function __construct( + private Validator $validator, + ) { + } + public function evaluate(mixed $input): Result { $length = $this->extractLength($input); diff --git a/src/Validators/Named.php b/src/Validators/Named.php index d883b06d..8e4c21e1 100644 --- a/src/Validators/Named.php +++ b/src/Validators/Named.php @@ -15,19 +15,18 @@ use Respect\Validation\Name; use Respect\Validation\Result; use Respect\Validation\Validator; use Respect\Validation\Validators\Core\Nameable; -use Respect\Validation\Validators\Core\Wrapper; use function is_string; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] -final class Named extends Wrapper implements Nameable +final readonly class Named implements Nameable { - private readonly Name $name; - - public function __construct(string|Name $name, Validator $validator) - { - parent::__construct($validator); + private Name $name; + public function __construct( + string|Name $name, + private Validator $validator, + ) { $this->name = is_string($name) ? new Name($name) : $name; } diff --git a/src/Validators/Not.php b/src/Validators/Not.php index 01302344..0e4f6ccc 100644 --- a/src/Validators/Not.php +++ b/src/Validators/Not.php @@ -19,11 +19,16 @@ namespace Respect\Validation\Validators; use Attribute; use Respect\Validation\Result; -use Respect\Validation\Validators\Core\Wrapper; +use Respect\Validation\Validator; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] -final class Not extends Wrapper +final readonly class Not implements Validator { + public function __construct( + private Validator $validator, + ) { + } + public function evaluate(mixed $input): Result { $result = $this->validator->evaluate($input); diff --git a/src/Validators/NullOr.php b/src/Validators/NullOr.php index 18fd7946..d977a94e 100644 --- a/src/Validators/NullOr.php +++ b/src/Validators/NullOr.php @@ -16,7 +16,7 @@ namespace Respect\Validation\Validators; use Attribute; use Respect\Validation\Message\Template; use Respect\Validation\Result; -use Respect\Validation\Validators\Core\Wrapper; +use Respect\Validation\Validator; use function array_map; @@ -25,8 +25,13 @@ use function array_map; 'or must be null', 'and must not be null', )] -final class NullOr extends Wrapper +final readonly class NullOr implements Validator { + public function __construct( + private Validator $validator, + ) { + } + public function evaluate(mixed $input): Result { $result = $this->validator->evaluate($input); diff --git a/src/Validators/Property.php b/src/Validators/Property.php index 04c61adf..68ff3645 100644 --- a/src/Validators/Property.php +++ b/src/Validators/Property.php @@ -22,16 +22,14 @@ use ReflectionObject; use Respect\Validation\Path; use Respect\Validation\Result; use Respect\Validation\Validator; -use Respect\Validation\Validators\Core\Wrapper; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class Property extends Wrapper +final readonly class Property implements Validator { public function __construct( - private readonly string $propertyName, - Validator $validator, + private string $propertyName, + private Validator $validator, ) { - parent::__construct($validator); } public function evaluate(mixed $input): Result diff --git a/src/Validators/PropertyOptional.php b/src/Validators/PropertyOptional.php index 7ab13683..65d58c99 100644 --- a/src/Validators/PropertyOptional.php +++ b/src/Validators/PropertyOptional.php @@ -14,16 +14,14 @@ namespace Respect\Validation\Validators; use Attribute; use Respect\Validation\Result; use Respect\Validation\Validator; -use Respect\Validation\Validators\Core\Wrapper; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] -final class PropertyOptional extends Wrapper +final readonly class PropertyOptional implements Validator { public function __construct( - private readonly string $propertyName, - Validator $validator, + private string $propertyName, + private Validator $validator, ) { - parent::__construct($validator); } public function evaluate(mixed $input): Result diff --git a/src/Validators/Size.php b/src/Validators/Size.php index 2b654233..3da0ae89 100644 --- a/src/Validators/Size.php +++ b/src/Validators/Size.php @@ -20,7 +20,6 @@ use Respect\Validation\Exceptions\InvalidValidatorException; use Respect\Validation\Message\Template; use Respect\Validation\Result; use Respect\Validation\Validator; -use Respect\Validation\Validators\Core\Wrapper; use SplFileInfo; use function filesize; @@ -37,7 +36,7 @@ use function is_string; '{{subject}} must not be a filename or an instance of SplFileInfo or a PSR-7 interface', self::TEMPLATE_WRONG_TYPE, )] -final class Size extends Wrapper +final readonly class Size implements Validator { public const string TEMPLATE_WRONG_TYPE = '__wrong_type__'; @@ -55,14 +54,12 @@ final class Size extends Wrapper /** @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit */ public function __construct( - private readonly string $unit, - Validator $validator, + private string $unit, + private Validator $validator, ) { if (!isset(self::DATA_STORAGE_UNITS[$unit])) { throw new InvalidValidatorException('"%s" is not a recognized data storage unit.', $unit); } - - parent::__construct($validator); } public function evaluate(mixed $input): Result diff --git a/src/Validators/Templated.php b/src/Validators/Templated.php index 1bbebd23..18e49c93 100644 --- a/src/Validators/Templated.php +++ b/src/Validators/Templated.php @@ -13,18 +13,16 @@ namespace Respect\Validation\Validators; use Attribute; use Respect\Validation\Result; use Respect\Validation\Validator; -use Respect\Validation\Validators\Core\Wrapper; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] -final class Templated extends Wrapper +final readonly class Templated implements Validator { /** @param array $parameters */ public function __construct( - private readonly string $template, - Validator $validator, - private readonly array $parameters = [], + private string $template, + private Validator $validator, + private array $parameters = [], ) { - parent::__construct($validator); } public function evaluate(mixed $input): Result diff --git a/src/Validators/UndefOr.php b/src/Validators/UndefOr.php index 3ddf82ca..48108dc7 100644 --- a/src/Validators/UndefOr.php +++ b/src/Validators/UndefOr.php @@ -16,7 +16,7 @@ use Attribute; use Respect\Validation\Helpers\CanValidateUndefined; use Respect\Validation\Message\Template; use Respect\Validation\Result; -use Respect\Validation\Validators\Core\Wrapper; +use Respect\Validation\Validator; use function array_map; @@ -25,10 +25,15 @@ use function array_map; 'or must be undefined', 'and must not be undefined', )] -final class UndefOr extends Wrapper +final readonly class UndefOr implements Validator { use CanValidateUndefined; + public function __construct( + private Validator $validator, + ) { + } + public function evaluate(mixed $input): Result { $result = $this->validator->evaluate($input); diff --git a/tests/src/Validators/Core/ConcreteWrapper.php b/tests/src/Validators/Core/ConcreteWrapper.php deleted file mode 100644 index 8b3b7515..00000000 --- a/tests/src/Validators/Core/ConcreteWrapper.php +++ /dev/null @@ -1,21 +0,0 @@ - - * SPDX-FileContributor: Andy Wendt - * SPDX-FileContributor: Graham Campbell - * SPDX-FileContributor: Henrique Moody - * SPDX-FileContributor: Nick Lombard - */ - -declare(strict_types=1); - -namespace Respect\Validation\Test\Validators\Core; - -use Respect\Validation\Validators\Core\Wrapper; - -final class ConcreteWrapper extends Wrapper -{ -} diff --git a/tests/unit/Validators/Core/WrapperTest.php b/tests/unit/Validators/Core/WrapperTest.php deleted file mode 100644 index b11f1e26..00000000 --- a/tests/unit/Validators/Core/WrapperTest.php +++ /dev/null @@ -1,38 +0,0 @@ - - * SPDX-FileContributor: Gabriel Caruso - * SPDX-FileContributor: Henrique Moody - * SPDX-FileContributor: Royall Spence - */ - -declare(strict_types=1); - -namespace Respect\Validation\Validators\Core; - -use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\Group; -use PHPUnit\Framework\Attributes\Test; -use Respect\Validation\Test\TestCase; -use Respect\Validation\Test\Validators\Core\ConcreteWrapper; -use Respect\Validation\Test\Validators\Stub; - -#[Group('core')] -#[CoversClass(Wrapper::class)] -final class WrapperTest extends TestCase -{ - #[Test] - public function shouldUseWrappedToEvaluate(): void - { - $wrapped = Stub::pass(2); - - $wrapper = new ConcreteWrapper($wrapped); - - $input = 'Whatever'; - - self::assertEquals($wrapped->evaluate($input), $wrapper->evaluate($input)); - } -}