Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Develop (#30)
Browse files Browse the repository at this point in the history
fixes #31
  • Loading branch information
prisis committed Sep 17, 2016
1 parent ced204a commit 51f4c8b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
45 changes: 26 additions & 19 deletions src/Arr/Arr.php
Expand Up @@ -237,7 +237,7 @@ public static function update(array $array, string $key, callable $callback)
$current = &$current[$key];
}

$current = call_user_func($callback, $current);
$current = $callback($current);

return $array;
}
Expand Down Expand Up @@ -491,7 +491,7 @@ public static function combine(array $array, callable $callback, bool $overwrite
$combined = [];

foreach ($array as $key => $value) {
$combinator = call_user_func($callback, $value, $key);
$combinator = $callback($value, $key);

// fix for hhvm #1871 bug
if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.10.0', '<=')) {
Expand Down Expand Up @@ -738,7 +738,7 @@ public static function groupBy(array $array, callable $callback = null): array
return array_reduce(
$array,
function ($buckets, $value) use ($callback) {
$key = call_user_func($callback, $value);
$key = $callback($value);

if (! array_key_exists($key, $buckets)) {
$buckets[$key] = [];
Expand Down Expand Up @@ -1026,7 +1026,7 @@ public static function map(array $array, callable $callback)
$newArray = [];

foreach ($array as $key => $item) {
$result = call_user_func($callback, $item, $key);
$result = $callback($item, $key);

$newArray = is_array($result) ?
array_replace_recursive($array, $result) :
Expand All @@ -1046,15 +1046,7 @@ public static function map(array $array, callable $callback)
*/
public static function filter(array $array, callable $callback)
{
$newArray = [];

foreach ($array as $key => $item) {
if (call_user_func($callback, $item, $key)) {
$newArray[$key] = $item;
}
}

return $newArray;
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
}

/**
Expand All @@ -1069,7 +1061,7 @@ public static function filter(array $array, callable $callback)
public static function all(array $array, callable $predicate)
{
foreach ($array as $key => $value) {
if (! call_user_func($predicate, $value, $key, $array)) {
if (! $predicate($value, $key, $array)) {
return false;
}
}
Expand All @@ -1088,7 +1080,7 @@ public static function all(array $array, callable $predicate)
public static function reject(array $array, callable $callback): array
{
return static::filter($array, function ($value, $key) use ($callback) {
return ! call_user_func($callback, $value, $key);
return ! $callback($value, $key);
});
}

Expand All @@ -1105,7 +1097,7 @@ public static function where(array $array, callable $callback): array
$filtered = [];

foreach ($array as $key => $value) {
if (call_user_func($callback, $key, $value)) {
if ($callback($key, $value)) {
$filtered[$key] = $value;
}
}
Expand Down Expand Up @@ -1135,7 +1127,7 @@ public static function first(array $array, callable $callback = null, $default =
}

foreach ($array as $key => $value) {
if (call_user_func($callback, $key, $value)) {
if ($callback($key, $value)) {
return $value;
}
}
Expand All @@ -1154,11 +1146,26 @@ public static function first(array $array, callable $callback = null, $default =
*/
public static function last(array $array, callable $callback = null, $default = null)
{
if (is_null($callback)) {
if ($callback === null) {
return empty($array) ? static::value($default) : end($array);
}

return static::first(array_reverse($array), $callback, $default);
return static::first(array_reverse($array, true), $callback, $default);
}

/**
* Get all of the given array except for a specified array of items.
*
* @param array $array
* @param array|string $keys
*
* @return array
*/
public static function except(array $array, $keys): array
{
static::forget($array, $keys);

return $array;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions tests/TraverseTest.php
Expand Up @@ -25,6 +25,7 @@ public function testFirst()
});

$this->assertEquals(200, $value);
$this->assertEquals(100, Arr::first($array));

$value = Arr::first($array, function ($key, $value) {
if ($key === $value) {
Expand Down Expand Up @@ -62,11 +63,19 @@ public function testReject()
public function testLast()
{
$array = [100, 200, 300];
$last = Arr::last($array, function () {
return true;

$last = Arr::last($array, function ($key, $value) {
return $value < 250;
});

$this->assertEquals(200, $last);

$last = Arr::last($array, function ($key) {
return $key < 2;
});

$this->assertEquals(300, $last);
$this->assertEquals(200, $last);
$this->assertEquals(300, Arr::last($array));
}

public function testWhere()
Expand Down

0 comments on commit 51f4c8b

Please sign in to comment.