Skip to content

Commit

Permalink
[10.x] Add Collection::select() method (#49845)
Browse files Browse the repository at this point in the history
* Add select method to Collection

* style

* Update Collection.php

* Update LazyCollection.php

---------

Co-authored-by: Craig Morris <>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
morrislaptop and taylorotwell authored Jan 31, 2024
1 parent 8206d96 commit 4d4a760
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,20 @@ public static function only($array, $keys)
return array_intersect_key($array, array_flip((array) $keys));
}

/**
* Select an array of values from an array.
*
* @param array $array
* @param array|string $keys
* @return array
*/
public static function select($array, $keys)
{
return static::map($array, function ($item) use ($keys) {
return array_intersect_key($item, array_flip((array) $keys));
});
}

/**
* Pluck an array of values from an array.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,27 @@ public function only($keys)
return new static(Arr::only($this->items, $keys));
}

/**
* Select specific values from the items within the collection.
*
* @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string|null $keys
* @return static
*/
public function select($keys)
{
if (is_null($keys)) {
return new static($this->items);
}

if ($keys instanceof Enumerable) {
$keys = $keys->all();
}

$keys = is_array($keys) ? $keys : func_get_args();

return new static(Arr::select($this->items, $keys));
}

/**
* Get and remove the last N items from the collection.
*
Expand Down
41 changes: 41 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,47 @@ public function only($keys)
});
}

/**
* Select specific values from the items within the collection.
*
* @param \Illuminate\Support\Enumerable<array-key, TKey>|array<array-key, TKey>|string $keys
* @return static
*/
public function select($keys)
{
if ($keys instanceof Enumerable) {
$keys = $keys->all();
} elseif (! is_null($keys)) {
$keys = is_array($keys) ? $keys : func_get_args();
}

return new static(function () use ($keys) {
if (is_null($keys)) {
yield from $this;
} else {
foreach ($this as $item) {
$itemKeys = array_flip($keys);

$result = [];

foreach ($item as $key => $value) {
if (array_key_exists($key, $itemKeys)) {
$result[$key] = $value;

unset($itemKeys[$key]);

if (empty($itemKeys)) {
continue;
}
}
}

yield $result;
}
}
});
}

/**
* Push all of the given items onto the collection.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4228,6 +4228,37 @@ public function testOnly($collection)
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->only(collect(['first', 'email']))->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testSelect($collection)
{
$data = new $collection([
['first' => 'Taylor', 'last' => 'Otwell', 'email' => '[email protected]'],
['first' => 'Jess', 'last' => 'Archer', 'email' => '[email protected]'],
]);

$this->assertEquals($data->all(), $data->select(null)->all());
$this->assertEquals([['first' => 'Taylor'], ['first' => 'Jess']], $data->select(['first', 'missing'])->all());
$this->assertEquals([['first' => 'Taylor'], ['first' => 'Jess']], $data->select('first', 'missing')->all());
$this->assertEquals([['first' => 'Taylor'], ['first' => 'Jess']], $data->select(collect(['first', 'missing']))->all());

$this->assertEquals([
['first' => 'Taylor', 'email' => '[email protected]'],
['first' => 'Jess', 'email' => '[email protected]'],
], $data->select(['first', 'email'])->all());

$this->assertEquals([
['first' => 'Taylor', 'email' => '[email protected]'],
['first' => 'Jess', 'email' => '[email protected]'],
], $data->select('first', 'email')->all());

$this->assertEquals([
['first' => 'Taylor', 'email' => '[email protected]'],
['first' => 'Jess', 'email' => '[email protected]'],
], $data->select(collect(['first', 'email']))->all());
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down

0 comments on commit 4d4a760

Please sign in to comment.