Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add Collection::select() method #49845

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
driesvints marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading