Skip to content

Commit dac3eea

Browse files
rodnaphlstrojny
authored andcommitted
add select_keys for dissecting assoc arrays (lstrojny#145)
* add select_keys for dissecting assoc arrays * support Traversable in select_keys * add docs for select_keys
1 parent c0c15f0 commit dac3eea

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"src/Functional/Reject.php",
9191
"src/Functional/Retry.php",
9292
"src/Functional/Select.php",
93+
"src/Functional/SelectKeys.php",
9394
"src/Functional/SequenceConstant.php",
9495
"src/Functional/SequenceExponential.php",
9596
"src/Functional/SequenceLinear.php",

docs/functional-php.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [first_index_of()](#first_index_of)
3737
- [last_index_of()](#last_index_of)
3838
- [indexes_of()](#indexes_of)
39+
- [select_keys()](#select_keys)
3940
- [Function functions](#function-functions)
4041
- [retry()](#retry)
4142
- [poll()](#poll)
@@ -610,6 +611,19 @@ use function Functional\indexes_of;
610611
$indexes = indexes_of(['value', 'value2', 'value'], 'value');
611612
```
612613

614+
## select_keys()
615+
616+
Returns an array containing only those entries in the array/Traversable whose key is in the supplied keys.
617+
618+
```php
619+
<?php
620+
621+
use function Functional\select_keys;
622+
623+
// $array will be ['foo' => 1, 'baz' => 3]
624+
$array = select_keys(['foo' => 1, 'bar' => 2', 'baz' => 3], ['foo', 'baz']);
625+
```
626+
613627
# Function functions
614628

615629
Function functions take a function or functions and return a new, modified version of the function.

src/Functional/SelectKeys.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright (C) 2011-2017 by Lars Strojny <lstrojny@php.net>
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
namespace Functional;
24+
25+
use Functional\Exceptions\InvalidArgumentException;
26+
use Traversable;
27+
28+
/**
29+
* Select the specified keys from the array
30+
*
31+
* @param Traversable|array $collection
32+
* @param array $keys
33+
* @return array
34+
*/
35+
function select_keys($collection, array $keys)
36+
{
37+
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
38+
39+
if ($collection instanceof Traversable) {
40+
$array = iterator_to_array($collection);
41+
} else {
42+
$array = $collection;
43+
}
44+
45+
return \array_intersect_key($array, \array_flip($keys));
46+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright (C) 2011-2017 by Lars Strojny <lstrojny@php.net>
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the 'Software'), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
namespace Functional\Tests;
24+
25+
use ArrayIterator;
26+
use function Functional\select_keys;
27+
28+
class SelectKeysTest extends AbstractTestCase
29+
{
30+
public static function getData()
31+
{
32+
return [
33+
[[], ['foo' => 1], []],
34+
[[], ['foo' => 1], ['bar']],
35+
[['foo' => 1], ['foo' => 1], ['foo']],
36+
[['foo' => 1, 'bar' => 2], ['foo' => 1, 'bar' => 2], ['foo', 'bar']],
37+
[[0 => 'a', 2 => 'c'], ['a', 'b', 'c'], [0, 2]],
38+
];
39+
}
40+
41+
/**
42+
* @dataProvider getData
43+
*/
44+
public function test(array $expected, array $input, array $keys)
45+
{
46+
$this->assertSame($expected, select_keys($input, $keys));
47+
$this->assertSame($expected, select_keys(new ArrayIterator($input), $keys));
48+
}
49+
50+
public function testPassNonArrayOrTraversable()
51+
{
52+
$this->expectArgumentError("Functional\select_keys() expects parameter 1 to be array or instance of Traversable");
53+
select_keys(new \stdclass(), []);
54+
}
55+
}

0 commit comments

Comments
 (0)