DateTimeHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Serializer\Handler;
  18. use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
  19. use Symfony\Component\Yaml\Inline;
  20. use JMS\SerializerBundle\Serializer\YamlSerializationVisitor;
  21. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  22. use JMS\SerializerBundle\Exception\RuntimeException;
  23. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  24. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  25. use JMS\SerializerBundle\Serializer\VisitorInterface;
  26. use JMS\SerializerBundle\Serializer\GraphNavigator;
  27. class DateTimeHandler implements SubscribingHandlerInterface
  28. {
  29. private $defaultFormat;
  30. private $defaultTimezone;
  31. public static function getSubscribingMethods()
  32. {
  33. $methods = array();
  34. foreach (array('json', 'xml', 'yml') as $format) {
  35. $methods[] = array(
  36. 'type' => 'DateTime',
  37. 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
  38. 'format' => $format,
  39. );
  40. $methods[] = array(
  41. 'type' => 'DateTime',
  42. 'format' => $format,
  43. 'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
  44. 'method' => 'serializeDateTime',
  45. );
  46. }
  47. return $methods;
  48. }
  49. public function __construct($defaultFormat = \DateTime::ISO8601, $defaultTimezone = 'UTC')
  50. {
  51. $this->defaultFormat = $defaultFormat;
  52. $this->defaultTimezone = new \DateTimeZone($defaultTimezone);
  53. }
  54. public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, array $type)
  55. {
  56. return $visitor->visitString($date->format($this->getFormat($type)), $type);
  57. }
  58. public function deserializeDateTimeFromXml(XmlDeserializationVisitor $visitor, $data, array $type)
  59. {
  60. $attributes = $data->attributes();
  61. if (isset($attributes['nil'][0]) && (string) $attributes['nil'][0] === 'true') {
  62. return null;
  63. }
  64. return $this->parseDateTime($data, $type);
  65. }
  66. public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type)
  67. {
  68. if (null === $data) {
  69. return null;
  70. }
  71. return $this->parseDateTime($data, $type);
  72. }
  73. private function parseDateTime($data, array $type)
  74. {
  75. $timezone = isset($type['params'][1]) ? $type['params'][1] : $this->defaultTimezone;
  76. $datetime = \DateTime::createFromFormat($this->getFormat($type), (string) $data, $timezone);
  77. if (false === $datetime) {
  78. throw new RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $this->defaultFormat));
  79. }
  80. return $datetime;
  81. }
  82. /**
  83. * @return string
  84. * @param array $type
  85. */
  86. private function getFormat(array $type)
  87. {
  88. return isset($type['params'][0]) ? $type['params'][0] : $this->defaultFormat;
  89. }
  90. }