| @@ 12-50 (lines=39) @@ | ||
| 9 | namespace Youshido\GraphQL\Type\Scalar; |
|
| 10 | ||
| 11 | ||
| 12 | class DateTimeType extends AbstractScalarType |
|
| 13 | { |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param $value \DateTime |
|
| 17 | * @return null|string |
|
| 18 | */ |
|
| 19 | public function serialize($value) |
|
| 20 | { |
|
| 21 | if ($value === null) { |
|
| 22 | return null; |
|
| 23 | } |
|
| 24 | ||
| 25 | return $value->format('Y-m-d H:i:s'); |
|
| 26 | } |
|
| 27 | ||
| 28 | public function isValidValue($value) |
|
| 29 | { |
|
| 30 | if (is_object($value)) { |
|
| 31 | return true; |
|
| 32 | } |
|
| 33 | ||
| 34 | $d = \DateTime::createFromFormat('Y-m-d H:i:s', $value); |
|
| 35 | ||
| 36 | return $d && $d->format('Y-m-d H:i:s') == $value; |
|
| 37 | } |
|
| 38 | ||
| 39 | public function getDescription() |
|
| 40 | { |
|
| 41 | return 'Representation of date and time in "Y-m-d H:i:s" format'; |
|
| 42 | } |
|
| 43 | ||
| 44 | public function getName() |
|
| 45 | { |
|
| 46 | return 'DateTime'; |
|
| 47 | } |
|
| 48 | ||
| 49 | ||
| 50 | } |
|
| @@ 12-44 (lines=33) @@ | ||
| 9 | namespace Youshido\GraphQL\Type\Scalar; |
|
| 10 | ||
| 11 | ||
| 12 | class DateType extends AbstractScalarType |
|
| 13 | { |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param $value \DateTime |
|
| 17 | * @return null|string |
|
| 18 | */ |
|
| 19 | public function serialize($value) |
|
| 20 | { |
|
| 21 | if ($value === null) { |
|
| 22 | return null; |
|
| 23 | } |
|
| 24 | ||
| 25 | return $value->format('Y-m-d'); |
|
| 26 | } |
|
| 27 | ||
| 28 | public function isValidValue($value) |
|
| 29 | { |
|
| 30 | if (is_object($value)) { |
|
| 31 | return true; |
|
| 32 | } |
|
| 33 | ||
| 34 | $d = \DateTime::createFromFormat('Y-m-d', $value); |
|
| 35 | ||
| 36 | return $d && $d->format('Y-m-d') == $value; |
|
| 37 | } |
|
| 38 | ||
| 39 | public function getDescription() |
|
| 40 | { |
|
| 41 | return 'Representation of date in "Y-m-d" format'; |
|
| 42 | } |
|
| 43 | ||
| 44 | } |
|
| @@ 12-50 (lines=39) @@ | ||
| 9 | namespace Youshido\GraphQL\Type\Scalar; |
|
| 10 | ||
| 11 | ||
| 12 | class DateTimeTzType extends AbstractScalarType |
|
| 13 | { |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param $value \DateTime |
|
| 17 | * @return null|string |
|
| 18 | */ |
|
| 19 | public function serialize($value) |
|
| 20 | { |
|
| 21 | if ($value === null) { |
|
| 22 | return null; |
|
| 23 | } |
|
| 24 | ||
| 25 | return $value->format('r'); |
|
| 26 | } |
|
| 27 | ||
| 28 | public function isValidValue($value) |
|
| 29 | { |
|
| 30 | if (is_object($value)) { |
|
| 31 | return true; |
|
| 32 | } |
|
| 33 | ||
| 34 | $d = \DateTime::createFromFormat('r', $value); |
|
| 35 | ||
| 36 | return $d && $d->format('r') == $value; |
|
| 37 | } |
|
| 38 | ||
| 39 | public function getDescription() |
|
| 40 | { |
|
| 41 | return 'Representation of date and time in "r" format'; |
|
| 42 | } |
|
| 43 | ||
| 44 | public function getName() |
|
| 45 | { |
|
| 46 | return 'DateTimeTz'; |
|
| 47 | } |
|
| 48 | ||
| 49 | ||
| 50 | } |
|