Skip to content

Commit ed3482e

Browse files
authored
Add get_each function (#88)
* get_each function returns an array with the values or a $default value in the case it does not exist of each item in a $coll * Fixed the error analysing code in get_each.php, parameter #1 $key of function Lambdish\Phunctional\_get_values_from_key expects string, int|string given. * get_each function documentation * get_each function will return an empty array when no item contains the expected key.
1 parent 133c298 commit ed3482e

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed

docs/docs.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [flat_map](functions/flat_map.md): Returns an array containing the results of applying a given function to the items of a collection and flattening the results
2121
* [flatten](functions/flatten.md): Returns a flat collection from a multidimensional collection
2222
* [get](functions/get.md): Returns the value of an item in a collection or a default value in the case it does not exists
23+
* [get_each](functions/get_each.md): Returns an array with the values or a default value in the case it does not exist of each item in a collection
2324
* [get_in](functions/get_in.md): Returns the value in a nested associative structure or a default value in the case it does not exists
2425
* [group_by](functions/group_by.md): Returns an array with the items grouped by the results of applying a function to each item
2526
* [key](functions/key.md): Returns the key of an item value in a collection or a default value in the case it does not exists

docs/functions/get_each.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# get_each
2+
3+
## Description
4+
Returns an array with the values of the key of each item in a collection. An empty array is returned if no item contains the key.
5+
6+
7+
## Parameters
8+
9+
<dl>
10+
<dt>key</dt>
11+
<dd>Key to search in the collection.</dd>
12+
<dt>coll</dt>
13+
<dd>Collection where search the expected key.</dd>
14+
</dl>
15+
16+
## Examples
17+
18+
Returns an array with emails from each element of an array:
19+
```php
20+
<?php
21+
22+
use function Lambdish\Phunctional\get_each;
23+
24+
$users = [
25+
'user1' => [
26+
'name' => 'Mike',
27+
'email' => '[email protected]'
28+
],
29+
'user2' => [
30+
'name' => 'John',
31+
'email' => '[email protected]'
32+
],
33+
'user3' => [
34+
'name' => 'James',
35+
'phone' => '555-555555'
36+
]
37+
];
38+
39+
return get_each('email', $users);
40+
41+
42+
```
43+
44+
Returns an empty array because the expected key does not exist in any item from the collection:
45+
```php
46+
<?php
47+
48+
use function Lambdish\Phunctional\get_each;
49+
50+
$users = [
51+
'user1' => [
52+
'name' => 'Mike',
53+
'email' => '[email protected]'
54+
],
55+
'user2' => [
56+
'name' => 'John',
57+
'email' => '[email protected]'
58+
],
59+
'user3' => [
60+
'name' => 'James',
61+
'phone' => '555-555555'
62+
]
63+
];
64+
65+
return get_each('surname', $users);
66+
67+
// => []
68+
```

src/_bootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require __DIR__ . '/flat_map.php';
2121
require __DIR__ . '/flatten.php';
2222
require __DIR__ . '/get.php';
23+
require __DIR__ . '/get_each.php';
2324
require __DIR__ . '/get_in.php';
2425
require __DIR__ . '/group_by.php';
2526
require __DIR__ . '/key.php';

src/get_each.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Lambdish\Phunctional;
6+
7+
use Traversable;
8+
9+
/**
10+
* Returns an array with the values of the key of each item in a collection.
11+
* An empty array is returned if no item contains the key.
12+
*
13+
* @param string|int $key key to search in the collection
14+
* @param iterable $coll collection where search the expected key
15+
*
16+
* @return array
17+
*
18+
* @since 0.1
19+
*/
20+
function get_each($key, iterable $coll): array {
21+
return apply(
22+
pipe(
23+
_convert_traversable_to_array(),
24+
_get_values_from_key($key),
25+
_remove_null_values(),
26+
_reindex_array()
27+
),
28+
[$coll]
29+
);
30+
}
31+
32+
function _convert_traversable_to_array(): callable {
33+
return static function (iterable $coll): iterable {
34+
return $coll instanceof Traversable ? iterator_to_array($coll) : $coll;
35+
};
36+
}
37+
38+
function _get_values_from_key($key): callable {
39+
return static function (array $coll) use ($key): array {
40+
return array_merge(...map(
41+
static function (array $item) use ($key): array {
42+
$value = get($key, $item, null);
43+
return [$value];
44+
},
45+
array_values($coll)
46+
));
47+
};
48+
}
49+
50+
function _remove_null_values(): callable {
51+
return static function (array $coll): array {
52+
return filter_null($coll);
53+
};
54+
}
55+
56+
function _reindex_array(): callable {
57+
return static function (array $coll): array {
58+
return array_values($coll);
59+
};
60+
}
61+
62+
const get_each = '\Lambdish\Phunctional\get_each';

tests/GetEachTest.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Lambdish\Phunctional\Tests;
6+
7+
use ArrayIterator;
8+
use PHPUnit\Framework\TestCase;
9+
use function Lambdish\Phunctional\get_each;
10+
11+
final class GetEachTest extends TestCase {
12+
/** @test */
13+
public function it_should_return_an_array_with_values_of_each_item_of_an_existent_key_indexed_by_integers(): void {
14+
$actual = [
15+
[
16+
'key' => 1,
17+
'other_key' => 3
18+
],
19+
[
20+
'key' => 2,
21+
'other_key' => 4
22+
]
23+
];
24+
25+
$this->assertSame([1, 2], get_each('key', $actual));
26+
}
27+
28+
/** @test */
29+
public function it_should_return_an_array_with_values_of_each_item_of_an_existent_key(): void {
30+
$actual = [
31+
'one' => [
32+
'key' => 1,
33+
'other_key' => 3
34+
],
35+
'two' => [
36+
'key' => 2,
37+
'other_key' => 4
38+
]
39+
];
40+
41+
$this->assertSame([1, 2], get_each('key', $actual));
42+
}
43+
44+
/** @test */
45+
public function it_should_return_an_array_with_values_of_each_item_of_an_existent_property_of_a_traversable(): void {
46+
$traversable = new ArrayIterator([
47+
'one' => [
48+
'key' => 1,
49+
'other_key' => 3
50+
],
51+
'two' => [
52+
'key' => 2,
53+
'other_key' => 4
54+
]
55+
]);
56+
57+
$this->assertSame([1, 2], get_each('key', $traversable));
58+
}
59+
60+
/** @test */
61+
public function it_should_return_an_empty_array_if_the_key_does_not_exist(): void {
62+
$actual = [
63+
'one' => [
64+
'key' => 1,
65+
'other_key' => 3
66+
],
67+
'two' => [
68+
'key' => 2,
69+
'other_key' => 4
70+
]
71+
];
72+
73+
$this->assertSame([], get_each('not_existing_key', $actual));
74+
}
75+
76+
/** @test */
77+
public function it_should_return_an_empty_array_if_the_property_does_not_exist(): void {
78+
$traversable = new ArrayIterator([
79+
'one' => [
80+
'key' => 1,
81+
'other_key' => 3
82+
],
83+
'two' => [
84+
'key' => 2,
85+
'other_key' => 4
86+
]
87+
]);
88+
89+
$this->assertSame([], get_each('not_existing_key', $traversable));
90+
}
91+
92+
/** @test */
93+
public function it_should_return_an_array_with_values_of_each_item_of_an_existent_false_value(): void {
94+
$actual = [
95+
'one' => [
96+
'key' => null,
97+
'other_key' => true
98+
],
99+
'two' => [
100+
'key' => false,
101+
'other_key' => true
102+
]
103+
];
104+
105+
$this->assertSame([false], get_each('key', $actual));
106+
}
107+
108+
/** @test */
109+
public function it_should_return_an_array_with_values_of_each_item_of_an_existent_false_property_of_a_traversable(): void {
110+
$traversable = new ArrayIterator([
111+
'one' => [
112+
'key' => null,
113+
'other_key' => true
114+
],
115+
'two' => [
116+
'key' => false,
117+
'other_key' => true
118+
]
119+
]);
120+
121+
$this->assertSame([false], get_each('key', $traversable));
122+
}
123+
}

0 commit comments

Comments
 (0)