Skip to content

Commit 296dc12

Browse files
authored
Merge pull request #104 from chadicus/v3.x
Add UUID Filter
2 parents 55d2841 + 577d745 commit 296dc12

File tree

7 files changed

+79
-37
lines changed

7 files changed

+79
-37
lines changed

.coveralls.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/php.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: PHP Composer
2+
3+
on:
4+
push:
5+
branches: [ v3.x ]
6+
pull_request:
7+
branches: [ v3.x ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-20.04
12+
strategy:
13+
matrix:
14+
php-versions: ['7.0', '7.2', '7.3', '7.4']
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
- name: Install PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: ${{ matrix.php-versions }}
22+
- name: Validate composer.json and composer.lock
23+
run: composer validate
24+
- name: Install dependencies
25+
run: composer install --prefer-dist --no-progress
26+
- name: Run PHPCS
27+
run: composer run-script lint
28+
- name: Run PHPUnit
29+
run: composer run-script test

.travis.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

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.

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@
3636
"traderinteractive/filter-dates": "^3.1",
3737
"traderinteractive/filter-floats": "^3.0",
3838
"traderinteractive/filter-ints": "^3.0",
39-
"traderinteractive/filter-strings": "^3.5"
39+
"traderinteractive/filter-strings": "^3.6"
4040
},
4141
"require-dev": {
42-
"php-coveralls/php-coveralls": "^2.0",
4342
"phpunit/phpunit": "^6.0",
4443
"squizlabs/php_codesniffer": "^3.2",
4544
"symfony/yaml": "^3.4"
4645
},
4746
"autoload": {
4847
"psr-4": { "TraderInteractive\\": "src/" }
48+
},
49+
"scripts": {
50+
"lint": "vendor/bin/phpcs",
51+
"test": "vendor/bin/phpunit"
4952
}
5053
}

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: 34 additions & 14 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

@@ -899,17 +916,18 @@ public function ofScalarsWithMeaninglessKeys()
899916
*/
900917
public function ofScalarsFail()
901918
{
919+
$valueOne = '1';
920+
$valueTwo = [];
921+
$valueThree = new stdClass();
902922
try {
903-
Filterer::ofScalars(['1', [], new stdClass], [['string']]);
923+
Filterer::ofScalars([$valueOne, $valueTwo, $valueThree], [['string']]);
904924
$this->fail();
905925
} catch (FilterException $e) {
926+
$valueTwoExport = var_export($valueTwo, true);
927+
$valueThreeExport = var_export($valueThree, true);
906928
$expected = <<<TXT
907-
Field '1' with value 'array (
908-
)' failed filtering, message 'Value 'array (
909-
)' is not a string'
910-
Field '2' with value 'stdClass::__set_state(array(
911-
))' failed filtering, message 'Value 'stdClass::__set_state(array(
912-
))' is not a string'
929+
Field '1' with value '{$valueTwoExport}' failed filtering, message 'Value '{$valueTwoExport}' is not a string'
930+
Field '2' with value '{$valueThreeExport}' failed filtering, message 'Value '{$valueThreeExport}' is not a string'
913931
TXT;
914932
$this->assertSame($expected, $e->getMessage());
915933
}
@@ -957,20 +975,22 @@ public function ofArraysRequiredAndUnknown()
957975
*/
958976
public function ofArraysFail()
959977
{
978+
$valueOne = new stdClass();
979+
$valueTwo = [];
980+
$valueThree = null;
981+
$valueFour = 'key';
960982
try {
961983
Filterer::ofArrays(
962-
[['key' => new stdClass], ['key' => []], ['key' => null], 'key'],
984+
[['key' => $valueOne], ['key' => $valueTwo], ['key' => $valueThree], $valueFour],
963985
['key' => [['string']]]
964986
);
965987
$this->fail();
966988
} catch (FilterException $e) {
989+
$valueOneExport = var_export($valueOne, true);
990+
$valueTwoExport = var_export($valueTwo, true);
967991
$expected = <<<TXT
968-
Field 'key' with value 'stdClass::__set_state(array(
969-
))' failed filtering, message 'Value 'stdClass::__set_state(array(
970-
))' is not a string'
971-
Field 'key' with value 'array (
972-
)' failed filtering, message 'Value 'array (
973-
)' is not a string'
992+
Field 'key' with value '{$valueOneExport}' failed filtering, message 'Value '{$valueOneExport}' is not a string'
993+
Field 'key' with value '{$valueTwoExport}' failed filtering, message 'Value '{$valueTwoExport}' is not a string'
974994
Field 'key' with value 'NULL' failed filtering, message 'Value failed filtering, \$allowNull is set to false'
975995
Value at position '3' was not an array
976996
TXT;

0 commit comments

Comments
 (0)