-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
5f33d94
commit c95233e
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} | ||
|