Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions src/Domain/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ class Collection extends BaseCollection
private int $perPage;

/**
* The type of item in the collection.
* The type of each items in the collection.
*
* @var string
*/
protected string $typeOf = 'mixed';
protected string $valueType = 'mixed';

/**
* The type of each keys in the collection.
*
* @var string
*/
protected string $keyType = 'integer';

/**
* Collection constructor.
Expand Down Expand Up @@ -85,24 +92,52 @@ protected function invariantAmountOfItemsMustBeLessOrEqualsThanTotalItems(): boo
* @return bool
* @throws InvariantViolation
*/
protected function invariantItemsMustBeOfSameType(): bool
protected function invariantItemsMustMatchTheRequiredType(): bool
{
$primitives = ['integer', 'boolean', 'float', 'string', 'array', 'object', 'callable'];
if ($this->typeOf !== 'mixed') {
$check = in_array($this->typeOf, $primitives)
? fn($value): bool => gettype($value) !== $this->typeOf
: fn($value): bool => !($value instanceof $this->typeOf);
if ($this->valueType !== 'mixed') {
$check = in_array($this->valueType, $primitives)
? fn($value): bool => gettype($value) !== $this->valueType
: fn($value): bool => !($value instanceof $this->valueType);

foreach ($this->items as $index => $item) {
if ($check($item)) {
throw new InvariantViolation("All items must be type of {$this->typeOf}");
throw new InvariantViolation("All items must be type of {$this->valueType}");
}
}
}

return true;
}

/**
* Invariant: Check the collection keys to match the required type.
*
* Supported types:
* - string
* - integer
*
* @return bool
* @throws InvariantViolation
*/
protected function invariantKeysMustMatchTheRequiredType(): bool
{
$supported = ['string', 'integer'];
if (!in_array($this->keyType, $supported)) {
throw new InvariantViolation(
"Unsupported key type, must be one of ".implode(', ', $supported)
);
}

foreach ($this->items as $index => $item) {
if (gettype($index) !== $this->keyType) {
throw new InvariantViolation("All keys must be type of {$this->keyType}");
}
}

return true;
}

/**
* Return the total amount of items on the data source.
*
Expand Down
24 changes: 21 additions & 3 deletions tests/Domain/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CollectionTest extends MockeryTestCase
public function testShouldSuccessfullyInstantiateACollection(): void
{
$c = new class (['foo', 'bar']) extends Collection {
protected string $typeOf = 'string';
protected string $valueType = 'string';
};

$this->assertInstanceOf(Collection::class, $c);
Expand All @@ -31,7 +31,7 @@ public function testShouldFailWithWrongPrimitiveValueItemTypes(): void
$this->expectException(InvariantViolation::class);

new class ([1, '2']) extends Collection {
protected string $typeOf = 'integer';
protected string $valueType = 'integer';
};
}

Expand All @@ -40,7 +40,7 @@ public function testShouldFailWithWrongClassValueItemTypes(): void
$this->expectException(InvariantViolation::class);

new class ([new stdClass(), '2']) extends Collection {
protected string $typeOf = stdClass::class;
protected string $valueType = stdClass::class;
};
}

Expand All @@ -57,4 +57,22 @@ public function testShouldFailDueToAmountOfItemsIsGreaterThanTotalItems(): void

new Collection([0, 1, 2, 3, 4], 2, 5);
}

public function testShouldFailDueToUnsupportedKeyType(): void
{
$this->expectException(InvariantViolation::class);

new class (['foo', 'bar']) extends Collection {
protected string $keyType = 'boolean';
};
}

public function testShouldFailDueToWrongKeyType(): void
{
$this->expectException(InvariantViolation::class);

new class (['foo', 'bar']) extends Collection {
protected string $keyType = 'string';
};
}
}