Skip to content

Commit

Permalink
php-cs-fix, small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
the-pulli committed Sep 28, 2023
1 parent 1a73cb8 commit 577e6f1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 70 deletions.
9 changes: 4 additions & 5 deletions src/Macros/MapToCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Closure;
use Illuminate\Support\Collection;

class MapToCollection {
class MapToCollection
{
public function __invoke(): Closure
{
return function (array $ary, $deep = false): Collection {
Expand All @@ -14,10 +15,8 @@ public function __invoke(): Closure
$ary = Collection::mapToCollection($ary, $deep);
}

if ($deep) {
if (is_object($ary) && method_exists($ary, 'toArray')) {
$ary = Collection::mapToCollection($ary->toArray(), $deep);
}
if ($deep && is_object($ary) && method_exists($ary, 'toArray')) {
$ary = Collection::mapToCollection($ary->toArray(), $deep);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/Macros/RecursiveToArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RecursiveToArray
{
public function __invoke(): Closure
{
return function(array $ary): array {
return function (array $ary): array {
$closure = function (&$ary) {
if (is_array($ary)) {
$ary = Collection::recursiveToArray($ary);
Expand Down
64 changes: 32 additions & 32 deletions tests/Macros/MapToCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,38 @@
});

it('wraps nested arrays and objects into collection objects', function () {
$data = Collection::mapToCollection([
new ParentObject(
name: 'parent1',
children: Collection::make([new ChildObject(name: 'child1')]),
other: Collection::make([new OtherObject(value: 'other1')]),
),
new ParentObject(
name: 'parent2',
children: Collection::make([new ChildObject(name: 'child2')]),
other: Collection::make([new OtherObject(value: 'other2')]),
),
], true);
$data = Collection::mapToCollection([
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']),
]),
]),
]));
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']),
]),
]),
]));
});
});
64 changes: 32 additions & 32 deletions tests/Macros/RecursiveToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@

describe('recursiveToArray macro', function () {
it('wraps nested collection and objects into array', function () {
$data = Collection::recursiveToArray([
new ParentObject(
name: 'parent1',
children: Collection::make([new ChildObject(name: 'child1')]),
other: Collection::make([new OtherObject(value: 'other1')]),
),
new ParentObject(
name: 'parent2',
children: Collection::make([new ChildObject(name: 'child2')]),
other: Collection::make([new OtherObject(value: 'other2')]),
),
]);
$data = Collection::recursiveToArray([
new ParentObject(
name: 'parent1',
children: Collection::make([new ChildObject(name: 'child1')]),
other: Collection::make([new OtherObject(value: 'other1')]),
),
new ParentObject(
name: 'parent2',
children: Collection::make([new ChildObject(name: 'child2')]),
other: Collection::make([new OtherObject(value: 'other2')]),
),
]);

expect($data)->toBe([
[
'name' => 'parent1',
'children' => [
['name' => 'child1'],
],
'other' => [
['value' => 'other1'],
],
],
[
'name' => 'parent2',
'children' => [
['name' => 'child2'],
],
'other' => [
['value' => 'other2'],
],
],
]);
expect($data)->toBe([
[
'name' => 'parent1',
'children' => [
['name' => 'child1'],
],
'other' => [
['value' => 'other1'],
],
],
[
'name' => 'parent2',
'children' => [
['name' => 'child2'],
],
'other' => [
['value' => 'other2'],
],
],
]);
});
});

0 comments on commit 577e6f1

Please sign in to comment.