Skip to content

Commit d921cdb

Browse files
authored
Allow strings|int in collection constructor (#25)
1 parent dbe7eae commit d921cdb

File tree

8 files changed

+68
-4
lines changed

8 files changed

+68
-4
lines changed

src/Domain/Value/Field/FieldCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ final class FieldCollection implements \Countable, \IteratorAggregate
2626
private array $items = [];
2727

2828
/**
29-
* @param list<Field> $items
29+
* @param list<Field|string> $items
3030
*/
3131
public function __construct(
3232
array $items = [],
3333
) {
3434
foreach ($items as $item) {
35+
if (\is_string($item)) {
36+
$item = new Field($item);
37+
}
38+
3539
$this->add($item);
3640
}
3741
}

src/Domain/Value/IdCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ final class IdCollection implements \Countable, \IteratorAggregate
2626
private array $items = [];
2727

2828
/**
29-
* @param list<Id> $items
29+
* @param list<Id|int> $items
3030
*/
3131
public function __construct(
3232
array $items = [],
3333
) {
3434
foreach ($items as $item) {
35+
if (\is_int($item)) {
36+
$item = new Id($item);
37+
}
38+
3539
$this->add($item);
3640
}
3741
}

src/Domain/Value/Resolver/RelationCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ final class RelationCollection implements \Countable, \IteratorAggregate
2626
private array $items = [];
2727

2828
/**
29-
* @param list<Relation> $items
29+
* @param list<Relation|string> $items
3030
*/
3131
public function __construct(
3232
array $items = [],
3333
) {
3434
foreach ($items as $item) {
35+
if (\is_string($item)) {
36+
$item = new Relation($item);
37+
}
38+
3539
$this->add($item);
3640
}
3741
}

src/Domain/Value/Tag/TagCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ final class TagCollection implements \Countable, \IteratorAggregate
2626
private array $items = [];
2727

2828
/**
29-
* @param list<Tag> $items
29+
* @param list<string|Tag> $items
3030
*/
3131
public function __construct(
3232
array $items = [],
3333
) {
3434
foreach ($items as $item) {
35+
if (\is_string($item)) {
36+
$item = new Tag($item);
37+
}
38+
3539
$this->add($item);
3640
}
3741
}

tests/Unit/Domain/Value/Field/FieldCollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ final class FieldCollectionTest extends TestCase
2525
{
2626
use FakerTrait;
2727

28+
/**
29+
* @test
30+
*/
31+
public function constructWithString(): void
32+
{
33+
$faker = self::faker();
34+
$collection = new FieldCollection([$faker->word(), $faker->word()]);
35+
36+
self::assertCount(2, $collection);
37+
self::assertContainsOnlyInstancesOf(Field::class, $collection);
38+
}
39+
2840
/**
2941
* @test
3042
*/

tests/Unit/Domain/Value/IdCollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ final class IdCollectionTest extends TestCase
2525
{
2626
use FakerTrait;
2727

28+
/**
29+
* @test
30+
*/
31+
public function constructWithString(): void
32+
{
33+
$faker = self::faker();
34+
$collection = new IdCollection([$faker->numberBetween(1), $faker->numberBetween(1)]);
35+
36+
self::assertCount(2, $collection);
37+
self::assertContainsOnlyInstancesOf(Id::class, $collection);
38+
}
39+
2840
/**
2941
* @test
3042
*/

tests/Unit/Domain/Value/Resolver/RelationCollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ final class RelationCollectionTest extends TestCase
2525
{
2626
use FakerTrait;
2727

28+
/**
29+
* @test
30+
*/
31+
public function constructWithString(): void
32+
{
33+
$faker = self::faker();
34+
$collection = new RelationCollection([$faker->relation(), $faker->relation()]);
35+
36+
self::assertCount(2, $collection);
37+
self::assertContainsOnlyInstancesOf(Relation::class, $collection);
38+
}
39+
2840
/**
2941
* @test
3042
*/

tests/Unit/Domain/Value/Tag/TagCollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ final class TagCollectionTest extends TestCase
2525
{
2626
use FakerTrait;
2727

28+
/**
29+
* @test
30+
*/
31+
public function constructWithString(): void
32+
{
33+
$faker = self::faker();
34+
$collection = new TagCollection([$faker->word(), $faker->word()]);
35+
36+
self::assertCount(2, $collection);
37+
self::assertContainsOnlyInstancesOf(Tag::class, $collection);
38+
}
39+
2840
/**
2941
* @test
3042
*/

0 commit comments

Comments
 (0)