Skip to content

Commit e6ae1df

Browse files
committed
feat: Add toCollection() method
1 parent 0a75a5f commit e6ae1df

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Bag/Concerns/WithCollections.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public static function collect(iterable $values = []): Collection
2828

2929
return ($collection)::make($values)->map(fn ($value): Bag => $value instanceof Bag ? $value : static::from($value));
3030
}
31+
32+
public function toCollection(): Collection
33+
{
34+
return Collection::make($this->toArray());
35+
}
3136
}

tests/Feature/Concerns/WithCollectionsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Bag\Internal\Cache;
99
use Tests\Fixtures\Collections\BagWithCollectionCollection;
1010
use Tests\Fixtures\Values\BagWithCollection;
11+
use Tests\Fixtures\Values\TestBag;
1112

1213
covers(WithCollections::class, Collection::class, CollectionAttribute::class);
1314

@@ -54,3 +55,13 @@
5455
->and($bag->age)->toBe($data[$index]['age']);
5556
});
5657
});
58+
59+
test('it can be cast to a collection', function () {
60+
$value = TestBag::from([
61+
'name' => 'Davey Shafik',
62+
'age' => 40,
63+
'email' => 'davey@php.net'
64+
])->toCollection();
65+
66+
expect($value->toArray())->toBe(['name' => 'Davey Shafik', 'age' => 40, 'email' => 'davey@php.net']);
67+
});

0 commit comments

Comments
 (0)