Skip to content

Commit

Permalink
Merge pull request #104 from chadicus/v3.x
Browse files Browse the repository at this point in the history
Add UUID Filter
  • Loading branch information
chadicus authored Nov 15, 2024
2 parents 55d2841 + 577d745 commit 296dc12
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .coveralls.yml

This file was deleted.

29 changes: 29 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: PHP Composer

on:
push:
branches: [ v3.x ]
pull_request:
branches: [ v3.x ]

jobs:
build:
runs-on: ubuntu-20.04
strategy:
matrix:
php-versions: ['7.0', '7.2', '7.3', '7.4']
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Validate composer.json and composer.lock
run: composer validate
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run PHPCS
run: composer run-script lint
- name: Run PHPUnit
run: composer run-script test
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,15 @@ The following ensures that `$value` is a valid `HH:MM:SS` formatted string.
$value = \TraderInteractive\Filter\TimeOfDayFilter::filter('12:15:23');
assert($value === '12:15:23');
```
#### UuidFilter::filter
Aliased in the filterer as `uuid`, this will filter a given string values as a valid UUID.

The following ensures the `$value` is a valid UUID v4 formatted string. Disallowing null values, nil UUIDs and UUID version other than v4

```php
$value = \TraderInteractive\Filter\UuidFilter::filter('2c02b87a-97ec-4de0-8c50-6721a29c150f', false, false, [4]);
assert($value === '2c02b87a-97ec-4de0-8c50-6721a29c150f');
```

#### XmlFilter::filter
Aliased in the filter as `xml`, this will ensure the given string value is valid XML, returning the original value.
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@
"traderinteractive/filter-dates": "^3.1",
"traderinteractive/filter-floats": "^3.0",
"traderinteractive/filter-ints": "^3.0",
"traderinteractive/filter-strings": "^3.5"
"traderinteractive/filter-strings": "^3.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.2",
"symfony/yaml": "^3.4"
},
"autoload": {
"psr-4": { "TraderInteractive\\": "src/" }
},
"scripts": {
"lint": "vendor/bin/phpcs",
"test": "vendor/bin/phpunit"
}
}
2 changes: 2 additions & 0 deletions src/Filterer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TraderInteractive\Filter\Json;
use TraderInteractive\Filter\PhoneFilter;
use TraderInteractive\Filter\TimeOfDayFilter;
use TraderInteractive\Filter\UuidFilter;
use TraderInteractive\Filter\XmlFilter;

/**
Expand Down Expand Up @@ -52,6 +53,7 @@ final class Filterer implements FiltererInterface
'translate' => '\\TraderInteractive\\Filter\\Strings::translate',
'uint' => '\\TraderInteractive\\Filter\\UnsignedInt::filter',
'url' => '\\TraderInteractive\\Filter\\Url::filter',
'uuid' => UuidFilter::class . '::filter',
'xml' => XmlFilter::class . '::filter',
'xml-extract' => XmlFilter::class . '::extract',
'xml-validate' => XmlFilter::class . '::validate',
Expand Down
48 changes: 34 additions & 14 deletions tests/FiltererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,23 @@ function (int $input, int $fieldOneValue) : int {
[],
],
],
'uuid' => [
'spec' => [
'field' => [['uuid', false, false, [4]]],
],
'input' => [
'field' => '2c02b87a-97ec-4de0-8c50-6721a29c150f',
],
'options' => [],
'result' => [
true,
[
'field' => '2c02b87a-97ec-4de0-8c50-6721a29c150f',
],
null,
[],
],
],
];
}

Expand Down Expand Up @@ -899,17 +916,18 @@ public function ofScalarsWithMeaninglessKeys()
*/
public function ofScalarsFail()
{
$valueOne = '1';
$valueTwo = [];
$valueThree = new stdClass();
try {
Filterer::ofScalars(['1', [], new stdClass], [['string']]);
Filterer::ofScalars([$valueOne, $valueTwo, $valueThree], [['string']]);
$this->fail();
} catch (FilterException $e) {
$valueTwoExport = var_export($valueTwo, true);
$valueThreeExport = var_export($valueThree, true);
$expected = <<<TXT
Field '1' with value 'array (
)' failed filtering, message 'Value 'array (
)' is not a string'
Field '2' with value 'stdClass::__set_state(array(
))' failed filtering, message 'Value 'stdClass::__set_state(array(
))' is not a string'
Field '1' with value '{$valueTwoExport}' failed filtering, message 'Value '{$valueTwoExport}' is not a string'
Field '2' with value '{$valueThreeExport}' failed filtering, message 'Value '{$valueThreeExport}' is not a string'
TXT;
$this->assertSame($expected, $e->getMessage());
}
Expand Down Expand Up @@ -957,20 +975,22 @@ public function ofArraysRequiredAndUnknown()
*/
public function ofArraysFail()
{
$valueOne = new stdClass();
$valueTwo = [];
$valueThree = null;
$valueFour = 'key';
try {
Filterer::ofArrays(
[['key' => new stdClass], ['key' => []], ['key' => null], 'key'],
[['key' => $valueOne], ['key' => $valueTwo], ['key' => $valueThree], $valueFour],
['key' => [['string']]]
);
$this->fail();
} catch (FilterException $e) {
$valueOneExport = var_export($valueOne, true);
$valueTwoExport = var_export($valueTwo, true);
$expected = <<<TXT
Field 'key' with value 'stdClass::__set_state(array(
))' failed filtering, message 'Value 'stdClass::__set_state(array(
))' is not a string'
Field 'key' with value 'array (
)' failed filtering, message 'Value 'array (
)' is not a string'
Field 'key' with value '{$valueOneExport}' failed filtering, message 'Value '{$valueOneExport}' is not a string'
Field 'key' with value '{$valueTwoExport}' failed filtering, message 'Value '{$valueTwoExport}' is not a string'
Field 'key' with value 'NULL' failed filtering, message 'Value failed filtering, \$allowNull is set to false'
Value at position '3' was not an array
TXT;
Expand Down

0 comments on commit 296dc12

Please sign in to comment.