Skip to content

Commit

Permalink
[5.4] Arr::pluck - handle situation when itemKey is an object (#19838)
Browse files Browse the repository at this point in the history
* Arr::pluck - handle situation when itemKey is an object

* Fix StyleCI issues
  • Loading branch information
crlcu authored and taylorotwell committed Jun 30, 2017
1 parent 6eaba45 commit 900a39c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public static function pluck($array, $value, $key = null)
} else {
$itemKey = data_get($item, $key);

if (gettype($itemKey) === 'object') {
$itemKey = (string) $itemKey;
}

$results[$itemKey] = $itemValue;
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use stdClass;
use ArrayObject;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -370,6 +371,15 @@ public function testPluckWithKeys()
], $test2);
}

public function testPluckWithCarbonKeys()
{
$array = [
['start' => new Carbon('2017-07-25 00:00:00'), 'end' => new Carbon('2017-07-30 00:00:00')],
];
$array = Arr::pluck($array, 'end', 'start');
$this->assertEquals(['2017-07-25 00:00:00' => '2017-07-30 00:00:00'], $array);
}

public function testPrepend()
{
$array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');
Expand Down

0 comments on commit 900a39c

Please sign in to comment.