Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,35 @@ $value = \TraderInteractive\Filter\Filterer::ofArrays($value, ['id' => [['uint']
Aliased in the filterer as `ofArray`, this filter verifies that the argument is an array that passes the given specification. This is
essentially a flipped version of `Filterer::filter` that allows for testing nested associative arrays.

#### Arrays::copy

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.

Example Usage:
```php
$specification = ['field' => [['array-copy', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]]];
$filterer = new TraderInteractive\Filterer($specification);
$input = ['foo' => 123, 'bar' => 'abc'];
$result = $filterer->execute($input);
assert(['field' => ['FOO_VALUE' => 123, 'BAR_VALUE' => 'abc']], $result->filteredValue);
```

#### Arrays::copyEach

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.

Example Usage:
```php
$specification = ['field' => [['array-copy-each', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]]];
$filterer = new TraderInteractive\Filterer($specification);
$input = [
['foo' => 123, 'bar' => 'abc'],
['foo' => 456, 'bar' => 'def'],
];
$result = $filterer->execute($input);
assert(['field' => [['FOO_VALUE' => 123, 'BAR_VALUE' => 'abc'], ['FOO_VALUE' => 456, 'BAR_VALUE' => 'def']]], $result->filteredValue);
```

#### Arrays::in
Aliased in the filterer as `in`, this filter is a wrapper around `in_array` including support for strict equality testing.

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

#### Arrays::pad

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.

Example Usage:
```php
$specification = ['field' => [['array-pad', 5, 0, Arrays::ARRAY_PAD_LEFT]]],
$filterer = new TraderInteractive\Filterer($specification);
$input = [2, 4, 6];
$result = $filterer->execute($input);
assert(['field' => [0, 0, 2, 4, 6]], $result->filteredValue);

```

#### Booleans::filter
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
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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require": {
"php": "^7.0",
"traderinteractive/exceptions": "^1.2",
"traderinteractive/filter-arrays": "^3.1",
"traderinteractive/filter-arrays": "^3.2",
"traderinteractive/filter-bools": "^3.0",
"traderinteractive/filter-dates": "^3.0",
"traderinteractive/filter-floats": "^3.0",
Expand Down
4 changes: 4 additions & 0 deletions src/Filterer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InvalidArgumentException;
use Throwable;
use TraderInteractive\Exceptions\FilterException;
use TraderInteractive\Filter\Arrays;
use TraderInteractive\Filter\Json;
use TraderInteractive\Filter\PhoneFilter;
use TraderInteractive\Filter\XmlFilter;
Expand All @@ -20,6 +21,9 @@ final class Filterer implements FiltererInterface
*/
const DEFAULT_FILTER_ALIASES = [
'array' => '\\TraderInteractive\\Filter\\Arrays::filter',
'array-copy' => Arrays::class . '::copy',
'array-copy-each' => Arrays::class . '::copyEach',
'array-pad' => Arrays::class . '::pad',
'arrayize' => '\\TraderInteractive\\Filter\\Arrays::arrayize',
'bool' => '\\TraderInteractive\\Filter\\Booleans::filter',
'bool-convert' => '\\TraderInteractive\\Filter\\Booleans::convert',
Expand Down
54 changes: 54 additions & 0 deletions tests/FiltererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use stdClass;
use Throwable;
use TraderInteractive\Exceptions\FilterException;
use TraderInteractive\Filter\Arrays;
use TypeError;

/**
Expand Down Expand Up @@ -461,6 +462,59 @@ function (int $input, int $fieldOneValue) : int {
[]
],
],
'array-copy' => [
'spec' => [
'field' => [['array-copy', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]],
],
'input' => ['field' => ['foo' => 'abc', 'bar' => 123]],
'options' => [],
'result' => [
true,
['field' => ['FOO_VALUE' => 'abc', 'BAR_VALUE' => 123]],
null,
[],
],
],
'array-copy-each' => [
'spec' => [
'field' => [['array-copy-each', ['FOO_VALUE' => 'foo', 'BAR_VALUE' => 'bar']]],
],
'input' => [
'field' => [
['foo' => 'abc', 'bar' => 123],
['foo' => 'xyz', 'bar' => 789],
],
],
'options' => [],
'result' => [
true,
[
'field' => [
['FOO_VALUE' => 'abc', 'BAR_VALUE' => 123],
['FOO_VALUE' => 'xyz', 'BAR_VALUE' => 789],
],
],
null,
[],
],
],
'array-pad' => [
'spec' => [
'field' => [['array-pad', 5, 0, Arrays::ARRAY_PAD_FRONT]],
],
'input' => [
'field' => ['a', 'b', 'c'],
],
'options' => [],
'result' => [
true,
[
'field' => [0, 0, 'a', 'b', 'c'],
],
null,
[],
],
],
];
}

Expand Down