Skip to content

Commit 58de438

Browse files
authored
Merge pull request #95 from chadicus/master
Update requirement for traderinteractive/filter-arrays
2 parents 2252d5e + b04d835 commit 58de438

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,35 @@ $value = \TraderInteractive\Filter\Filterer::ofArrays($value, ['id' => [['uint']
471471
Aliased in the filterer as `ofArray`, this filter verifies that the argument is an array that passes the given specification. This is
472472
essentially a flipped version of `Filterer::filter` that allows for testing nested associative arrays.
473473

474+
#### Arrays::copy
475+
476+
Aliased in the filterer as `array-copy`, this filter copies values from the source array into a destination array using the provided destination key map.
477+
478+
Example Usage:
479+
```php
480+
$specification = ['field' => [['array-copy', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]]];
481+
$filterer = new TraderInteractive\Filterer($specification);
482+
$input = ['foo' => 123, 'bar' => 'abc'];
483+
$result = $filterer->execute($input);
484+
assert(['field' => ['FOO_VALUE' => 123, 'BAR_VALUE' => 'abc']], $result->filteredValue);
485+
```
486+
487+
#### Arrays::copyEach
488+
489+
Aliased in the filterer as `array-copy-each`, this filter copies values from each array in the source array into a destination array using the provided destination key map.
490+
491+
Example Usage:
492+
```php
493+
$specification = ['field' => [['array-copy-each', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]]];
494+
$filterer = new TraderInteractive\Filterer($specification);
495+
$input = [
496+
['foo' => 123, 'bar' => 'abc'],
497+
['foo' => 456, 'bar' => 'def'],
498+
];
499+
$result = $filterer->execute($input);
500+
assert(['field' => [['FOO_VALUE' => 123, 'BAR_VALUE' => 'abc'], ['FOO_VALUE' => 456, 'BAR_VALUE' => 'def']]], $result->filteredValue);
501+
```
502+
474503
#### Arrays::in
475504
Aliased in the filterer as `in`, this filter is a wrapper around `in_array` including support for strict equality testing.
476505

@@ -503,6 +532,20 @@ $value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
503532
assert($value === [1, 2, 3, 4, 5]);
504533
```
505534

535+
#### Arrays::pad
536+
537+
Aliased in the filterer as `array-pad`, this filter pads an array to the specified length with a value. Padding optionally to the front or end of the array.
538+
539+
Example Usage:
540+
```php
541+
$specification = ['field' => [['array-pad', 5, 0, Arrays::ARRAY_PAD_LEFT]]],
542+
$filterer = new TraderInteractive\Filterer($specification);
543+
$input = [2, 4, 6];
544+
$result = $filterer->execute($input);
545+
assert(['field' => [0, 0, 2, 4, 6]], $result->filteredValue);
546+
547+
```
548+
506549
#### Booleans::filter
507550
Aliased in the filterer as `bool`, this filter verifies that the argument is a boolean value or a string that maps to one. The second parameter
508551
can be set to `true` to allow null values through without an error (they will stay null and not get converted to false). The last parameters

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"require": {
3232
"php": "^7.0",
3333
"traderinteractive/exceptions": "^1.2",
34-
"traderinteractive/filter-arrays": "^3.1",
34+
"traderinteractive/filter-arrays": "^3.2",
3535
"traderinteractive/filter-bools": "^3.0",
3636
"traderinteractive/filter-dates": "^3.0",
3737
"traderinteractive/filter-floats": "^3.0",

src/Filterer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use InvalidArgumentException;
77
use Throwable;
88
use TraderInteractive\Exceptions\FilterException;
9+
use TraderInteractive\Filter\Arrays;
910
use TraderInteractive\Filter\Json;
1011
use TraderInteractive\Filter\PhoneFilter;
1112
use TraderInteractive\Filter\XmlFilter;
@@ -20,6 +21,9 @@ final class Filterer implements FiltererInterface
2021
*/
2122
const DEFAULT_FILTER_ALIASES = [
2223
'array' => '\\TraderInteractive\\Filter\\Arrays::filter',
24+
'array-copy' => Arrays::class . '::copy',
25+
'array-copy-each' => Arrays::class . '::copyEach',
26+
'array-pad' => Arrays::class . '::pad',
2327
'arrayize' => '\\TraderInteractive\\Filter\\Arrays::arrayize',
2428
'bool' => '\\TraderInteractive\\Filter\\Booleans::filter',
2529
'bool-convert' => '\\TraderInteractive\\Filter\\Booleans::convert',

tests/FiltererTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use stdClass;
1010
use Throwable;
1111
use TraderInteractive\Exceptions\FilterException;
12+
use TraderInteractive\Filter\Arrays;
1213
use TypeError;
1314

1415
/**
@@ -461,6 +462,59 @@ function (int $input, int $fieldOneValue) : int {
461462
[]
462463
],
463464
],
465+
'array-copy' => [
466+
'spec' => [
467+
'field' => [['array-copy', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]],
468+
],
469+
'input' => ['field' => ['foo' => 'abc', 'bar' => 123]],
470+
'options' => [],
471+
'result' => [
472+
true,
473+
['field' => ['FOO_VALUE' => 'abc', 'BAR_VALUE' => 123]],
474+
null,
475+
[],
476+
],
477+
],
478+
'array-copy-each' => [
479+
'spec' => [
480+
'field' => [['array-copy-each', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]],
481+
],
482+
'input' => [
483+
'field' => [
484+
['foo' => 'abc', 'bar' => 123],
485+
['foo' => 'xyz', 'bar' => 789],
486+
],
487+
],
488+
'options' => [],
489+
'result' => [
490+
true,
491+
[
492+
'field' => [
493+
['FOO_VALUE' => 'abc', 'BAR_VALUE' => 123],
494+
['FOO_VALUE' => 'xyz', 'BAR_VALUE' => 789],
495+
],
496+
],
497+
null,
498+
[],
499+
],
500+
],
501+
'array-pad' => [
502+
'spec' => [
503+
'field' => [['array-pad', 5, 0, Arrays::ARRAY_PAD_FRONT]],
504+
],
505+
'input' => [
506+
'field' => ['a', 'b', 'c'],
507+
],
508+
'options' => [],
509+
'result' => [
510+
true,
511+
[
512+
'field' => [0, 0, 'a', 'b', 'c'],
513+
],
514+
null,
515+
[],
516+
],
517+
],
464518
];
465519
}
466520

0 commit comments

Comments
 (0)