Skip to content

Commit 202338a

Browse files
author
Paul
committed
Added key parameter to filter strategy callback.
Updated documentation for filter strategy.
1 parent 4a7cc61 commit 202338a

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Filter(Strategy|Mapping|array|mixed $expression, callable $callback = null)
518518
```
519519

520520
1. `$expression` – Expression.
521-
2. `$callback` – Callback function that receives the current value as its first argument.
521+
2. `$callback` – Callback function that receives the current value as its first argument, the current key as its second argument and context as its third argument.
522522

523523
#### Example
524524

src/Strategy/Filter.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
use ScriptFUSION\Mapper\Mapping;
55

66
/**
7-
* Filters null values or values rejected by the specified callback
7+
* Filters null values or values rejected by the specified callback.
88
*/
99
class Filter extends Delegate
1010
{
11+
/**
12+
* @var callable|null
13+
*/
1114
private $callback;
1215

1316
/**
1417
* @param Strategy|Mapping|array|mixed $expression Expression.
15-
* @param callable|null $callback Callback function that receives the current value as its first argument.
18+
* @param callable|null $callback Callback function that receives the current value as its first argument, the
19+
* current key as its second argument and context as its third argument.
1620
*/
1721
public function __construct($expression, callable $callback = null)
1822
{
@@ -27,18 +31,20 @@ public function __invoke($data, $context = null)
2731
return null;
2832
}
2933

30-
$filter = function (array $data, $context) {
31-
$callback = $this->callback ?: function ($value) {
32-
return $value !== null;
33-
};
34+
return iterator_to_array(self::filter($this->callback ?: [$this, 'filterNulls'], $data, $context));
35+
}
3436

35-
foreach ($data as $datum) {
36-
if ($callback($datum, $context)) {
37-
yield $datum;
38-
}
37+
private static function filter(callable $callback, array $data, $context)
38+
{
39+
foreach ($data as $key => $datum) {
40+
if ($callback($datum, $key, $context)) {
41+
yield $key => $datum;
3942
}
40-
};
43+
}
44+
}
4145

42-
return iterator_to_array($filter($data, $context));
46+
private static function filterNulls($value)
47+
{
48+
return $value !== null;
4349
}
4450
}

test/Unit/Mapper/Strategy/FilterTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,38 @@ final class FilterTest extends \PHPUnit_Framework_TestCase
1212
public function testDefaultCallback()
1313
{
1414
$filter = new Filter(null);
15-
$filter->setMapper(MockFactory::mockMapper([null, 0, "0", null]));
15+
$filter->setMapper(MockFactory::mockMapper([null, 0, '0', null]));
1616

17-
self::assertEquals([0, "0"], array_values($filter([])));
17+
self::assertSame([1 => 0, 2 => '0'], $filter([]));
1818
}
1919

20-
public function testCustomCallback()
20+
public function testFilterByValue()
2121
{
2222
$filter = new Filter(null, function ($value) {
2323
return $value['foo'] === 'bar';
2424
});
2525

2626
$filter->setMapper(MockFactory::mockMapper([
27-
[
28-
'foo' => 'bar',
29-
],
30-
[
31-
'foo' => 'baz',
32-
],
27+
$bar = ['foo' => 'bar'],
28+
['foo' => 'baz'],
3329
]));
3430

35-
self::assertEquals([['foo' => 'bar']], $filter([]));
31+
self::assertSame([$bar], $filter([]));
32+
}
33+
34+
public function testFilterByKey()
35+
{
36+
$filter = new Filter(null, function ($_, $key) {
37+
return $key % 2;
38+
});
39+
$filter->setMapper(MockFactory::mockMapper(range('a', 'e')));
40+
41+
self::assertSame([1 => 'b', 3 => 'd'], $filter([]));
3642
}
3743

3844
public function testContextPassed()
3945
{
40-
$filter = new Filter(null, function ($_, $context) {
46+
$filter = new Filter(null, function ($_, $__, $context) {
4147
self::assertSame('foo', $context);
4248
});
4349
$filter->setMapper(MockFactory::mockMapper(['bar']));

0 commit comments

Comments
 (0)