Skip to content

Commit c95233e

Browse files
sfinktahfreekmurze
andauthored
Sfink pluck many values (#242)
* added pluckManyValues; extended pluckMany * commited tests for PluckManyvALUES * ... updated README * fixed test for pluckManyValues * Update PluckManyValues.php --------- Co-authored-by: Freek Van der Herten <[email protected]>
1 parent 5f33d94 commit c95233e

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ The package will automatically register itself.
6969
- [`parallelMap`](#parallelmap)
7070
- [`path`](#path)
7171
- [`pluckMany`](#pluckmany)
72+
- [`pluckManyValues`](#pluckmanyvalues)
7273
- [`pluckToArray`](#plucktoarray)
7374
- [`prioritize`](#prioritize)
7475
- [`recursive`](#recursive)
@@ -659,6 +660,25 @@ $collection->pluckMany(['a', 'b']);
659660
// ]);
660661
```
661662

663+
### `pluckManyValues`
664+
665+
Returns a collection with only the specified keys' values.
666+
667+
```php
668+
$collection = collect([
669+
['a' => 1, 'b' => 10, 'c' => 100],
670+
['a' => 2, 'b' => 20, 'c' => 200],
671+
]);
672+
673+
$collection->pluckMany(['a', 'b']);
674+
675+
// returns
676+
// collect([
677+
// [1, 10],
678+
// [2, 20],
679+
// ]);
680+
```
681+
662682
### `pluckToArray`
663683

664684
Returns array of values of a given key.

src/CollectionMacroServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private function macros(): array
5151
'parallelMap' => \Spatie\CollectionMacros\Macros\ParallelMap::class,
5252
'path' => \Spatie\CollectionMacros\Macros\Path::class,
5353
'pluckMany' => \Spatie\CollectionMacros\Macros\PluckMany::class,
54+
'pluckManyValues' => \Spatie\CollectionMacros\Macros\PluckManyValues::class,
5455
'pluckToArray' => \Spatie\CollectionMacros\Macros\PluckToArray::class,
5556
'prioritize' => \Spatie\CollectionMacros\Macros\Prioritize::class,
5657
'recursive' => \Spatie\CollectionMacros\Macros\Recursive::class,

src/Macros/PluckMany.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class PluckMany
2020
public function __invoke()
2121
{
2222
return function ($keys): Collection {
23+
// Allow passing multiple keys as multiple arguments
24+
$keys = is_array($keys) ? $keys : func_get_args();
2325
return $this->map(function ($item) use ($keys) {
2426
if ($item instanceof Collection) {
2527
return $item->only($keys);

src/Macros/PluckManyValues.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Spatie\CollectionMacros\Macros;
4+
5+
use ArrayAccess;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
8+
9+
/**
10+
* Get a Collection with only the specified keys.
11+
*
12+
* @param array $keys
13+
*
14+
* @mixin \Illuminate\Support\Collection
15+
*
16+
* @return Collection
17+
*/
18+
class PluckManyValues
19+
{
20+
public function __invoke()
21+
{
22+
return function ($keys): Collection {
23+
// Allow passing multiple keys as multiple arguments
24+
$keys = is_array($keys) ? $keys : func_get_args();
25+
return $this->pluckMany($keys)->map(function ($item) {
26+
if ($item instanceof Collection) {
27+
return $item->values();
28+
}
29+
30+
if (is_array($item)) {
31+
return array_values($item);
32+
}
33+
34+
return (object) array_values(get_object_vars($item));
35+
});
36+
};
37+
}
38+
}

tests/Macros/PluckManyValuesTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Spatie\CollectionMacros\Test\Macros;
4+
5+
use ArrayAccess;
6+
use Illuminate\Support\Collection;
7+
use Spatie\CollectionMacros\Test\TestCase;
8+
9+
class PluckManyValuesTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_provides_a_pluckManyValues_macro()
13+
{
14+
$this->assertTrue(Collection::hasMacro('pluckManyValues'));
15+
}
16+
17+
/** @test */
18+
public function it_can_pluck_from_a_collection_of_collections()
19+
{
20+
$data = Collection::make([
21+
collect(['id' => 1, 'name' => 'matt', 'hobby' => 'coding']),
22+
collect(['id' => 2, 'name' => 'tomo', 'hobby' => 'cooking']),
23+
]);
24+
25+
$this->assertEquals($data->map->only(['name', 'hobby'])->map->values(), $data->pluckManyValues(['name', 'hobby']));
26+
}
27+
28+
/** @test */
29+
public function it_can_pluck_from_array_and_object_items()
30+
{
31+
$data = Collection::make([
32+
(object) ['id' => 1, 'name' => 'matt', 'hobby' => 'coding'],
33+
['id' => 2, 'name' => 'tomo', 'hobby' => 'cooking'],
34+
]);
35+
36+
$this->assertEquals(
37+
[
38+
(object) ['matt', 'coding'],
39+
['tomo', 'cooking'],
40+
],
41+
$data->pluckManyValues(['name', 'hobby'])->all()
42+
);
43+
}
44+
45+
/** @test */
46+
public function it_can_pluck_from_objects_that_implement_array_access_interface()
47+
{
48+
$data = Collection::make([
49+
new TestArrayAccessImplementation(['id' => 1, 'name' => 'marco', 'hobby' => 'drinking']),
50+
new TestArrayAccessImplementation(['id' => 2, 'name' => 'belle', 'hobby' => 'cross-stitch']),
51+
]);
52+
53+
$this->assertEquals(
54+
[
55+
['marco', 'drinking'],
56+
['belle', 'cross-stitch'],
57+
],
58+
$data->pluckManyValues(['name', 'hobby'])->all()
59+
);
60+
}
61+
}
62+

0 commit comments

Comments
 (0)