Skip to content

Commit 8f62d13

Browse files
committed
Array collection
1 parent 577eeb8 commit 8f62d13

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

src/Util/ArrayUtils.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ function mapWithKeys(array $array, callable $callback) : array
1616
return $arr;
1717
}
1818

19+
function filter(array $array, callable $callback) : array
20+
{
21+
return array_filter($array, function ($v, $k) use ($callback) {
22+
return $callback($k, $v);
23+
}, ARRAY_FILTER_USE_BOTH);
24+
}
25+
1926
function each(array $array, callable $callback) : void
2027
{
2128
foreach ($array as $k => $v) {
@@ -27,3 +34,8 @@ function max(array $items) : int
2734
{
2835
return count($items) > 0 ? \max($items) : 0;
2936
}
37+
38+
function collect(array $items) : Collection
39+
{
40+
return new Collection($items);
41+
}

src/Util/Collection.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\CliMenu\Util;
6+
7+
class Collection
8+
{
9+
/**
10+
* @var array
11+
*/
12+
private $items;
13+
14+
public function __construct(array $items)
15+
{
16+
$this->items = $items;
17+
}
18+
19+
public function map(callable $cb) : self
20+
{
21+
return new self(mapWithKeys($this->items, $cb));
22+
}
23+
24+
public function filter(callable $cb) : self
25+
{
26+
return new self(filter($this->items, $cb));
27+
}
28+
29+
public function values() : self
30+
{
31+
return new self(array_values($this->items));
32+
}
33+
34+
public function each(callable $cb) : self
35+
{
36+
each($this->items, $cb);
37+
38+
return $this;
39+
}
40+
41+
public function all() : array
42+
{
43+
return $this->items;
44+
}
45+
}

test/Util/ArrayUtilTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use PhpSchool\CliMenu\Util\ArrayUtil;
88
use PHPUnit\Framework\TestCase;
9+
use function PhpSchool\CliMenu\Util\collect;
910
use function PhpSchool\CliMenu\Util\each;
11+
use function PhpSchool\CliMenu\Util\filter;
1012
use function PhpSchool\CliMenu\Util\mapWithKeys;
1113
use function PhpSchool\CliMenu\Util\max;
1214

@@ -55,4 +57,18 @@ public function testMax() : void
5557
self::assertEquals(3, max([1, 2, 3]));
5658
self::assertEquals(6, max([1, 6, 3]));
5759
}
60+
61+
public function testFilter() : void
62+
{
63+
$cb = function (int $k, int $v) {
64+
return $v > 3;
65+
};
66+
67+
self::assertEquals([3 => 4, 4 => 5, 5 => 6], filter([1, 2, 3, 4, 5, 6], $cb));
68+
}
69+
70+
public function testCollect() : void
71+
{
72+
self::assertEquals([1, 2, 3], collect([1, 2, 3])->all());
73+
}
5874
}

test/Util/CollectionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\CliMenuTest\Util;
6+
7+
use PhpSchool\CliMenu\Util\Collection;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class CollectionTest extends TestCase
11+
{
12+
public function testCollection() : void
13+
{
14+
$result = (new Collection([1, 2, 3, 4, 5, 6]))
15+
->filter(function ($k, $v) {
16+
return $v > 3;
17+
})
18+
->values()
19+
->map(function ($k, $v) {
20+
return $v * 2;
21+
})
22+
->all();
23+
24+
self::assertEquals([8, 10, 12], $result);
25+
}
26+
}

0 commit comments

Comments
 (0)