diff --git a/library/Message/StandardRenderer.php b/library/Message/StandardRenderer.php index 0dc8e7775..969cc5afc 100644 --- a/library/Message/StandardRenderer.php +++ b/library/Message/StandardRenderer.php @@ -13,7 +13,6 @@ use Respect\Stringifier\Stringifier; use Respect\Validation\Message\Placeholder\Listed; use Respect\Validation\Message\Placeholder\Quoted; -use Respect\Validation\Mode; use Respect\Validation\Result; use Respect\Validation\Rule; @@ -115,7 +114,7 @@ private function getTemplateMessage(Result $result): string continue; } - if ($result->mode == Mode::INVERTED) { + if ($result->hasInvertedMode) { return $template->inverted; } diff --git a/library/Mode.php b/library/Mode.php deleted file mode 100644 index d08e741a1..000000000 --- a/library/Mode.php +++ /dev/null @@ -1,16 +0,0 @@ - - * SPDX-License-Identifier: MIT - */ - -declare(strict_types=1); - -namespace Respect\Validation; - -enum Mode -{ - case DEFAULT; - case INVERTED; -} diff --git a/library/Result.php b/library/Result.php index 1a97219d3..eae2c97d3 100644 --- a/library/Result.php +++ b/library/Result.php @@ -36,7 +36,7 @@ public function __construct( public readonly Rule $rule, public readonly array $parameters = [], public readonly string $template = Rule::TEMPLATE_STANDARD, - public readonly Mode $mode = Mode::DEFAULT, + public readonly bool $hasInvertedMode = false, public readonly ?string $name = null, ?string $id = null, public readonly ?Result $adjacent = null, @@ -199,22 +199,22 @@ public function withAdjacent(Result $adjacent): self return $this->clone(adjacent: $adjacent); } - public function withInvertedValidation(): self + public function withToggledValidation(): self { return $this->clone( isValid: !$this->isValid, - adjacent: $this->adjacent?->withInvertedValidation(), - children: array_map(static fn (Result $child) => $child->withInvertedValidation(), $this->children), + adjacent: $this->adjacent?->withToggledValidation(), + children: array_map(static fn (Result $child) => $child->withToggledValidation(), $this->children), ); } - public function withInvertedMode(): self + public function withToggledModeAndValidation(): self { return $this->clone( isValid: !$this->isValid, - mode: $this->mode == Mode::DEFAULT ? Mode::INVERTED : Mode::DEFAULT, - adjacent: $this->adjacent?->withInvertedMode(), - children: array_map(static fn (Result $child) => $child->withInvertedMode(), $this->children), + mode: !$this->hasInvertedMode, + adjacent: $this->adjacent?->withToggledModeAndValidation(), + children: array_map(static fn (Result $child) => $child->withToggledModeAndValidation(), $this->children), ); } @@ -246,7 +246,7 @@ private function clone( mixed $input = null, ?array $parameters = null, ?string $template = null, - ?Mode $mode = null, + ?bool $mode = null, ?string $name = null, ?string $id = null, ?Result $adjacent = null, @@ -259,7 +259,7 @@ private function clone( $this->rule, $parameters ?? $this->parameters, $template ?? $this->template, - $mode ?? $this->mode, + $mode ?? $this->hasInvertedMode, $name ?? $this->name, $id ?? $this->id, $adjacent ?? $this->adjacent, diff --git a/library/Rules/KeyOptional.php b/library/Rules/KeyOptional.php index 217777b64..bba456e81 100644 --- a/library/Rules/KeyOptional.php +++ b/library/Rules/KeyOptional.php @@ -34,7 +34,7 @@ public function evaluate(mixed $input): Result { $keyExistsResult = (new KeyExists($this->key))->evaluate($input); if (!$keyExistsResult->isValid) { - return $keyExistsResult->withNameFrom($this->rule)->withInvertedMode(); + return $keyExistsResult->withNameFrom($this->rule)->withToggledModeAndValidation(); } return (new Key($this->key, $this->rule))->evaluate($input); diff --git a/library/Rules/NoneOf.php b/library/Rules/NoneOf.php index 81ad3f639..ffd257700 100644 --- a/library/Rules/NoneOf.php +++ b/library/Rules/NoneOf.php @@ -38,7 +38,10 @@ final class NoneOf extends Composite public function evaluate(mixed $input): Result { - $children = array_map(static fn (Rule $rule) => $rule->evaluate($input)->withInvertedMode(), $this->rules); + $children = array_map( + static fn (Rule $rule) => $rule->evaluate($input)->withToggledModeAndValidation(), + $this->rules + ); $valid = array_reduce($children, static fn (bool $carry, Result $result) => $carry && $result->isValid, true); $failed = array_filter($children, static fn (Result $result): bool => !$result->isValid); $template = self::TEMPLATE_SOME; diff --git a/library/Rules/Not.php b/library/Rules/Not.php index fc6cc73bd..2c91c2f32 100644 --- a/library/Rules/Not.php +++ b/library/Rules/Not.php @@ -18,6 +18,6 @@ final class Not extends Wrapper { public function evaluate(mixed $input): Result { - return $this->rule->evaluate($input)->withInvertedMode()->withPrefix('not'); + return $this->rule->evaluate($input)->withToggledModeAndValidation()->withPrefix('not'); } } diff --git a/library/Rules/NullOr.php b/library/Rules/NullOr.php index b34d492fc..552636055 100644 --- a/library/Rules/NullOr.php +++ b/library/Rules/NullOr.php @@ -31,7 +31,7 @@ public function evaluate(mixed $input): Result } if (!$result->isValid) { - return $this->enrichResult($result->withInvertedValidation()); + return $this->enrichResult($result->withToggledValidation()); } return $this->enrichResult($result); diff --git a/library/Rules/OneOf.php b/library/Rules/OneOf.php index 0b558fa39..ff8451afc 100644 --- a/library/Rules/OneOf.php +++ b/library/Rules/OneOf.php @@ -49,7 +49,7 @@ public function evaluate(mixed $input): Result $template = self::TEMPLATE_MORE_THAN_ONE; $children = array_map( - static fn (Result $child) => $child->isValid ? $child->withInvertedValidation() : $child, + static fn (Result $child) => $child->isValid ? $child->withToggledValidation() : $child, $children ); } diff --git a/library/Rules/PropertyOptional.php b/library/Rules/PropertyOptional.php index 2d33043c5..41be5024c 100644 --- a/library/Rules/PropertyOptional.php +++ b/library/Rules/PropertyOptional.php @@ -28,7 +28,7 @@ public function evaluate(mixed $input): Result { $propertyExistsResult = (new PropertyExists($this->propertyName))->evaluate($input); if (!$propertyExistsResult->isValid) { - return $propertyExistsResult->withNameFrom($this->rule)->withInvertedMode(); + return $propertyExistsResult->withNameFrom($this->rule)->withToggledModeAndValidation(); } return (new Property($this->propertyName, $this->rule))->evaluate($input); diff --git a/library/Rules/UndefOr.php b/library/Rules/UndefOr.php index b108b1dd0..39e5a12eb 100644 --- a/library/Rules/UndefOr.php +++ b/library/Rules/UndefOr.php @@ -34,7 +34,7 @@ public function evaluate(mixed $input): Result } if (!$result->isValid) { - return $this->enrichResult($result->withInvertedValidation()); + return $this->enrichResult($result->withToggledValidation()); } return $this->enrichResult($result); diff --git a/tests/library/Builders/ResultBuilder.php b/tests/library/Builders/ResultBuilder.php index 21ab87075..b83999a10 100644 --- a/tests/library/Builders/ResultBuilder.php +++ b/tests/library/Builders/ResultBuilder.php @@ -9,7 +9,6 @@ namespace Respect\Validation\Test\Builders; -use Respect\Validation\Mode; use Respect\Validation\Result; use Respect\Validation\Rule; use Respect\Validation\Test\Rules\Stub; @@ -20,7 +19,7 @@ final class ResultBuilder private mixed $input = 'input'; - private Mode $mode = Mode::DEFAULT; + private bool $hasInvertedMode = false; private string $template = Rule::TEMPLATE_STANDARD; @@ -51,7 +50,7 @@ public function build(): Result $this->rule, $this->parameters, $this->template, - $this->mode, + $this->hasInvertedMode, $this->name, $this->id, $this->adjacent, @@ -117,9 +116,9 @@ public function children(Result ...$children): self return $this; } - public function mode(Mode $mode): self + public function hasInvertedMode(): self { - $this->mode = $mode; + $this->hasInvertedMode = true; return $this; } diff --git a/tests/unit/Message/StandardRendererTest.php b/tests/unit/Message/StandardRendererTest.php index ac712999a..41eccd048 100644 --- a/tests/unit/Message/StandardRendererTest.php +++ b/tests/unit/Message/StandardRendererTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\Attributes\Test; use Respect\Validation\Message\Translator\ArrayTranslator; use Respect\Validation\Message\Translator\DummyTranslator; -use Respect\Validation\Mode; use Respect\Validation\Test\Builders\ResultBuilder; use Respect\Validation\Test\Message\TestingStringifier; use Respect\Validation\Test\TestCase; @@ -318,7 +317,7 @@ public function itShouldRenderResultWithNonCustomTemplateAndInvertedMode(): void $stringifier = new TestingStringifier(); $renderer = new StandardRenderer($stringifier); - $result = (new ResultBuilder())->mode(Mode::INVERTED)->build(); + $result = (new ResultBuilder())->hasInvertedMode()->build(); self::assertSame( sprintf('%s must not be a valid stub', $stringifier->stringify($result->input, 0)), @@ -331,7 +330,7 @@ public function itShouldRenderResultWithNonCustomTemplateWhenCannotFindAttachedT { $renderer = new StandardRenderer(new TestingStringifier()); - $result = (new ResultBuilder())->template('__not_standard__')->mode(Mode::INVERTED)->build(); + $result = (new ResultBuilder())->template('__not_standard__')->hasInvertedMode()->build(); self::assertSame( $result->template, diff --git a/tests/unit/Rules/NotTest.php b/tests/unit/Rules/NotTest.php index 85339f171..f6db1ff64 100644 --- a/tests/unit/Rules/NotTest.php +++ b/tests/unit/Rules/NotTest.php @@ -28,7 +28,7 @@ public function shouldInvertTheResultOfWrappedRule(): void self::assertEquals( $rule->evaluate('input'), - $wrapped->evaluate('input')->withPrefix('not')->withInvertedMode() + $wrapped->evaluate('input')->withPrefix('not')->withToggledModeAndValidation() ); }