Skip to content

Commit 7aeb609

Browse files
committed
Add UUID Filter alias
1 parent 808256a commit 7aeb609

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,15 @@ The following ensures that `$value` is a valid `HH:MM:SS` formatted string.
696696
$value = \TraderInteractive\Filter\TimeOfDayFilter::filter('12:15:23');
697697
assert($value === '12:15:23');
698698
```
699+
#### UuidFilter::filter
700+
Aliased in the filterer as `uuid`, this will filter a given string values as a valid UUID.
701+
702+
The following ensures the `$value` is a valid UUID v4 formatted string. Disallowing null values, nil UUIDs and UUID version other than v4
703+
704+
```php
705+
$value = \TraderInteractive\Filter\UuidFilter::filter('2c02b87a-97ec-4de0-8c50-6721a29c150f', false, false, [4]);
706+
assert($value === '2c02b87a-97ec-4de0-8c50-6721a29c150f');
707+
```
699708

700709
#### XmlFilter::filter
701710
Aliased in the filter as `xml`, this will ensure the given string value is valid XML, returning the original value.

src/Filterer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use TraderInteractive\Filter\Json;
1111
use TraderInteractive\Filter\PhoneFilter;
1212
use TraderInteractive\Filter\TimeOfDayFilter;
13+
use TraderInteractive\Filter\UuidFilter;
1314
use TraderInteractive\Filter\XmlFilter;
1415

1516
/**
@@ -52,6 +53,7 @@ final class Filterer implements FiltererInterface
5253
'translate' => '\\TraderInteractive\\Filter\\Strings::translate',
5354
'uint' => '\\TraderInteractive\\Filter\\UnsignedInt::filter',
5455
'url' => '\\TraderInteractive\\Filter\\Url::filter',
56+
'uuid' => UuidFilter::class . '::filter',
5557
'xml' => XmlFilter::class . '::filter',
5658
'xml-extract' => XmlFilter::class . '::extract',
5759
'xml-validate' => XmlFilter::class . '::validate',

tests/FiltererTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,23 @@ function (int $input, int $fieldOneValue) : int {
532532
[],
533533
],
534534
],
535+
'uuid' => [
536+
'spec' => [
537+
'field' => [['uuid', false, false, [4]]],
538+
],
539+
'input' => [
540+
'field' => '2c02b87a-97ec-4de0-8c50-6721a29c150f',
541+
],
542+
'options' => [],
543+
'result' => [
544+
true,
545+
[
546+
'field' => '2c02b87a-97ec-4de0-8c50-6721a29c150f',
547+
],
548+
null,
549+
[],
550+
],
551+
],
535552
];
536553
}
537554

0 commit comments

Comments
 (0)