Skip to content

Commit 9c56cad

Browse files
committed
Add omit_keys function.
1 parent ad864b2 commit 9c56cad

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-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/OmitKeys.php

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

tests/Functional/OmitKeysTest.php

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

0 commit comments

Comments
 (0)