-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typedCollect helper function (#20)
Co-authored-by: Jérôme Gamez <jerome@gamez.name>
- Loading branch information
1 parent
d073e8e
commit 0ced0f6
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Gamez\Illuminate\Support\TypedCollection; | ||
|
||
if (!function_exists('typedCollect')) { | ||
/** | ||
* Create an anonymous typed collection | ||
* | ||
* @template TKey of array-key | ||
* @template TValue | ||
* | ||
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $value | ||
* @param string|string[] $types | ||
* @return \Illuminate\Support\Collection<TKey, TValue> | ||
*/ | ||
function typedCollect($value = [], string|array $types = []) | ||
{ | ||
$types = is_array($types) ? $types : [$types]; | ||
|
||
$collection = new class extends TypedCollection {}; | ||
|
||
$reflectionClass = new ReflectionClass($collection); | ||
$reflectionProperty = $reflectionClass->getProperty('allowedTypes'); | ||
$reflectionProperty->setValue($collection, $types); | ||
|
||
return $collection::make($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Gamez\Illuminate\Support\Tests; | ||
|
||
use DateTime; | ||
use DateTimeInterface; | ||
use Gamez\Illuminate\Support\Tests\ArrayableItem; | ||
use Gamez\Illuminate\Support\TypedCollection; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HelpersTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_cannot_be_created_with_an_unsupported_type_of_item() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
typedCollect([new DateTime(), 'string', new DateTime()], DateTimeInterface::class); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_be_created_with_supported_types() | ||
{ | ||
$typedCollection = typedCollect([new DateTime(), new DateTime()], DateTimeInterface::class); | ||
$this->assertInstanceOf(TypedCollection::class, $typedCollection); | ||
$this->addToAssertionCount(1); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_accept_an_array_of_types() | ||
{ | ||
$typedCollection = typedCollect([new DateTime(), new ArrayableItem()], [DateTimeInterface::class, ArrayableItem::class]); | ||
$this->assertInstanceOf(TypedCollection::class, $typedCollection); | ||
$this->addToAssertionCount(1); | ||
} | ||
} |