Skip to content

Commit 507b136

Browse files
test except
1 parent 402d313 commit 507b136

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"src/Contains.php",
3737
"src/DotNotation.php",
3838
"src/Exactly.php",
39+
"src/Except.php",
3940
"src/Expand.php",
4041
"src/First.php",
4142
"src/Flatten.php",

src/Except.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* Returns a subset of the collection with elements that do not match given keys.
8+
* The keys are preserved
9+
*
10+
* @param array $items The collection
11+
* @param mixed ...$keys keys to be filtered
12+
* @return array
13+
*/
14+
function except(array $items, ...$keys): array
15+
{
16+
return array_diff_key($items, array_flip($keys));
17+
}

src/array_functions.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33

44
namespace BrenoRoosevelt;
55

6-
function except(array $items, ...$keys): array
7-
{
8-
return array_diff_key($items, array_flip($keys));
9-
}
10-
116
function column(iterable $items, $column): array
127
{
138
$result = [];

tests/ExceptTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\except;
8+
9+
class ExceptTest extends TestCase
10+
{
11+
public function testExcept()
12+
{
13+
$elements = [1, 'b', 'c', 'd'];
14+
$this->assertEquals([1, 1 => 'b', 3 => 'd'], except($elements, 2));
15+
}
16+
17+
public function testExceptKeyNotExists()
18+
{
19+
$elements = [1, 'b', 'c', 'd'];
20+
$this->assertEquals([1, 'b', 'c', 'd'], except($elements, 'b'));
21+
}
22+
23+
public function testExceptMany()
24+
{
25+
$elements = [1, 'b', 'c', 'd'];
26+
$this->assertEquals([2 => 'c', 3 => 'd'], except($elements, 0, 1));
27+
}
28+
29+
public function testExceptManyCase2()
30+
{
31+
$elements = [1, 'b', 'c', 'd'];
32+
$this->assertEquals([1, 'b'], except($elements, 2, 3));
33+
}
34+
}

tests/OnlyTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use PHPUnit\Framework\TestCase;
77
use function BrenoRoosevelt\only;
8-
use function BrenoRoosevelt\reject;
98

109
class OnlyTest extends TestCase
1110
{

0 commit comments

Comments
 (0)