Total Complexity | 7 |
Total Lines | 62 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
14 | class ChronosType extends ScalarType |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | public $description = 'A date with time and timezone.'; |
||
20 | |||
21 | /** |
||
22 | * Serializes an internal value to include in a response. |
||
23 | * |
||
24 | * @param mixed $value |
||
25 | * |
||
26 | * @return mixed |
||
27 | */ |
||
28 | public function serialize($value) |
||
29 | { |
||
30 | if ($value instanceof Chronos) { |
||
31 | return $value->format('c'); |
||
32 | } |
||
33 | |||
34 | return $value; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Parses an externally provided value (query variable) to use as an input |
||
39 | * |
||
40 | * @param mixed $value |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function parseValue($value) |
||
45 | { |
||
46 | if (!is_string($value)) { // quite naive, but after all this is example |
||
47 | throw new \UnexpectedValueException('Cannot represent value as Chronos date: ' . Utils::printSafe($value)); |
||
48 | } |
||
49 | |||
50 | if ($value === '') { |
||
51 | return null; |
||
52 | } |
||
53 | |||
54 | $date = new Chronos($value); |
||
55 | $date = $date->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
||
56 | |||
57 | return $date; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Parses an externally provided literal value to use as an input (e.g. in Query AST) |
||
62 | * |
||
63 | * @param $ast Node |
||
64 | * |
||
65 | * @return null|string |
||
66 | */ |
||
67 | public function parseLiteral($ast, array $variables = null) |
||
76 | } |
||
77 | } |
||
78 |