Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The sorted rule accepts a callback on its constructor that may be used to filter values from inside the input. However, with the "Call" rule one can archive almost the same result. Besides that particular characteristic, its constructor accepts a boolean value to determine whether the sorting is ascending or descending. This commit will remove the callback from the constructor and replace the boolean by a string which can be "ASC" or "DESC." Along with those changes, this change will make a few more improvements: - Make the exception message specific about the sorting direction; - Allow the rule to validate also strings; - Update documentation. Co-authored-by: Danilo Correa <danilosilva87@gmail.com> Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
- Loading branch information
1 parent
5444ab5
commit 1f6c821
Showing
6 changed files
with
207 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
# Ordered | ||
# Sorted | ||
|
||
- `Sorted(callable $fn = null, bool $ascending = true)` | ||
- `Sorted(string $direction)` | ||
|
||
Validates if the input is Sorted | ||
Validates whether the input is sorted in a certain order or not. | ||
|
||
```php | ||
v::sorted()->validate([1,2,3]); // true | ||
v::sorted()->validate([1,6,3]); // false | ||
v::sorted(null, false)->validate([3,2,1]); // true | ||
v::sorted(function($x){ | ||
return $x['key']; | ||
})->validate([ | ||
[ | ||
'key' => 1, | ||
], | ||
[ | ||
'key' => 5, | ||
], | ||
[ | ||
'key' => 9, | ||
], | ||
]); // true | ||
v::sorted(function($x){ | ||
return $x['key']; | ||
})->validate([ | ||
[ | ||
'key' => 1, | ||
], | ||
[ | ||
'key' => 7, | ||
], | ||
[ | ||
'key' => 4, | ||
], | ||
]); // false | ||
v::sorted('ASC')->validate([1, 2, 3]); // true | ||
v::sorted('ASC')->validate('ABC'); // true | ||
v::sorted('DESC')->validate([3, 2, 1]); // true | ||
v::sorted('ASC')->validate([]); // true | ||
v::sorted('ASC')->validate([1]); // true | ||
``` | ||
|
||
You can also combine [Call](Call.md) to create custom validations: | ||
|
||
```php | ||
v::call( | ||
static function (array $input): array { | ||
return array_column($input, 'key'); | ||
}, | ||
v::sorted('ASC') | ||
)->validate([ | ||
['key' => 1], | ||
['key' => 5], | ||
['key' => 9], | ||
]); // true | ||
|
||
v::call('strval', v::sorted('DESC'))->validate(4321); // true | ||
|
||
v::call('iterator_to_array', v::sorted())->validate(new ArrayIterator([1, 7, 4])); // false | ||
``` | ||
|
||
## Changelog | ||
|
||
Version | Description | ||
--------|------------- | ||
2.0.0 | Add support for strings | ||
2.0.0 | Do not use array keys to sort | ||
2.0.0 | Use sorting direction instead of boolean value | ||
2.0.0 | Do not accept callback in the constructor | ||
1.1.1 | Created | ||
|
||
*** | ||
See also: | ||
|
||
- [Call](Call.md) | ||
- [ArrayVal](ArrayVal.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--CREDITS-- | ||
Danilo Correa <danilosilva87@gmail.com> | ||
Henrique Moody <henriquemoody@gmail.com> | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Exceptions\NestedValidationException; | ||
use Respect\Validation\Exceptions\SortedException; | ||
use Respect\Validation\Validator as v; | ||
|
||
try { | ||
v::sorted('ASC')->check([1, 3, 2]); | ||
} catch (SortedException $exception) { | ||
echo $exception->getMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::sorted('DESC')->check([1, 2, 3]); | ||
} catch (SortedException $exception) { | ||
echo $exception->getMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::not(v::sorted('ASC'))->check([1, 2, 3]); | ||
} catch (SortedException $exception) { | ||
echo $exception->getMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::not(v::sorted('DESC'))->check([3, 2, 1]); | ||
} catch (SortedException $exception) { | ||
echo $exception->getMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::sorted('ASC')->assert([3, 2, 1]); | ||
} catch (NestedValidationException $exception) { | ||
echo $exception->getFullMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::sorted('DESC')->assert([1, 2, 3]); | ||
} catch (NestedValidationException $exception) { | ||
echo $exception->getFullMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::not(v::sorted('ASC'))->assert([1, 2, 3]); | ||
} catch (NestedValidationException $exception) { | ||
echo $exception->getFullMessage().PHP_EOL; | ||
} | ||
|
||
try { | ||
v::not(v::sorted('DESC'))->assert([3, 2, 1]); | ||
} catch (NestedValidationException $exception) { | ||
echo $exception->getFullMessage().PHP_EOL; | ||
} | ||
?> | ||
--EXPECT-- | ||
`{ 1, 3, 2 }` must be sorted in ascending order | ||
`{ 1, 2, 3 }` must be sorted in descending order | ||
`{ 1, 2, 3 }` must not be sorted in ascending order | ||
`{ 3, 2, 1 }` must not be sorted in descending order | ||
- `{ 3, 2, 1 }` must be sorted in ascending order | ||
- `{ 1, 2, 3 }` must be sorted in descending order | ||
- `{ 1, 2, 3 }` must not be sorted in ascending order | ||
- `{ 3, 2, 1 }` must not be sorted in descending order |
Oops, something went wrong.