From 18d8246fa5ec209a935cefa149d6293ca001458d Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Fri, 20 Dec 2024 14:34:49 +0100 Subject: [PATCH] Remove duplication and improve naming of adjacent results I identified a pattern among rules that create results with adjacent results, so I created a method that abstracts that. I did have to compromise with the DateTimeDiff, having to escape the input instead of using the name itself, but that seems like a good trade-off. I've also renamed "Subsequent" to "Adjacent" because it sounded better. This is the second time I've renamed this concept, and I hope it will be the last. --- library/Message/StandardRenderer.php | 4 +- library/Result.php | 45 ++++++--- library/Rules/Core/ArrayAggregateFunction.php | 49 ---------- library/Rules/DateTimeDiff.php | 36 ++------ library/Rules/Length.php | 18 +--- library/Rules/Max.php | 11 +-- library/Rules/Min.php | 11 +-- library/Rules/NullOr.php | 4 +- library/Rules/Size.php | 21 +---- library/Rules/UndefOr.php | 4 +- tests/feature/Rules/DateTimeDiffTest.php | 92 +++++++++---------- tests/feature/Rules/NullOrTest.php | 4 +- tests/feature/Rules/UndefOrTest.php | 4 +- .../Transformers/DeprecatedAgeTest.php | 16 ++-- tests/feature/TranslatorTest.php | 2 +- tests/library/Builders/ResultBuilder.php | 8 +- tests/unit/Message/StandardRendererTest.php | 10 +- 17 files changed, 131 insertions(+), 208 deletions(-) delete mode 100644 library/Rules/Core/ArrayAggregateFunction.php diff --git a/library/Message/StandardRenderer.php b/library/Message/StandardRenderer.php index 2d06b4d3c..5bee117b0 100644 --- a/library/Message/StandardRenderer.php +++ b/library/Message/StandardRenderer.php @@ -52,8 +52,8 @@ function (array $matches) use ($parameters, $translator) { $translator->translate($template ?? $this->getTemplateMessage($result)) ); - if (!$result->hasCustomTemplate() && $result->subsequent !== null) { - $rendered .= ' ' . $this->render($result->subsequent, $translator); + if (!$result->hasCustomTemplate() && $result->adjacent !== null) { + $rendered .= ' ' . $this->render($result->adjacent, $translator); } return $rendered; diff --git a/library/Result.php b/library/Result.php index d308dab3c..1241a6e77 100644 --- a/library/Result.php +++ b/library/Result.php @@ -39,7 +39,7 @@ public function __construct( public readonly Mode $mode = Mode::DEFAULT, ?string $name = null, ?string $id = null, - public readonly ?Result $subsequent = null, + public readonly ?Result $adjacent = null, public readonly bool $unchangeableId = false, Result ...$children, ) { @@ -69,6 +69,29 @@ public static function passed( return new self(true, $input, $rule, $parameters, $template); } + /** @param array $parameters */ + public static function fromAdjacent( + mixed $input, + string $prefix, + Rule $rule, + Result $adjacent, + array $parameters = [], + string $template = Rule::TEMPLATE_STANDARD + ): Result { + if ($adjacent->allowsAdjacent()) { + return (new Result($adjacent->isValid, $input, $rule, $parameters, $template, id: $adjacent->id)) + ->withPrefixedId($prefix) + ->withAdjacent($adjacent->withInput($input)); + } + + $childrenAsAdjacent = array_map( + static fn(Result $child) => self::fromAdjacent($input, $prefix, $rule, $child, $parameters, $template), + $adjacent->children + ); + + return $adjacent->withInput($input)->withChildren(...$childrenAsAdjacent); + } + public function withTemplate(string $template): self { return $this->clone(template: $template); @@ -123,16 +146,16 @@ public function withInput(mixed $input): self ); } - public function withSubsequent(Result $subsequent): self + public function withAdjacent(Result $adjacent): self { - return $this->clone(subsequent: $subsequent); + return $this->clone(adjacent: $adjacent); } public function withInvertedValidation(): self { return $this->clone( isValid: !$this->isValid, - subsequent: $this->subsequent?->withInvertedValidation(), + adjacent: $this->adjacent?->withInvertedValidation(), children: array_map(static fn (Result $child) => $child->withInvertedValidation(), $this->children), ); } @@ -142,7 +165,7 @@ public function withInvertedMode(): self return $this->clone( isValid: !$this->isValid, mode: $this->mode == Mode::DEFAULT ? Mode::INVERTED : Mode::DEFAULT, - subsequent: $this->subsequent?->withInvertedMode(), + adjacent: $this->adjacent?->withInvertedMode(), children: array_map(static fn (Result $child) => $child->withInvertedMode(), $this->children), ); } @@ -152,18 +175,18 @@ public function hasCustomTemplate(): bool return preg_match('/__[0-9a-z_]+_/', $this->template) === 0; } - public function allowsSubsequent(): bool + public function allowsAdjacent(): bool { if ($this->children === [] && !$this->hasCustomTemplate()) { return true; } - $childrenThatAllowSubsequent = array_filter( + $childrenThatAllowAdjacent = array_filter( $this->children, - static fn (Result $child) => $child->allowsSubsequent() + static fn (Result $child) => $child->allowsAdjacent() ); - return count($childrenThatAllowSubsequent) === 1; + return count($childrenThatAllowAdjacent) === 1; } /** @@ -176,7 +199,7 @@ private function clone( ?Mode $mode = null, ?string $name = null, ?string $id = null, - ?Result $subsequent = null, + ?Result $adjacent = null, ?bool $unchangeableId = null, ?array $children = null ): self { @@ -189,7 +212,7 @@ private function clone( $mode ?? $this->mode, $name ?? $this->name, $id ?? $this->id, - $subsequent ?? $this->subsequent, + $adjacent ?? $this->adjacent, $unchangeableId ?? $this->unchangeableId, ...($children ?? $this->children) ); diff --git a/library/Rules/Core/ArrayAggregateFunction.php b/library/Rules/Core/ArrayAggregateFunction.php deleted file mode 100644 index 6747390ae..000000000 --- a/library/Rules/Core/ArrayAggregateFunction.php +++ /dev/null @@ -1,49 +0,0 @@ - - * SPDX-License-Identifier: MIT - */ - -declare(strict_types=1); - -namespace Respect\Validation\Rules\Core; - -use Respect\Validation\Result; - -use function array_map; - -abstract class ArrayAggregateFunction extends FilteredNonEmptyArray -{ - protected string $idPrefix; - - /** - * This function should extract the aggregate data from the input array - * - * @param non-empty-array $input - */ - abstract protected function extractAggregate(array $input): mixed; - - /** @param non-empty-array $input */ - protected function evaluateNonEmptyArray(array $input): Result - { - $aggregate = $this->extractAggregate($input); - - return $this->enrichResult($input, $this->rule->evaluate($aggregate)); - } - - private function enrichResult(mixed $input, Result $result): Result - { - if (!$result->allowsSubsequent()) { - return $result - ->withInput($input) - ->withChildren( - ...array_map(fn(Result $child) => $this->enrichResult($input, $child), $result->children) - ); - } - - return (new Result($result->isValid, $input, $this, id: $result->id)) - ->withPrefixedId($this->idPrefix) - ->withSubsequent($result->withInput($input)); - } -} diff --git a/library/Rules/DateTimeDiff.php b/library/Rules/DateTimeDiff.php index 8f92116c0..13ed85d2c 100644 --- a/library/Rules/DateTimeDiff.php +++ b/library/Rules/DateTimeDiff.php @@ -20,7 +20,6 @@ use Respect\Validation\Rules\Core\Standard; use Throwable; -use function array_map; use function in_array; use function ucfirst; @@ -31,8 +30,8 @@ self::TEMPLATE_STANDARD )] #[Template( - 'The number of {{type|trans}} between {{now|raw}} and', - 'The number of {{type|trans}} between {{now|raw}} and', + 'The number of {{type|trans}} between {{now}} and', + 'The number of {{type|trans}} between {{now}} and', self::TEMPLATE_CUSTOMIZED )] #[Template( @@ -82,33 +81,18 @@ public function evaluate(mixed $input): Result ->withId('dateTimeDiff' . ucfirst($this->rule->evaluate($input)->id)); } - return $this->enrichResult( - $this->nowParameter($now), + $nowPlaceholder = $this->nowParameter($now); + + return Result::fromAdjacent( $input, - $this->rule->evaluate($this->comparisonValue($now, $compareTo)) + 'dateTimeDiff', + $this, + $this->rule->evaluate($this->comparisonValue($now, $compareTo)), + ['type' => $this->type, 'now' => $nowPlaceholder], + $nowPlaceholder === 'now' ? self::TEMPLATE_STANDARD : self::TEMPLATE_CUSTOMIZED ); } - private function enrichResult(string $now, mixed $input, Result $result): Result - { - $name = $input instanceof DateTimeInterface ? $input->format('c') : $input; - - if (!$result->allowsSubsequent()) { - return $result - ->withNameIfMissing($name) - ->withChildren( - ...array_map(fn(Result $child) => $this->enrichResult($now, $input, $child), $result->children) - ); - } - - $parameters = ['type' => $this->type, 'now' => $now]; - $template = $now === 'now' ? self::TEMPLATE_STANDARD : self::TEMPLATE_CUSTOMIZED; - - return (new Result($result->isValid, $input, $this, $parameters, $template, id: $result->id)) - ->withPrefixedId('dateTimeDiff') - ->withSubsequent($result->withNameIfMissing($name)); - } - private function comparisonValue(DateTimeInterface $now, DateTimeInterface $compareTo): int|float { return match ($this->type) { diff --git a/library/Rules/Length.php b/library/Rules/Length.php index b6f799bfe..cbc436646 100644 --- a/library/Rules/Length.php +++ b/library/Rules/Length.php @@ -15,7 +15,6 @@ use Respect\Validation\Result; use Respect\Validation\Rules\Core\Wrapper; -use function array_map; use function count; use function is_array; use function is_string; @@ -45,22 +44,7 @@ public function evaluate(mixed $input): Result ->withId('length' . ucfirst($this->rule->evaluate($input)->id)); } - return $this->enrichResult($input, $this->rule->evaluate($length)); - } - - private function enrichResult(mixed $input, Result $result): Result - { - if (!$result->allowsSubsequent()) { - return $result - ->withInput($input) - ->withChildren( - ...array_map(fn(Result $child) => $this->enrichResult($input, $child), $result->children) - ); - } - - return (new Result($result->isValid, $input, $this, id: $result->id)) - ->withPrefixedId('length') - ->withSubsequent($result->withInput($input)); + return Result::fromAdjacent($input, 'length', $this, $this->rule->evaluate($length)); } private function extractLength(mixed $input): ?int diff --git a/library/Rules/Max.php b/library/Rules/Max.php index 2bf888c02..cc09829c6 100644 --- a/library/Rules/Max.php +++ b/library/Rules/Max.php @@ -11,19 +11,18 @@ use Attribute; use Respect\Validation\Message\Template; -use Respect\Validation\Rules\Core\ArrayAggregateFunction; +use Respect\Validation\Result; +use Respect\Validation\Rules\Core\FilteredNonEmptyArray; use function max; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] #[Template('The maximum of', 'The maximum of')] -final class Max extends ArrayAggregateFunction +final class Max extends FilteredNonEmptyArray { - protected string $idPrefix = 'max'; - /** @param non-empty-array $input */ - protected function extractAggregate(array $input): mixed + protected function evaluateNonEmptyArray(array $input): Result { - return max($input); + return Result::fromAdjacent($input, 'max', $this, $this->rule->evaluate(max($input))); } } diff --git a/library/Rules/Min.php b/library/Rules/Min.php index 47d06ea31..7f992d4a3 100644 --- a/library/Rules/Min.php +++ b/library/Rules/Min.php @@ -11,19 +11,18 @@ use Attribute; use Respect\Validation\Message\Template; -use Respect\Validation\Rules\Core\ArrayAggregateFunction; +use Respect\Validation\Result; +use Respect\Validation\Rules\Core\FilteredNonEmptyArray; use function min; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] #[Template('The minimum of', 'The minimum of')] -final class Min extends ArrayAggregateFunction +final class Min extends FilteredNonEmptyArray { - protected string $idPrefix = 'min'; - /** @param non-empty-array $input */ - protected function extractAggregate(array $input): mixed + protected function evaluateNonEmptyArray(array $input): Result { - return min($input); + return Result::fromAdjacent($input, 'min', $this, $this->rule->evaluate(min($input))); } } diff --git a/library/Rules/NullOr.php b/library/Rules/NullOr.php index 91af36c9d..fc165571f 100644 --- a/library/Rules/NullOr.php +++ b/library/Rules/NullOr.php @@ -39,10 +39,10 @@ public function evaluate(mixed $input): Result private function enrichResult(Result $result): Result { - if ($result->allowsSubsequent()) { + if ($result->allowsAdjacent()) { return $result ->withPrefixedId('nullOr') - ->withSubsequent(new Result($result->isValid, $result->input, $this)); + ->withAdjacent(new Result($result->isValid, $result->input, $this)); } return $result->withChildren(...array_map(fn(Result $child) => $this->enrichResult($child), $result->children)); diff --git a/library/Rules/Size.php b/library/Rules/Size.php index 8ddca3058..29d89b044 100644 --- a/library/Rules/Size.php +++ b/library/Rules/Size.php @@ -19,7 +19,6 @@ use Respect\Validation\Rules\Core\Wrapper; use SplFileInfo; -use function array_map; use function filesize; use function is_string; use function ucfirst; @@ -72,8 +71,9 @@ public function evaluate(mixed $input): Result } $result = $this->rule->evaluate($this->getSize($input) / self::DATA_STORAGE_UNITS[$this->unit]['bytes']); + $parameters = ['unit' => self::DATA_STORAGE_UNITS[$this->unit]['name']]; - return $this->enrichResult($input, $result); + return Result::fromAdjacent($input, 'size', $this, $result, $parameters); } private function getSize(mixed $input): ?int @@ -96,21 +96,4 @@ private function getSize(mixed $input): ?int return null; } - - private function enrichResult(mixed $input, Result $result): Result - { - if (!$result->allowsSubsequent()) { - return $result - ->withInput($input) - ->withChildren( - ...array_map(fn(Result $child) => $this->enrichResult($input, $child), $result->children) - ); - } - - $parameters = ['unit' => self::DATA_STORAGE_UNITS[$this->unit]['name']]; - - return (new Result($result->isValid, $input, $this, $parameters, id: $result->id)) - ->withPrefixedId('size') - ->withSubsequent($result->withInput($input)); - } } diff --git a/library/Rules/UndefOr.php b/library/Rules/UndefOr.php index 2938e2b08..bb27bddd1 100644 --- a/library/Rules/UndefOr.php +++ b/library/Rules/UndefOr.php @@ -42,10 +42,10 @@ public function evaluate(mixed $input): Result private function enrichResult(Result $result): Result { - if ($result->allowsSubsequent()) { + if ($result->allowsAdjacent()) { return $result ->withPrefixedId('undefOr') - ->withSubsequent(new Result($result->isValid, $result->input, $this)); + ->withAdjacent(new Result($result->isValid, $result->input, $this)); } return $result->withChildren(...array_map(fn(Result $child) => $this->enrichResult($child), $result->children)); diff --git a/tests/feature/Rules/DateTimeDiffTest.php b/tests/feature/Rules/DateTimeDiffTest.php index 87eba4f1f..d59fe358b 100644 --- a/tests/feature/Rules/DateTimeDiffTest.php +++ b/tests/feature/Rules/DateTimeDiffTest.php @@ -9,51 +9,51 @@ test('With $type = "years"', expectAll( fn() => v::dateTimeDiff('years', v::equals(2))->assert('1 year ago'), - 'The number of years between now and 1 year ago must be equal to 2', - '- The number of years between now and 1 year ago must be equal to 2', - ['dateTimeDiffEquals' => 'The number of years between now and 1 year ago must be equal to 2'] + 'The number of years between now and "1 year ago" must be equal to 2', + '- The number of years between now and "1 year ago" must be equal to 2', + ['dateTimeDiffEquals' => 'The number of years between now and "1 year ago" must be equal to 2'] )); test('With $type = "months"', expectAll( fn() => v::dateTimeDiff('months', v::equals(3))->assert('2 months ago'), - 'The number of months between now and 2 months ago must be equal to 3', - '- The number of months between now and 2 months ago must be equal to 3', - ['dateTimeDiffEquals' => 'The number of months between now and 2 months ago must be equal to 3'] + 'The number of months between now and "2 months ago" must be equal to 3', + '- The number of months between now and "2 months ago" must be equal to 3', + ['dateTimeDiffEquals' => 'The number of months between now and "2 months ago" must be equal to 3'] )); test('With $type = "days"', expectAll( fn() => v::dateTimeDiff('days', v::equals(4))->assert('3 days ago'), - 'The number of days between now and 3 days ago must be equal to 4', - '- The number of days between now and 3 days ago must be equal to 4', - ['dateTimeDiffEquals' => 'The number of days between now and 3 days ago must be equal to 4'] + 'The number of days between now and "3 days ago" must be equal to 4', + '- The number of days between now and "3 days ago" must be equal to 4', + ['dateTimeDiffEquals' => 'The number of days between now and "3 days ago" must be equal to 4'] )); test('With $type = "hours"', expectAll( fn() => v::dateTimeDiff('hours', v::equals(5))->assert('4 hours ago'), - 'The number of hours between now and 4 hours ago must be equal to 5', - '- The number of hours between now and 4 hours ago must be equal to 5', - ['dateTimeDiffEquals' => 'The number of hours between now and 4 hours ago must be equal to 5'] + 'The number of hours between now and "4 hours ago" must be equal to 5', + '- The number of hours between now and "4 hours ago" must be equal to 5', + ['dateTimeDiffEquals' => 'The number of hours between now and "4 hours ago" must be equal to 5'] )); test('With $type = "minutes"', expectAll( fn() => v::dateTimeDiff('minutes', v::equals(6))->assert('5 minutes ago'), - 'The number of minutes between now and 5 minutes ago must be equal to 6', - '- The number of minutes between now and 5 minutes ago must be equal to 6', - ['dateTimeDiffEquals' => 'The number of minutes between now and 5 minutes ago must be equal to 6'] + 'The number of minutes between now and "5 minutes ago" must be equal to 6', + '- The number of minutes between now and "5 minutes ago" must be equal to 6', + ['dateTimeDiffEquals' => 'The number of minutes between now and "5 minutes ago" must be equal to 6'] )); test('With $type = "microseconds"', expectAll( fn() => v::dateTimeDiff('microseconds', v::equals(7))->assert('6 microseconds ago'), - 'The number of microseconds between now and 6 microseconds ago must be equal to 7', - '- The number of microseconds between now and 6 microseconds ago must be equal to 7', - ['dateTimeDiffEquals' => 'The number of microseconds between now and 6 microseconds ago must be equal to 7'] + 'The number of microseconds between now and "6 microseconds ago" must be equal to 7', + '- The number of microseconds between now and "6 microseconds ago" must be equal to 7', + ['dateTimeDiffEquals' => 'The number of microseconds between now and "6 microseconds ago" must be equal to 7'] )); test('With custom $format', expectAllToMatch( fn() => v::dateTimeDiff('years', v::lessThan(8), 'd/m/Y')->assert('09/12/1988'), - 'The number of years between %d/%d/%d and 09/12/1988 must be less than 8', - '- The number of years between %d/%d/%d and 09/12/1988 must be less than 8', - ['dateTimeDiffLessThan' => 'The number of years between %d/%d/%d and 09/12/1988 must be less than 8'] + 'The number of years between "%d/%d/%d" and "09/12/1988" must be less than 8', + '- The number of years between "%d/%d/%d" and "09/12/1988" must be less than 8', + ['dateTimeDiffLessThan' => 'The number of years between "%d/%d/%d" and "09/12/1988" must be less than 8'] )); test('With input in non-parseable date', expectAll( @@ -72,9 +72,9 @@ test('With custom $now', expectAllToMatch( fn() => v::dateTimeDiff('years', v::lessThan(9), null, new DateTimeImmutable())->assert('09/12/1988'), - 'The number of years between %d-%d-%d %d:%d:%d.%d and 09/12/1988 must be less than 9', - '- The number of years between %d-%d-%d %d:%d:%d.%d and 09/12/1988 must be less than 9', - ['dateTimeDiffLessThan' => 'The number of years between %d-%d-%d %d:%d:%d.%d and 09/12/1988 must be less than 9'] + 'The number of years between "%d-%d-%d %d:%d:%d.%d" and "09/12/1988" must be less than 9', + '- The number of years between "%d-%d-%d %d:%d:%d.%d" and "09/12/1988" must be less than 9', + ['dateTimeDiffLessThan' => 'The number of years between "%d-%d-%d %d:%d:%d.%d" and "09/12/1988" must be less than 9'] )); test('With custom template', expectAll( @@ -86,16 +86,16 @@ test('Wrapped by "not"', expectAll( fn() => v::not(v::dateTimeDiff('years', v::lessThan(8)))->assert('7 year ago'), - 'The number of years between now and 7 year ago must not be less than 8', - '- The number of years between now and 7 year ago must not be less than 8', - ['notDateTimeDiffLessThan' => 'The number of years between now and 7 year ago must not be less than 8'] + 'The number of years between now and "7 year ago" must not be less than 8', + '- The number of years between now and "7 year ago" must not be less than 8', + ['notDateTimeDiffLessThan' => 'The number of years between now and "7 year ago" must not be less than 8'] )); test('Wrapping "not"', expectAll( fn() => v::dateTimeDiff('years', v::not(v::lessThan(9)))->assert('8 year ago'), - 'The number of years between now and 8 year ago must not be less than 9', - '- The number of years between now and 8 year ago must not be less than 9', - ['dateTimeDiffNotLessThan' => 'The number of years between now and 8 year ago must not be less than 9'] + 'The number of years between now and "8 year ago" must not be less than 9', + '- The number of years between now and "8 year ago" must not be less than 9', + ['dateTimeDiffNotLessThan' => 'The number of years between now and "8 year ago" must not be less than 9'] )); test('Wrapped with custom template', expectAll( @@ -112,37 +112,37 @@ ['dateTimeDiffEquals' => 'Wrapper with custom template'] )); -test('Without subsequent result', expectAll( +test('Without adjacent result', expectAll( fn() => v::dateTimeDiff('years', v::primeNumber()->between(2, 5))->assert('1 year ago'), - 'The number of years between now and 1 year ago must be a prime number', + 'The number of years between now and "1 year ago" must be a prime number', <<<'FULL_MESSAGE' - - All the required rules must pass for 1 year ago - - The number of years between now and 1 year ago must be a prime number - - The number of years between now and 1 year ago must be between 2 and 5 + - All the required rules must pass for "1 year ago" + - The number of years between now and "1 year ago" must be a prime number + - The number of years between now and "1 year ago" must be between 2 and 5 FULL_MESSAGE, [ - '__root__' => 'All the required rules must pass for 1 year ago', - 'dateTimeDiffPrimeNumber' => 'The number of years between now and 1 year ago must be a prime number', - 'dateTimeDiffBetween' => 'The number of years between now and 1 year ago must be between 2 and 5', + '__root__' => 'All the required rules must pass for "1 year ago"', + 'dateTimeDiffPrimeNumber' => 'The number of years between now and "1 year ago" must be a prime number', + 'dateTimeDiffBetween' => 'The number of years between now and "1 year ago" must be between 2 and 5', ] )); -test('Without subsequent result with templates', expectAll( +test('Without adjacent result with templates', expectAll( fn() => v::dateTimeDiff('years', v::primeNumber()->between(2, 5))->setTemplates([ 'dateTimeDiff' => [ 'primeNumber' => 'Interval must be a valid prime number', 'between' => 'Interval must be between 2 and 5', ], ])->assert('1 year ago'), - 'The number of years between now and 1 year ago must be a prime number', + 'The number of years between now and "1 year ago" must be a prime number', <<<'FULL_MESSAGE' - - All the required rules must pass for 1 year ago - - The number of years between now and 1 year ago must be a prime number - - The number of years between now and 1 year ago must be between 2 and 5 + - All the required rules must pass for "1 year ago" + - The number of years between now and "1 year ago" must be a prime number + - The number of years between now and "1 year ago" must be between 2 and 5 FULL_MESSAGE, [ - '__root__' => 'All the required rules must pass for 1 year ago', - 'dateTimeDiffPrimeNumber' => 'The number of years between now and 1 year ago must be a prime number', - 'dateTimeDiffBetween' => 'The number of years between now and 1 year ago must be between 2 and 5', + '__root__' => 'All the required rules must pass for "1 year ago"', + 'dateTimeDiffPrimeNumber' => 'The number of years between now and "1 year ago" must be a prime number', + 'dateTimeDiffBetween' => 'The number of years between now and "1 year ago" must be between 2 and 5', ] )); diff --git a/tests/feature/Rules/NullOrTest.php b/tests/feature/Rules/NullOrTest.php index 9f86dfa37..73a199a08 100644 --- a/tests/feature/Rules/NullOrTest.php +++ b/tests/feature/Rules/NullOrTest.php @@ -77,7 +77,7 @@ ['notNullOrAlpha' => 'Next to nifty null notations'] )); -test('Without subsequent result', expectAll( +test('Without adjacent result', expectAll( fn() => v::nullOr(v::alpha()->stringType())->assert(1234), '1234 must contain only letters (a-z) or must be null', <<<'FULL_MESSAGE' @@ -92,7 +92,7 @@ ] )); -test('Without subsequent result with templates', expectAll( +test('Without adjacent result with templates', expectAll( fn() => v::nullOr(v::alpha()->stringType())->assert(1234, [ 'nullOrAlpha' => 'Should be nul or alpha', 'nullOrStringType' => 'Should be nul or string type', diff --git a/tests/feature/Rules/UndefOrTest.php b/tests/feature/Rules/UndefOrTest.php index 7c67e4174..890aaeab0 100644 --- a/tests/feature/Rules/UndefOrTest.php +++ b/tests/feature/Rules/UndefOrTest.php @@ -77,7 +77,7 @@ ['notUndefOrAlpha' => 'Should not be undefined or alpha'] )); -test('Without subsequent result', expectAll( +test('Without adjacent result', expectAll( fn() => v::undefOr(v::alpha()->stringType())->assert(1234), '1234 must contain only letters (a-z) or must be undefined', <<<'FULL_MESSAGE' @@ -92,7 +92,7 @@ ] )); -test('Without subsequent result with templates', expectAll( +test('Without adjacent result with templates', expectAll( fn() => v::undefOr(v::alpha()->stringType())->assert(1234, [ 'undefOrAlpha' => 'Should be nul or alpha', 'undefOrStringType' => 'Should be nul or string type', diff --git a/tests/feature/Transformers/DeprecatedAgeTest.php b/tests/feature/Transformers/DeprecatedAgeTest.php index a38662bc8..3341e0c94 100644 --- a/tests/feature/Transformers/DeprecatedAgeTest.php +++ b/tests/feature/Transformers/DeprecatedAgeTest.php @@ -11,48 +11,48 @@ test('Scenario #1', expectMessageAndError( fn() => v::minAge(18)->assert('17 years ago'), - 'The number of years between now and 17 years ago must be greater than or equal to 18', + 'The number of years between now and "17 years ago" must be greater than or equal to 18', 'The minAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #2', expectMessageAndError( fn() => v::not(v::minAge(18))->assert('-30 years'), - 'The number of years between now and -30 years must be less than 18', + 'The number of years between now and "-30 years" must be less than 18', 'The minAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #3', expectMessageAndError( fn() => v::minAge(18)->assert('yesterday'), - 'The number of years between now and yesterday must be greater than or equal to 18', + 'The number of years between now and "yesterday" must be greater than or equal to 18', 'The minAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #4', expectMessageAndError( fn() => v::minAge(18, 'd/m/Y')->assert('12/10/2010'), - 'The number of years between now and 12/10/2010 must be greater than or equal to 18', + 'The number of years between now and "12/10/2010" must be greater than or equal to 18', 'The minAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #5', expectMessageAndError( fn() => v::maxAge(12)->assert('50 years ago'), - 'The number of years between now and 50 years ago must be less than or equal to 12', + 'The number of years between now and "50 years ago" must be less than or equal to 12', 'The maxAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #6', expectMessageAndError( fn() => v::not(v::maxAge(12))->assert('11 years ago'), - 'The number of years between now and 11 years ago must be greater than 12', + 'The number of years between now and "11 years ago" must be greater than 12', 'The maxAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #7', expectMessageAndError( fn() => v::maxAge(12, 'Y-m-d')->assert('1988-09-09'), - 'The number of years between now and 1988-09-09 must be less than or equal to 12', + 'The number of years between now and "1988-09-09" must be less than or equal to 12', 'The maxAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); test('Scenario #8', expectMessageAndError( fn() => v::not(v::maxAge(12, 'Y-m-d'))->assert('2018-01-01'), - 'The number of years between now and 2018-01-01 must be greater than 12', + 'The number of years between now and "2018-01-01" must be greater than 12', 'The maxAge() rule has been deprecated and will be removed in the next major version. Use dateTimeDiff() instead.', )); diff --git a/tests/feature/TranslatorTest.php b/tests/feature/TranslatorTest.php index fe36b58a8..ddd11403c 100644 --- a/tests/feature/TranslatorTest.php +++ b/tests/feature/TranslatorTest.php @@ -43,5 +43,5 @@ function (): void { v::dateTimeDiff('years', v::equals(2))->assert('1972-02-09'); }, - 'O número de anos entre agora e 1972-02-09 deve ser igual a 2', + 'O número de anos entre agora e "1972-02-09" deve ser igual a 2', )); diff --git a/tests/library/Builders/ResultBuilder.php b/tests/library/Builders/ResultBuilder.php index b2f31a5ec..d37a9f0b3 100644 --- a/tests/library/Builders/ResultBuilder.php +++ b/tests/library/Builders/ResultBuilder.php @@ -33,7 +33,7 @@ final class ResultBuilder private Rule $rule; - private ?Result $subsequent = null; + private ?Result $adjacent = null; private bool $unchangeableId = false; @@ -56,7 +56,7 @@ public function build(): Result $this->mode, $this->name, $this->id, - $this->subsequent, + $this->adjacent, $this->unchangeableId, ...$this->children ); @@ -126,9 +126,9 @@ public function mode(Mode $mode): self return $this; } - public function subsequent(Result $build): self + public function adjacent(Result $build): self { - $this->subsequent = $build; + $this->adjacent = $build; return $this; } diff --git a/tests/unit/Message/StandardRendererTest.php b/tests/unit/Message/StandardRendererTest.php index 984df0ce2..ac712999a 100644 --- a/tests/unit/Message/StandardRendererTest.php +++ b/tests/unit/Message/StandardRendererTest.php @@ -340,14 +340,14 @@ public function itShouldRenderResultWithNonCustomTemplateWhenCannotFindAttachedT } #[Test] - public function itShouldRenderResultWithItsSubsequentsWhenItHasNoCustomTemplate(): void + public function itShouldRenderResultWithItsAdjacentsWhenItHasNoCustomTemplate(): void { $renderer = new StandardRenderer(new TestingStringifier()); $result = (new ResultBuilder())->template('__1st__') - ->subsequent( + ->adjacent( (new ResultBuilder())->template('__2nd__') - ->subsequent( + ->adjacent( (new ResultBuilder())->template('__3rd__')->build(), ) ->build(), @@ -360,12 +360,12 @@ public function itShouldRenderResultWithItsSubsequentsWhenItHasNoCustomTemplate( } #[Test] - public function itShouldRenderResultWithoutItsSubsequentsWhenItHasCustomTemplate(): void + public function itShouldRenderResultWithoutItsAdjacentsWhenItHasCustomTemplate(): void { $template = 'Custom template'; $result = (new ResultBuilder())->template($template) - ->subsequent((new ResultBuilder())->template('and this is a subsequent')->build()) + ->adjacent((new ResultBuilder())->template('and this is a adjacent')->build()) ->build(); $renderer = new StandardRenderer(new TestingStringifier());