Skip to content

Commit dbf8a44

Browse files
jasonmmlstrojny
authored andcommitted
Add omit_keys function. (#204)
1 parent ad864b2 commit dbf8a44

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"src/Functional/Minimum.php",
7979
"src/Functional/None.php",
8080
"src/Functional/Not.php",
81+
"src/Functional/OmitKeys.php",
8182
"src/Functional/PartialAny.php",
8283
"src/Functional/PartialLeft.php",
8384
"src/Functional/PartialMethod.php",

docs/functional-php.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- [last_index_of()](#last_index_of)
3838
- [indexes_of()](#indexes_of)
3939
- [select_keys()](#select_keys)
40+
- [omit_keys()](#omit_keys)
4041
- [take_left()](#take_left)
4142
- [take_right()](#take_right)
4243
- [Function functions](#function-functions)
@@ -627,7 +628,20 @@ Returns an array containing only those entries in the array/Traversable whose ke
627628
use function Functional\select_keys;
628629

629630
// $array will be ['foo' => 1, 'baz' => 3]
630-
$array = select_keys(['foo' => 1, 'bar' => 2', 'baz' => 3], ['foo', 'baz']);
631+
$array = select_keys(['foo' => 1, 'bar' => 2, 'baz' => 3], ['foo', 'baz']);
632+
```
633+
634+
## omit_keys()
635+
636+
Returns an array containing only those entries in the array/Traversable whose key is not in the supplied keys.
637+
638+
```php
639+
<?php
640+
641+
use function Functional\omit_keys;
642+
643+
// $array will be ['bar' => 2]
644+
$array = omit_keys(['foo' => 1, 'bar' => 2, 'baz' => 3], ['foo', 'baz']);
631645
```
632646

633647
## take_left()

src/Functional/Functional.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ final class Functional
277277
*/
278278
const not = '\Functional\not';
279279

280+
/**
281+
* @see \Functional\omit_keys
282+
*/
283+
const omit_keys = '\Functional\omit_keys';
284+
280285
/**
281286
* @see \Functional\partial_any
282287
*/

src/Functional/OmitKeys.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* @package Functional-php
5+
* @author Lars Strojny <lstrojny@php.net>
6+
* @copyright 2011-2017 Lars Strojny
7+
* @license https://opensource.org/licenses/MIT MIT
8+
* @link https://github.com/lstrojny/functional-php
9+
*/
10+
11+
namespace Functional;
12+
13+
use Functional\Exceptions\InvalidArgumentException;
14+
use Traversable;
15+
16+
/**
17+
* Returns an array with the specified keys omitted from the array
18+
*
19+
* @param Traversable|array $collection
20+
* @param array $keys
21+
* @return array
22+
*/
23+
function omit_keys($collection, array $keys)
24+
{
25+
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
26+
27+
if ($collection instanceof Traversable) {
28+
$array = \iterator_to_array($collection);
29+
} else {
30+
$array = $collection;
31+
}
32+
33+
return \array_diff_key($array, \array_flip($keys));
34+
}

tests/Functional/OmitKeysTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* @package Functional-php
5+
* @author Lars Strojny <lstrojny@php.net>
6+
* @copyright 2011-2017 Lars Strojny
7+
* @license https://opensource.org/licenses/MIT MIT
8+
* @link https://github.com/lstrojny/functional-php
9+
*/
10+
11+
namespace Functional\Tests;
12+
13+
use ArrayIterator;
14+
15+
use function Functional\omit_keys;
16+
17+
class OmitKeysTest extends AbstractTestCase
18+
{
19+
public static function getData()
20+
{
21+
return [
22+
[['foo' => 1], ['foo' => 1], []],
23+
[['foo' => 1], ['foo' => 1], ['bar']],
24+
[[], ['foo' => 1], ['foo']],
25+
[[], ['foo' => 1, 'bar' => 2], ['foo', 'bar']],
26+
[['bar' => 2], ['foo' => 1, 'bar' => 2], ['foo']],
27+
[[1 => 'b'], ['a', 'b', 'c'], [0, 2]],
28+
];
29+
}
30+
31+
/**
32+
* @dataProvider getData
33+
*/
34+
public function test(array $expected, array $input, array $keys)
35+
{
36+
$this->assertSame($expected, omit_keys($input, $keys));
37+
$this->assertSame($expected, omit_keys(new ArrayIterator($input), $keys));
38+
}
39+
40+
public function testPassNonArrayOrTraversable()
41+
{
42+
$this->expectArgumentError("Functional\omit_keys() expects parameter 1 to be array or instance of Traversable");
43+
omit_keys(new \stdclass(), []);
44+
}
45+
}

0 commit comments

Comments
 (0)