Skip to content

Commit

Permalink
Sfink pluck many values (#242)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
sfinktah and freekmurze authored May 11, 2023
1 parent 5f33d94 commit c95233e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The package will automatically register itself.
- [`parallelMap`](#parallelmap)
- [`path`](#path)
- [`pluckMany`](#pluckmany)
- [`pluckManyValues`](#pluckmanyvalues)
- [`pluckToArray`](#plucktoarray)
- [`prioritize`](#prioritize)
- [`recursive`](#recursive)
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/CollectionMacroServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/Macros/PluckMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions src/Macros/PluckManyValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\CollectionMacros\Macros;

use ArrayAccess;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

/**
* Get a Collection with only the specified keys.
*
* @param array $keys
*
* @mixin \Illuminate\Support\Collection
*
* @return Collection
*/
class PluckManyValues
{
public function __invoke()
{
return function ($keys): Collection {
// Allow passing multiple keys as multiple arguments
$keys = is_array($keys) ? $keys : func_get_args();
return $this->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));
});
};
}
}
62 changes: 62 additions & 0 deletions tests/Macros/PluckManyValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Spatie\CollectionMacros\Test\Macros;

use ArrayAccess;
use Illuminate\Support\Collection;
use Spatie\CollectionMacros\Test\TestCase;

class PluckManyValuesTest extends TestCase
{
/** @test */
public function it_provides_a_pluckManyValues_macro()
{
$this->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()
);
}
}

0 comments on commit c95233e

Please sign in to comment.