Skip to content

Commit

Permalink
Add support for simple types recognized by the get_debug_type() fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
jeromegamez committed Mar 17, 2024
1 parent 23d8229 commit fd60dd0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Added support for simple types recognized by the [`get_debug_type()` function](https://www.php.net/get-debug-type).

## 7.0.0 - 2024-03-17

- Added support for Laravel 11 ([#21](https://github.com/jeromegamez/typed-collection/issues/21))
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ try {
echo $e->getMessage().PHP_EOL;
}
/* Output:
Output: A People collection only accepts objects of the following type(s): Person.
Output: A People collection only accepts items of the following type(s): Person.
*/
```

Expand Down Expand Up @@ -88,6 +88,12 @@ Output: A People collection only accepts objects of the following type(s): Perso
*/
```

### Supported types

Supported types are class strings, like `Person::class`, or types recognized by the
[`get_debug_type()` function](https://www.php.net/get-debug-type), `int`, `float`,
`string`, `bool`, and `array`.

### Helper functions

The `typedCollect()` helper function enables you to dynamically create typed collections
Expand Down
8 changes: 5 additions & 3 deletions src/ChecksForValidTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait ChecksForValidTypes
{
/** @var list<class-string> */
/** @var list<class-string|non-empty-string> */
protected static $allowedTypes = [];

/**
Expand All @@ -26,14 +26,16 @@ protected function assertValidTypes(...$items): void
*/
protected function assertValidType($item): void
{
$itemType = get_debug_type($item);

foreach (static::$allowedTypes as $allowedType) {
if ($item instanceof $allowedType) {
if (($itemType === $allowedType) || ($item instanceof $allowedType)) {
return;
}
}

throw new InvalidArgumentException(sprintf(
'A %s collection only accepts objects of the following type(s): %s.',
'A %s collection only accepts items of the following type(s): %s.',
get_class($this), implode(', ', static::$allowedTypes)
));
}
Expand Down
12 changes: 12 additions & 0 deletions tests/LazyMixedTypeCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Gamez\Illuminate\Support\Tests;

use Gamez\Illuminate\Support\LazyTypedCollection;

final class LazyMixedTypeCollection extends LazyTypedCollection
{
protected static $allowedTypes = ['int', 'string', ArrayableItem::class];
}
15 changes: 15 additions & 0 deletions tests/LazyTypedCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use DateTimeImmutable;
use Gamez\Illuminate\Support\LazyTypedCollection;
use Gamez\Illuminate\Support\TypedCollection;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -97,4 +98,18 @@ public function items_are_untyped_when_mapped(): void

$this->assertEquals(['2022-04-25'], $mapped->toArray());
}

#[Test]
#[DoesNotPerformAssertions]
public function items_can_be_of_simple_type(): void
{
new LazyMixedTypeCollection([1, 'string', new ArrayableItem()]);
}

#[Test]
public function items_of_simple_type_can_be_rejected(): void
{
$this->expectException(InvalidArgumentException::class);
new LazyMixedTypeCollection([true]);
}
}
15 changes: 15 additions & 0 deletions tests/TypedCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,19 @@ public function items_are_untyped_when_mapped(): void

$this->assertEquals(['2022-04-25'], $mapped->toArray());
}

#[Test]
public function items_can_be_of_simple_type(): void
{
$collection = new class extends TypedCollection {
protected static $allowedTypes = ['int', 'bool', 'string'];
};

$collection->add(1);
$collection->add(true);
$collection->add('string');

$this->expectException(InvalidArgumentException::class);
$collection->add(new \stdClass());
}
}

0 comments on commit fd60dd0

Please sign in to comment.