-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
271 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/laravel-dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Laravel\Caster; | ||
|
||
use fab2s\Dt0\Caster\ArrayType; | ||
use fab2s\Dt0\Caster\CasterInterface; | ||
use fab2s\Dt0\Caster\ScalarType; | ||
use fab2s\Dt0\Caster\ScalarTypeCaster; | ||
use fab2s\Dt0\Dt0; | ||
use fab2s\Dt0\Exception\CasterException; | ||
use fab2s\Dt0\Property\Property; | ||
use Illuminate\Support\Collection; | ||
|
||
class CollectionOfCaster implements CasterInterface | ||
{ | ||
public readonly ArrayType|ScalarType|string $logicalType; | ||
protected ?ScalarTypeCaster $scalarTypeCaster; | ||
|
||
/** | ||
* @throws CasterException | ||
*/ | ||
public function __construct( | ||
/** @var class-string<Dt0|UnitEnum>|ScalarType|string */ | ||
public readonly ScalarType|string $type, | ||
) { | ||
if (is_string($type)) { | ||
$logicalType = match (true) { | ||
is_subclass_of($type, Dt0::class) => ArrayType::DT0, | ||
is_subclass_of($type, UnitEnum::class) => ArrayType::ENUM, | ||
default => ScalarType::tryFrom($type), | ||
}; | ||
} else { | ||
$logicalType = $type; | ||
} | ||
|
||
if (! $logicalType) { | ||
throw new CasterException('[' . Dt0::classBasename(static::class) . "] $type is not an ArrayType nor a ScalarType"); | ||
} | ||
|
||
$this->logicalType = $logicalType; | ||
$this->scalarTypeCaster = $this->logicalType instanceof ScalarType ? new ScalarTypeCaster($this->logicalType) : null; | ||
} | ||
|
||
public function cast(mixed $value): ?Collection | ||
{ | ||
if (! is_iterable($value)) { | ||
return null; | ||
} | ||
|
||
$result = Collection::make(); | ||
|
||
foreach ($value as $item) { | ||
$result->push(match ($this->logicalType) { | ||
ArrayType::DT0 => $this->type::tryFrom($item), | ||
ArrayType::ENUM => Property::tryEnum($this->type, $item), | ||
default => $this->scalarTypeCaster->cast($item), | ||
}); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/laravel-dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Laravel; | ||
|
||
use fab2s\Dt0\Laravel\Casts\Dt0Cast; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
|
||
trait LaravelDt0Trait | ||
{ | ||
public static function castUsing(array $arguments): CastsAttributes | ||
{ | ||
return new Dt0Cast(static::class, ...$arguments); | ||
} | ||
} |
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,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/laravel-dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Laravel\Tests\Caster; | ||
|
||
use Exception; | ||
use fab2s\Dt0\Caster\ScalarType; | ||
use fab2s\Dt0\Exception\CasterException; | ||
use fab2s\Dt0\Laravel\Caster\CollectionOfCaster; | ||
use fab2s\Dt0\Laravel\Tests\Artifacts\DumbDt0; | ||
use fab2s\Dt0\Laravel\Tests\TestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
class CollectionOfCasterTest extends TestCase | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
#[DataProvider('castProvider')] | ||
public function test_cast(ScalarType|string $type, $value, $expected): void | ||
{ | ||
$caster = new CollectionOfCaster($type); | ||
$casted = $caster->cast($value); | ||
|
||
$this->assertSame(json_encode($expected), json_encode($caster->cast($value))); | ||
} | ||
|
||
public function test_exception(): void | ||
{ | ||
$this->expectException(CasterException::class); | ||
new CollectionOfCaster('NotAType'); | ||
} | ||
|
||
public static function castProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'type' => DumbDt0::class, | ||
'value' => [ | ||
DumbDt0::make(prop1: 'ONE', prop2: 'ONE', prop3: 'ONE'), | ||
['prop1' => 'TWO', 'prop2' => 'TWO', 'prop3' => 'TWO'], | ||
'{"prop1":"three","prop2":"three","prop3":"three"}', | ||
], | ||
'expected' => collect([ | ||
DumbDt0::make(prop1: 'ONE', prop2: 'ONE', prop3: 'ONE'), | ||
DumbDt0::make(prop1: 'TWO', prop2: 'TWO', prop3: 'TWO'), | ||
DumbDt0::make(prop1: 'three', prop2: 'three', prop3: 'three'), | ||
]), | ||
], | ||
[ | ||
'type' => 'string', | ||
'value' => collect([ | ||
'ONE', | ||
'TWO', | ||
'three', | ||
]), | ||
'expected' => collect([ | ||
'ONE', | ||
'TWO', | ||
'three', | ||
]), | ||
], | ||
[ | ||
'type' => 'int', | ||
'value' => [ | ||
null, | ||
'42', | ||
42.42, | ||
'1337.1337', | ||
], | ||
'expected' => collect([ | ||
0, | ||
42, | ||
42, | ||
1337, | ||
]), | ||
], | ||
[ | ||
'type' => ScalarType::bool, | ||
'value' => null, | ||
'expected' => null, | ||
], | ||
]; | ||
} | ||
} |