Skip to content

Commit d4d3415

Browse files
committed
array_split_filter
1 parent 5d9a092 commit d4d3415

File tree

7 files changed

+84
-7
lines changed

7 files changed

+84
-7
lines changed

CHANGELOG.md

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

33
All Notable changes to `array-functions` will be documented in this file
44

5+
## 1.5.0 - 2015-06-29
6+
7+
### Added
8+
- array_split_filter function
9+
510
## 1.4.0 - 2015-06-29
611

712
### Added

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ function values_in_array($needles, array $haystack)
7676
function array_keys_exist($needles, array $haystack)
7777
```
7878

79+
```php`
80+
/**
81+
* Returns an array with two elements.
82+
*
83+
* Iterates over each value in the array passing them to the callback function.
84+
* If the callback function returns true, the current value from array is returned in the first
85+
* element of result array. If not, it is return in the second element of result array.
86+
*
87+
* Array keys are preserved.
88+
*
89+
* @param array $array
90+
* @param callable $callback
91+
* @return array
92+
*
93+
*/
94+
function array_split_filter(array $array, callable $callback)
95+
``
96+
7997
## Change log
8098

8199
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

src/array_functions.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function array_rand_value(array $array, $numReq = 1)
2828
/**
2929
* Get a random value from an array, with the ability to skew the results.
3030
* Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
31-
*
31+
*
3232
* @param array $array
33-
*
33+
*
3434
* @return mixed
3535
*/
3636
function array_rand_weighted(array $array)
@@ -65,7 +65,7 @@ function values_in_array($needles, array $haystack)
6565

6666
/**
6767
* Determine if all given needles are present in the haystack as array keys.
68-
*
68+
*
6969
* @param array|string $needles
7070
* @param array $haystack
7171
*
@@ -77,5 +77,30 @@ function array_keys_exist($needles, array $haystack)
7777
return array_key_exists($needles, $haystack);
7878
}
7979

80-
return values_in_array($needles, array_keys($haystack));
80+
return values_in_array($needles, array_keys($haystack));
81+
}
82+
83+
/**
84+
* Returns an array with two elements.
85+
*
86+
* Iterates over each value in the array passing them to the callback function.
87+
* If the callback function returns true, the current value from array is returned in the first
88+
* element of result array. If not, it is return in the second element of result array.
89+
*
90+
* Array keys are preserved.
91+
*
92+
* @param array $array
93+
* @param callable $callback
94+
*
95+
* @return array
96+
*/
97+
function array_split_filter(array $array, callable $callback)
98+
{
99+
$passesFilter = array_filter($array, $callback);
100+
101+
$negatedCallback = function ($item) use ($callback) { return ! $callback($item); };
102+
103+
$doesNotPassFilter = array_filter($array, $negatedCallback);
104+
105+
return [$passesFilter, $doesNotPassFilter];
81106
}

tests/ArrayKeysExistTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,4 @@ public function it_returns_false_when_needle_is_not_present_in_haystack()
7070
$this->assertFalse(array_keys_exist('d', ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']));
7171
$this->assertFalse(array_keys_exist(['d'], ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']));
7272
}
73-
7473
}

tests/ArrayRandValueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ArrayRandValueTest extends \PHPUnit_Framework_TestCase
99
protected $testArray = [
1010
'one' => 'a',
1111
'two' => 'b',
12-
'three' => 'c'
12+
'three' => 'c',
1313
];
1414

1515
/**

tests/ArraySplitFilterTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Spatie\Test;
4+
5+
use function spatie\array_split_filter;
6+
7+
class ArraySplitFilterTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function it_will_put_all_that_pass_the_test_in_the_first_element_and_all_that_not_pass_in_the_second()
13+
{
14+
$numbers = range(1, 6);
15+
16+
$evenTest = function ($number) { return $number % 2 == 0; };
17+
$oddTest = function ($number) { return $number % 2 != 0; };
18+
19+
$this->assertSame(array_filter($numbers, $evenTest), array_split_filter($numbers, $evenTest)[0]);
20+
$this->assertSame(array_filter($numbers, $oddTest), array_split_filter($numbers, $evenTest)[1]);
21+
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function it_can_handle_an_empty_array()
27+
{
28+
$this->assertTrue(is_array(array_split_filter([], function () {})));
29+
$this->assertCount(2, array_split_filter([], function () {}));
30+
}
31+
}

tests/ValuesInArrayTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,4 @@ public function it_returns_false_when_needle_is_not_present_in_haystack()
6363
$this->assertFalse(values_in_array('d', ['a', 'b', 'c']));
6464
$this->assertFalse(values_in_array(['d'], ['a', 'b', 'c']));
6565
}
66-
6766
}

0 commit comments

Comments
 (0)