Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StringToDateTime | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |
0.00% |
0 / 1 |
| convert | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Converters; |
| 3 | |
| 4 | use Apie\Core\Utils\ConverterUtils; |
| 5 | use Apie\TypeConverter\ConverterInterface; |
| 6 | use DateTime; |
| 7 | use DateTimeInterface; |
| 8 | use ReflectionType; |
| 9 | |
| 10 | /** |
| 11 | * @implements ConverterInterface<string, DateTimeInterface> |
| 12 | */ |
| 13 | class StringToDateTime implements ConverterInterface |
| 14 | { |
| 15 | public function convert(string $input, ?ReflectionType $wantedType): ?DateTimeInterface |
| 16 | { |
| 17 | $class = ConverterUtils::toReflectionClass($wantedType); |
| 18 | assert(null !== $class); |
| 19 | $className = $class->name; |
| 20 | return match ($className) { |
| 21 | DateTimeInterface::class => new DateTime($input), |
| 22 | default => new $className($input), |
| 23 | }; |
| 24 | } |
| 25 | } |