Skip to content

Commit

Permalink
Replace "LazyConsecutive" with "Consecutive"
Browse files Browse the repository at this point in the history
With this and the Lazy rule, the LazyConsecutive lost its purpose.

While working on it, I did refactor the Domain rule a bit, but mainly to
check how this rule could behave.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Mar 6, 2024
1 parent 78715fb commit 2610a38
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 287 deletions.
8 changes: 4 additions & 4 deletions docs/08-list-of-rules-by-category.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
- [CallableType](rules/CallableType.md)
- [Callback](rules/Callback.md)
- [Lazy](rules/Lazy.md)
- [LazyConsecutive](rules/LazyConsecutive.md)

## Comparisons

Expand All @@ -60,12 +59,13 @@

- [AllOf](rules/AllOf.md)
- [AnyOf](rules/AnyOf.md)
- [LazyConsecutive](rules/LazyConsecutive.md)
- [Consecutive](rules/Consecutive.md)
- [NoneOf](rules/NoneOf.md)
- [OneOf](rules/OneOf.md)

## Conditions

- [Consecutive](rules/Consecutive.md)
- [Not](rules/Not.md)
- [When](rules/When.md)

Expand Down Expand Up @@ -161,11 +161,11 @@
- [AllOf](rules/AllOf.md)
- [AnyOf](rules/AnyOf.md)
- [Call](rules/Call.md)
- [Consecutive](rules/Consecutive.md)
- [Each](rules/Each.md)
- [Key](rules/Key.md)
- [KeySet](rules/KeySet.md)
- [Lazy](rules/Lazy.md)
- [LazyConsecutive](rules/LazyConsecutive.md)
- [NoneOf](rules/NoneOf.md)
- [Not](rules/Not.md)
- [Nullable](rules/Nullable.md)
Expand Down Expand Up @@ -306,6 +306,7 @@
- [Charset](rules/Charset.md)
- [Cnh](rules/Cnh.md)
- [Cnpj](rules/Cnpj.md)
- [Consecutive](rules/Consecutive.md)
- [Consonant](rules/Consonant.md)
- [Contains](rules/Contains.md)
- [ContainsAny](rules/ContainsAny.md)
Expand Down Expand Up @@ -362,7 +363,6 @@
- [KeySet](rules/KeySet.md)
- [LanguageCode](rules/LanguageCode.md)
- [Lazy](rules/Lazy.md)
- [LazyConsecutive](rules/LazyConsecutive.md)
- [LeapDate](rules/LeapDate.md)
- [LeapYear](rules/LeapYear.md)
- [Length](rules/Length.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/AllOf.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Version | Description
See also:

- [AnyOf](AnyOf.md)
- [LazyConsecutive](LazyConsecutive.md)
- [Consecutive](Consecutive.md)
- [NoneOf](NoneOf.md)
- [OneOf](OneOf.md)
- [When](When.md)
2 changes: 1 addition & 1 deletion docs/rules/AnyOf.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Version | Description
See also:

- [AllOf](AllOf.md)
- [Consecutive](Consecutive.md)
- [ContainsAny](ContainsAny.md)
- [LazyConsecutive](LazyConsecutive.md)
- [NoneOf](NoneOf.md)
- [OneOf](OneOf.md)
- [When](When.md)
1 change: 0 additions & 1 deletion docs/rules/Call.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ See also:
- [Callback](Callback.md)
- [Each](Each.md)
- [Lazy](Lazy.md)
- [LazyConsecutive](LazyConsecutive.md)
- [Sorted](Sorted.md)
44 changes: 44 additions & 0 deletions docs/rules/Consecutive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Consecutive

- `Consecutive(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`

Validates the input against a series of rules until one fails.

This rule can be handy for getting the least error messages possible from a chain.

This rule can be helpful in combinations with [Lazy](Lazy.md). An excellent example is when you want to validate a
country code and a subdivision code.

```php
v::consecutive(
v::key('countryCode', v::countryCode()),
v::lazy(static fn($input) => v::key('subdivisionCode', v::subdivisionCode($input['countryCode']))),
)->validate($_POST);
```

You need a valid country code to create a [SubdivisionCode](SubdivisionCode.md), so it makes sense only to validate the
subdivision code only if the country code is valid. In this case, you could also have used [When](When.md), but you
would then have to write `v::key('countryCode', v::countryCode())` twice in your chain.

## Categorization

- Composite
- Conditions
- Nesting

## Changelog

| Version | Description |
|--------:|-------------|
| 3.0.0 | Created |

***
See also:

- [AllOf](AllOf.md)
- [AnyOf](AnyOf.md)
- [Lazy](Lazy.md)
- [NoneOf](NoneOf.md)
- [OneOf](OneOf.md)
- [SubdivisionCode](SubdivisionCode.md)
- [When](When.md)
1 change: 0 additions & 1 deletion docs/rules/Equals.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ See also:
- [Contains](Contains.md)
- [Equivalent](Equivalent.md)
- [Identical](Identical.md)
- [LazyConsecutive](LazyConsecutive.md)
- [Version](Version.md)
1 change: 1 addition & 0 deletions docs/rules/Lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ See also:

- [Call](Call.md)
- [CallableType](CallableType.md)
- [Consecutive](Consecutive.md)
65 changes: 0 additions & 65 deletions docs/rules/LazyConsecutive.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/rules/NoneOf.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ See also:

- [AllOf](AllOf.md)
- [AnyOf](AnyOf.md)
- [LazyConsecutive](LazyConsecutive.md)
- [Consecutive](Consecutive.md)
- [Not](Not.md)
- [OneOf](OneOf.md)
- [When](When.md)
2 changes: 1 addition & 1 deletion docs/rules/OneOf.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ See also:

- [AllOf](AllOf.md)
- [AnyOf](AnyOf.md)
- [LazyConsecutive](LazyConsecutive.md)
- [Consecutive](Consecutive.md)
- [NoneOf](NoneOf.md)
- [When](When.md)
2 changes: 1 addition & 1 deletion docs/rules/SubdivisionCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Version | Description
***
See also:

- [Consecutive](Consecutive.md)
- [CountryCode](CountryCode.md)
- [CurrencyCode](CurrencyCode.md)
- [LazyConsecutive](LazyConsecutive.md)
- [Nip](Nip.md)
- [Pesel](Pesel.md)
- [PolishIdCard](PolishIdCard.md)
Expand Down
1 change: 1 addition & 0 deletions docs/rules/When.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ See also:
- [AllOf](AllOf.md)
- [AlwaysInvalid](AlwaysInvalid.md)
- [AnyOf](AnyOf.md)
- [Consecutive](Consecutive.md)
- [NoneOf](NoneOf.md)
- [OneOf](OneOf.md)
4 changes: 2 additions & 2 deletions library/Mixins/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function cnh(): ChainedValidator;

public function cnpj(): ChainedValidator;

public function consecutive(Validatable $rule1, Validatable $rule2, Validatable ...$rule): ChainedValidator;

public function control(string ...$additionalChars): ChainedValidator;

public function consonant(string ...$additionalChars): ChainedValidator;
Expand Down Expand Up @@ -175,8 +177,6 @@ public function keySet(Validatable $rule, Validatable ...$rules): ChainedValidat
/** @param callable(mixed): Validatable $ruleCreator */
public function lazy(callable $ruleCreator): ChainedValidator;

public function lazyConsecutive(callable $ruleCreator, callable ...$ruleCreators): ChainedValidator;

/** @param "alpha-2"|"alpha-3" $set */
public function languageCode(string $set = 'alpha-2'): ChainedValidator;

Expand Down
4 changes: 2 additions & 2 deletions library/Mixins/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static function cnh(): ChainedValidator;

public static function cnpj(): ChainedValidator;

public static function consecutive(Validatable $rule1, Validatable $rule2, Validatable ...$rule): ChainedValidator;

public static function control(string ...$additionalChars): ChainedValidator;

public static function consonant(string ...$additionalChars): ChainedValidator;
Expand Down Expand Up @@ -177,8 +179,6 @@ public static function keySet(Validatable $rule, Validatable ...$rules): Chained
/** @param callable(mixed): Validatable $ruleCreator */
public static function lazy(callable $ruleCreator): ChainedValidator;

public static function lazyConsecutive(callable $ruleCreator, callable ...$ruleCreators): ChainedValidator;

/** @param "alpha-2"|"alpha-3" $set */
public static function languageCode(string $set = 'alpha-2'): ChainedValidator;

Expand Down
31 changes: 31 additions & 0 deletions library/Rules/Consecutive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Rules;

use Respect\Validation\Helpers\CanBindEvaluateRule;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Composite;

final class Consecutive extends Composite
{
use CanBindEvaluateRule;

public function evaluate(mixed $input): Result
{
foreach ($this->rules as $rule) {
$result = $this->bindEvaluate($rule, $this, $input);
if (!$result->isValid) {
return $result;
}
}

return $result;
}
}
35 changes: 13 additions & 22 deletions library/Rules/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Respect\Validation\Validatable;

use function array_filter;
use function array_map;
use function array_merge;
use function array_pop;
use function count;
Expand All @@ -32,7 +31,7 @@
)]
final class Domain extends AbstractRule
{
private readonly AllOf $genericRule;
private readonly Consecutive $genericRule;

private readonly Validatable $tldRule;

Expand Down Expand Up @@ -66,33 +65,25 @@ public function assert(mixed $input): void

public function evaluate(mixed $input): Result
{
$failedGenericResults = array_filter(array_map(
static fn (Validatable $rule) => $rule->evaluate($input),
$this->genericRule->getRules()
), static fn (Result $result) => !$result->isValid);

if (count($failedGenericResults)) {
return (new Result(false, $input, $this))->withChildren(...$failedGenericResults);
$genericResult = $this->genericRule->evaluate($input);
if (!$genericResult->isValid) {
return (new Result(false, $input, $this))->withChildren($genericResult);
}

$children = [];
$valid = true;
$parts = explode('.', (string) $input);
if (count($parts) >= 2) {
$tld = array_pop($parts);
foreach ($this->tldRule instanceof AllOf ? $this->tldRule->getRules() : [$this->tldRule] as $rule) {
$childResult = $rule->evaluate($tld);
$valid = $valid && $childResult->isValid;
$children[] = $childResult;
}
$childResult = $this->tldRule->evaluate($tld);
$valid = $childResult->isValid;
$children[] = $childResult;
}

foreach ($parts as $part) {
foreach ($this->partsRule->getRules() as $rule) {
$childResult = $rule->evaluate($part);
$valid = $valid && $childResult->isValid;
$children[] = $childResult;
}
$partsResult = $this->partsRule->evaluate($part);
$valid = $valid && $partsResult->isValid;
$children = array_merge($children, $partsResult->children);
}

return (new Result($valid, $input, $this))
Expand Down Expand Up @@ -141,9 +132,9 @@ private function collectAssertException(array &$exceptions, Validatable $validat
}
}

private function createGenericRule(): AllOf
private function createGenericRule(): Consecutive
{
return new AllOf(
return new Consecutive(
new StringType(),
new NoWhitespace(),
new Contains('.'),
Expand All @@ -157,7 +148,7 @@ private function createTldRule(bool $realTldCheck): Validatable
return new Tld();
}

return new AllOf(
return new Consecutive(
new Not(new StartsWith('-')),
new NoWhitespace(),
new Length(2)
Expand Down
Loading

0 comments on commit 2610a38

Please sign in to comment.