Skip to content

Commit a4825b7

Browse files
authored
Merge pull request #103 from chadicus/master
Add UUID Filter
2 parents 4cb52c7 + 5eb9383 commit a4825b7

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,16 @@ $value = \TraderInteractive\Filter\TimeOfDayFilter::filter('12:15:23');
722722
assert($value === '12:15:23');
723723
```
724724

725+
#### UuidFilter::filter
726+
Aliased in the filterer as `uuid`, this will filter a given string values as a valid UUID.
727+
728+
The following ensures the `$value` is a valid UUID v4 formatted string. Disallowing null values, nil UUIDs and UUID version other than v4
729+
730+
```php
731+
$value = \TraderInteractive\Filter\UuidFilter::filter('2c02b87a-97ec-4de0-8c50-6721a29c150f', false, false, [4]);
732+
assert($value === '2c02b87a-97ec-4de0-8c50-6721a29c150f');
733+
```
734+
725735
#### XmlFilter::filter
726736
Aliased in the filter as `xml`, this will ensure the given string value is valid XML, returning the original value.
727737

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"traderinteractive/filter-dates": "^4.0",
3737
"traderinteractive/filter-floats": "^4.0",
3838
"traderinteractive/filter-ints": "^4.0",
39-
"traderinteractive/filter-strings": "^4.0"
39+
"traderinteractive/filter-strings": "^4.1"
4040
},
4141
"require-dev": {
4242
"phpunit/phpunit": "^9.0",

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
/**
@@ -54,6 +55,7 @@ final class Filterer implements FiltererInterface
5455
'translate' => '\\TraderInteractive\\Filter\\Strings::translate',
5556
'uint' => '\\TraderInteractive\\Filter\\UnsignedInt::filter',
5657
'url' => '\\TraderInteractive\\Filter\\Url::filter',
58+
'uuid' => UuidFilter::class . '::filter',
5759
'xml' => XmlFilter::class . '::filter',
5860
'xml-extract' => XmlFilter::class . '::extract',
5961
'xml-validate' => XmlFilter::class . '::validate',

tests/FiltererTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,23 @@ function (int $input, int $fieldOneValue) : int {
548548
null,
549549
[],
550550
],
551-
551+
],
552+
'uuid' => [
553+
'spec' => [
554+
'field' => [['uuid', false, false, [4]]],
555+
],
556+
'input' => [
557+
'field' => '2c02b87a-97ec-4de0-8c50-6721a29c150f',
558+
],
559+
'options' => [],
560+
'result' => [
561+
true,
562+
[
563+
'field' => '2c02b87a-97ec-4de0-8c50-6721a29c150f',
564+
],
565+
null,
566+
[],
567+
],
552568
],
553569
];
554570
}

0 commit comments

Comments
 (0)