diff --git a/README.md b/README.md index 66b122c..986a269 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ The package will automatically register itself. - [`parallelMap`](#parallelmap) - [`path`](#path) - [`pluckMany`](#pluckmany) +- [`pluckManyValues`](#pluckmanyvalues) - [`pluckToArray`](#plucktoarray) - [`prioritize`](#prioritize) - [`recursive`](#recursive) @@ -659,6 +660,25 @@ $collection->pluckMany(['a', 'b']); // ]); ``` +### `pluckManyValues` + +Returns a collection with only the specified keys' values. + +```php +$collection = collect([ + ['a' => 1, 'b' => 10, 'c' => 100], + ['a' => 2, 'b' => 20, 'c' => 200], +]); + +$collection->pluckMany(['a', 'b']); + +// returns +// collect([ +// [1, 10], +// [2, 20], +// ]); +``` + ### `pluckToArray` Returns array of values of a given key. diff --git a/src/CollectionMacroServiceProvider.php b/src/CollectionMacroServiceProvider.php index 270be6c..0a02144 100644 --- a/src/CollectionMacroServiceProvider.php +++ b/src/CollectionMacroServiceProvider.php @@ -51,6 +51,7 @@ private function macros(): array 'parallelMap' => \Spatie\CollectionMacros\Macros\ParallelMap::class, 'path' => \Spatie\CollectionMacros\Macros\Path::class, 'pluckMany' => \Spatie\CollectionMacros\Macros\PluckMany::class, + 'pluckManyValues' => \Spatie\CollectionMacros\Macros\PluckManyValues::class, 'pluckToArray' => \Spatie\CollectionMacros\Macros\PluckToArray::class, 'prioritize' => \Spatie\CollectionMacros\Macros\Prioritize::class, 'recursive' => \Spatie\CollectionMacros\Macros\Recursive::class, diff --git a/src/Macros/PluckMany.php b/src/Macros/PluckMany.php index 625d507..d24c9d0 100644 --- a/src/Macros/PluckMany.php +++ b/src/Macros/PluckMany.php @@ -20,6 +20,8 @@ class PluckMany public function __invoke() { return function ($keys): Collection { + // Allow passing multiple keys as multiple arguments + $keys = is_array($keys) ? $keys : func_get_args(); return $this->map(function ($item) use ($keys) { if ($item instanceof Collection) { return $item->only($keys); diff --git a/src/Macros/PluckManyValues.php b/src/Macros/PluckManyValues.php new file mode 100644 index 0000000..ed5a77c --- /dev/null +++ b/src/Macros/PluckManyValues.php @@ -0,0 +1,38 @@ +pluckMany($keys)->map(function ($item) { + if ($item instanceof Collection) { + return $item->values(); + } + + if (is_array($item)) { + return array_values($item); + } + + return (object) array_values(get_object_vars($item)); + }); + }; + } +} diff --git a/tests/Macros/PluckManyValuesTest.php b/tests/Macros/PluckManyValuesTest.php new file mode 100644 index 0000000..884d7ec --- /dev/null +++ b/tests/Macros/PluckManyValuesTest.php @@ -0,0 +1,62 @@ +assertTrue(Collection::hasMacro('pluckManyValues')); + } + + /** @test */ + public function it_can_pluck_from_a_collection_of_collections() + { + $data = Collection::make([ + collect(['id' => 1, 'name' => 'matt', 'hobby' => 'coding']), + collect(['id' => 2, 'name' => 'tomo', 'hobby' => 'cooking']), + ]); + + $this->assertEquals($data->map->only(['name', 'hobby'])->map->values(), $data->pluckManyValues(['name', 'hobby'])); + } + + /** @test */ + public function it_can_pluck_from_array_and_object_items() + { + $data = Collection::make([ + (object) ['id' => 1, 'name' => 'matt', 'hobby' => 'coding'], + ['id' => 2, 'name' => 'tomo', 'hobby' => 'cooking'], + ]); + + $this->assertEquals( + [ + (object) ['matt', 'coding'], + ['tomo', 'cooking'], + ], + $data->pluckManyValues(['name', 'hobby'])->all() + ); + } + + /** @test */ + public function it_can_pluck_from_objects_that_implement_array_access_interface() + { + $data = Collection::make([ + new TestArrayAccessImplementation(['id' => 1, 'name' => 'marco', 'hobby' => 'drinking']), + new TestArrayAccessImplementation(['id' => 2, 'name' => 'belle', 'hobby' => 'cross-stitch']), + ]); + + $this->assertEquals( + [ + ['marco', 'drinking'], + ['belle', 'cross-stitch'], + ], + $data->pluckManyValues(['name', 'hobby'])->all() + ); + } +} +