-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
344 additions
and
55 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,26 @@ | ||
<?php | ||
|
||
namespace Pulli\CollectionMacros; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CollectionMacroServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
Collection::make(self::macros()) | ||
->reject(fn ($class, $macro) => Collection::hasMacro($macro)) | ||
->each(fn ($class, $macro) => Collection::macro($macro, app($class)())); | ||
} | ||
|
||
public static function macros(): array | ||
{ | ||
return [ | ||
'mapToCollection' => \Pulli\CollectionMacros\Macros\MapToCollection::class, | ||
'mapToCollectionFrom' => \Pulli\CollectionMacros\Macros\MapToCollectionFrom::class, | ||
'recursiveToArray' => \Pulli\CollectionMacros\Macros\RecursiveToArray::class, | ||
'recursiveToArrayFrom' => \Pulli\CollectionMacros\Macros\RecursiveToArrayFrom::class, | ||
]; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Pulli\CollectionMacros\Macros; | ||
|
||
use Closure; | ||
use Illuminate\Support\Collection; | ||
|
||
class MapToCollectionFrom | ||
{ | ||
public function __invoke(): Closure | ||
{ | ||
return function (array $ary, bool $deep = false): Collection { | ||
$closure = function (&$ary) use ($deep) { | ||
if (is_array($ary)) { | ||
$ary = Collection::mapToCollectionFrom($ary, $deep); | ||
} | ||
|
||
if ($deep && is_object($ary) && method_exists($ary, 'toArray')) { | ||
$ary = Collection::mapToCollectionFrom($ary->toArray(), $deep); | ||
} | ||
}; | ||
|
||
array_walk($ary, $closure); | ||
|
||
return new Collection($ary); | ||
}; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Pulli\CollectionMacros\Macros; | ||
|
||
use Closure; | ||
use Illuminate\Support\Collection; | ||
|
||
class RecursiveToArrayFrom | ||
{ | ||
public function __invoke(): Closure | ||
{ | ||
return function (array $ary): array { | ||
$closure = function (&$ary) { | ||
if (is_array($ary)) { | ||
$ary = Collection::recursiveToArrayFrom($ary); | ||
} | ||
|
||
if (is_object($ary) && method_exists($ary, 'toArray')) { | ||
$ary = Collection::recursiveToArrayFrom($ary->toArray()); | ||
} | ||
}; | ||
|
||
array_walk($ary, $closure); | ||
|
||
return $ary; | ||
}; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Pulli\CollectionMacros\Test; | ||
|
||
use Orchestra\Testbench\TestCase as Orchestra; | ||
use Pulli\CollectionMacros\CollectionMacroServiceProvider; | ||
|
||
abstract class IntegrationTestCase extends Orchestra | ||
{ | ||
protected function getPackageProviders($app): array | ||
{ | ||
return [CollectionMacroServiceProvider::class]; | ||
} | ||
|
||
public function avoidTestMarkedAsRisky(): void | ||
{ | ||
$this->assertTrue(true); | ||
} | ||
} |
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,97 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Collection; | ||
use Pulli\CollectionMacros\Test\Data\ChildObject; | ||
use Pulli\CollectionMacros\Test\Data\OtherObject; | ||
use Pulli\CollectionMacros\Test\Data\ParentObject; | ||
use Pulli\CollectionMacros\Test\Data\TestObject; | ||
|
||
describe('mapToCollectionFrom macro', function () { | ||
it('wraps all arrays into collection objects', function () { | ||
$data = Collection::mapToCollectionFrom([ | ||
['test' => ['test' => '1.1']], | ||
['test' => ['test' => '1.2']], | ||
['test' => ['test' => ['test' => '1.3.1']]], | ||
]); | ||
|
||
expect($data[0])->toBeInstanceOf(Collection::class) | ||
->and($data[1])->toBeInstanceOf(Collection::class) | ||
->and($data[0]['test'])->toBeInstanceOf(Collection::class) | ||
->and($data[1]['test'])->toBeInstanceOf(Collection::class) | ||
->and($data->get(0)->get('test')->get('test'))->toBe('1.1') | ||
->and($data->get(1)->get('test')->get('test'))->toBe('1.2') | ||
->and($data->get(2)->get('test')->get('test')->get('test'))->toBe('1.3.1'); | ||
}); | ||
|
||
it('wraps data objects into collection objects', function () { | ||
$data = Collection::mapToCollectionFrom([ | ||
new TestObject('John Doe', 'Programmer'), | ||
new TestObject('Jane Doe', 'Designer'), | ||
], true); | ||
|
||
expect($data[0])->toBeInstanceOf(Collection::class) | ||
->and($data[1])->toBeInstanceOf(Collection::class) | ||
->and($data->get(0))->toEqual(new Collection([ | ||
'name' => 'John Doe', | ||
'job' => 'Programmer', | ||
])) | ||
->and($data->get(1))->toEqual(new Collection([ | ||
'name' => 'Jane Doe', | ||
'job' => 'Designer', | ||
])); | ||
}); | ||
|
||
it('wraps nested data objects into collection objects', function () { | ||
$data = Collection::mapToCollectionFrom([ | ||
['first' => new TestObject('John Doe', 'Programmer')], | ||
['second' => new TestObject('Jane Doe', 'Designer')], | ||
], true); | ||
|
||
expect($data[0])->toBeInstanceOf(Collection::class) | ||
->and($data[1])->toBeInstanceOf(Collection::class) | ||
->and($data->get(0)->get('first'))->toEqual(new Collection([ | ||
'name' => 'John Doe', | ||
'job' => 'Programmer', | ||
])) | ||
->and($data->get(1)->get('second'))->toEqual(new Collection([ | ||
'name' => 'Jane Doe', | ||
'job' => 'Designer', | ||
])); | ||
}); | ||
|
||
it('wraps nested arrays and objects into collection objects', function () { | ||
$data = Collection::mapToCollectionFrom([ | ||
new ParentObject( | ||
name: 'parent1', | ||
children: new Collection([new ChildObject(name: 'child1')]), | ||
other: new Collection([new OtherObject(value: 'other1')]), | ||
), | ||
new ParentObject( | ||
name: 'parent2', | ||
children: new Collection([new ChildObject(name: 'child2')]), | ||
other: new Collection([new OtherObject(value: 'other2')]), | ||
), | ||
], true); | ||
|
||
expect($data)->toEqual(new Collection([ | ||
new Collection([ | ||
'name' => 'parent1', | ||
'children' => new Collection([ | ||
new Collection(['name' => 'child1']), | ||
]), | ||
'other' => new Collection([ | ||
new Collection(['value' => 'other1']), | ||
]), | ||
]), | ||
new Collection([ | ||
'name' => 'parent2', | ||
'children' => new Collection([ | ||
new Collection(['name' => 'child2']), | ||
]), | ||
'other' => new Collection([ | ||
new Collection(['value' => 'other2']), | ||
]), | ||
]), | ||
])); | ||
}); | ||
}); |
Oops, something went wrong.