-
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
8 changed files
with
182 additions
and
3 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,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/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\Caster; | ||
|
||
use fab2s\Dt0\Dt0; | ||
use fab2s\Dt0\Exception\CasterException; | ||
use fab2s\Dt0\Exception\Dt0Exception; | ||
use fab2s\Dt0\Property\Property; | ||
use JsonException; | ||
use UnitEnum; | ||
|
||
class ArrayOfTypeCaster implements CasterInterface | ||
{ | ||
public readonly ArrayType|ScalarType|string $logicalType; | ||
private ?ScalarTypeCaster $scalarTypeCaster; | ||
|
||
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(Dt0::class, $type) => ArrayType::DT0, | ||
is_subclass_of(UnitEnum::class, $type) => 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; | ||
} | ||
|
||
/** | ||
* @throws Dt0Exception | ||
* @throws JsonException | ||
*/ | ||
public function cast(mixed $value): ?array | ||
{ | ||
if (! is_array($value)) { | ||
return null; | ||
} | ||
|
||
$result = []; | ||
foreach ($value as $item) { | ||
$result[] = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/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\Caster; | ||
|
||
enum ArrayType: string | ||
{ | ||
case ENUM = 'enum'; | ||
case DT0 = 'dt0'; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/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\Caster; | ||
|
||
enum ScalarType: string | ||
{ | ||
case boolean = 'boolean'; | ||
case bool = 'bool'; | ||
case integer = 'integer'; | ||
case int = 'int'; | ||
case float = 'float'; | ||
case double = 'double'; | ||
case string = 'string'; | ||
case null = 'null'; | ||
case resource = 'resource'; | ||
} |
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,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/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\Caster; | ||
|
||
use fab2s\Dt0\Dt0; | ||
use fab2s\Dt0\Exception\CasterException; | ||
|
||
class ScalarTypeCaster implements CasterInterface | ||
{ | ||
public readonly ?ScalarType $type; | ||
|
||
public function __construct( | ||
ScalarType|string $type, | ||
) { | ||
if (is_string($type) && ! ($type = ScalarType::tryFrom($type))) { | ||
throw new CasterException('[' . Dt0::classBasename(static::class) . "] $type is not ScalarType"); | ||
} | ||
|
||
$this->type = $type; | ||
|
||
} | ||
|
||
/** | ||
* @param scalar $value | ||
* | ||
* @return string|int|float|bool|null|resource | ||
*/ | ||
public function cast(mixed $value): mixed | ||
{ | ||
if (! is_scalar($value)) { | ||
return null; | ||
} | ||
|
||
return match ($this->type) { | ||
ScalarType::resource => is_resource($value) ? $value : null, | ||
default => settype($value, $this->type?->value) ? $value : null, | ||
}; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/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\Exception; | ||
|
||
class CasterException extends Dt0Exception | ||
{ | ||
} |
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