| Total Complexity | 7 |
| Total Lines | 47 |
| Duplicated Lines | 0 % |
| Coverage | 93.75% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 15 | final class ChronosType extends ScalarType |
||
| 16 | { |
||
| 17 | public ?string $description = 'A date with time and timezone.'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Serializes an internal value to include in a response. |
||
| 21 | */ |
||
| 22 | 1 | public function serialize(mixed $value): mixed |
|
| 23 | { |
||
| 24 | 1 | if ($value instanceof Chronos) { |
|
| 25 | 1 | return $value->format('c'); |
|
| 26 | } |
||
| 27 | |||
| 28 | return $value; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Parses an externally provided value (query variable) to use as an input. |
||
| 33 | */ |
||
| 34 | 9 | public function parseValue(mixed $value): ?Chronos |
|
| 35 | { |
||
| 36 | 9 | if (!is_string($value)) { |
|
| 37 | 1 | throw new Error('Cannot represent value as Chronos date: ' . Utils::printSafe($value)); |
|
| 38 | } |
||
| 39 | |||
| 40 | 8 | if ($value === '') { |
|
| 41 | 2 | return null; |
|
| 42 | } |
||
| 43 | |||
| 44 | 6 | $date = new Chronos($value); |
|
| 45 | 6 | $date = $date->setTimezone(new DateTimeZone(date_default_timezone_get())); |
|
| 46 | |||
| 47 | 6 | return $date; |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Parses an externally provided literal value to use as an input (e.g. in Query AST). |
||
| 52 | */ |
||
| 53 | 5 | public function parseLiteral(Node $valueNode, ?array $variables = null): ?Chronos |
|
| 62 | } |
||
| 63 | } |
||
| 64 |