-
-
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.
* getDeeply method * register macro * add tests * update readme * Fix styling * fix description * fix docblock params Co-authored-by: michael-rubel <[email protected]>
- Loading branch information
1 parent
f9ad718
commit 1003341
Showing
4 changed files
with
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Spatie\CollectionMacros\Macros; | ||
|
||
use Illuminate\Support\Arr; | ||
|
||
/*** | ||
* Get an item from the collection with multidimensional data using "dot" notation. | ||
* | ||
* @param mixed $key | ||
* @param mixed $default | ||
* | ||
* @mixin \Illuminate\Support\Collection | ||
* | ||
* @return mixed | ||
*/ | ||
class GetDeeply | ||
{ | ||
public function __invoke() | ||
{ | ||
return function ($key, $default = null) { | ||
return Arr::get($this->items, $key, $default); | ||
}; | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Spatie\CollectionMacros\Test\Macros; | ||
|
||
use Illuminate\Support\Collection; | ||
use Spatie\CollectionMacros\Test\TestCase; | ||
|
||
class GetDeeplyTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_retrieves_item_from_collection() | ||
{ | ||
$collection = new Collection(['foo', 'bar']); | ||
|
||
$this->assertSame( | ||
'foo', | ||
$collection->getDeeply(0) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_retrieves_item_from_collection_using_dot_notation() | ||
{ | ||
$collection = new Collection([ | ||
'foo' => [ | ||
'bar' => [ | ||
'baz' => 100, | ||
], | ||
], | ||
]); | ||
|
||
$this->assertSame( | ||
100, | ||
$collection->getDeeply('foo.bar.baz') | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_doesnt_remove_item_from_collection() | ||
{ | ||
$collection = new Collection(['foo', 'bar']); | ||
|
||
$collection->getDeeply(0); | ||
|
||
$this->assertEquals( | ||
[ | ||
0 => 'foo', | ||
1 => 'bar', | ||
], | ||
$collection->all() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_default() | ||
{ | ||
$collection = new Collection([]); | ||
|
||
$this->assertSame( | ||
'foo', | ||
$collection->getDeeply(0, 'foo') | ||
); | ||
} | ||
} |