DateTimeHandler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 JMS\SerializerBundle\Serializer\GraphNavigator;
  20. use Symfony\Component\Yaml\Inline;
  21. use JMS\SerializerBundle\Serializer\YamlSerializationVisitor;
  22. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  23. use JMS\SerializerBundle\Serializer\GenericDeserializationVisitor;
  24. use JMS\SerializerBundle\Exception\RuntimeException;
  25. use JMS\SerializerBundle\Serializer\GenericSerializationVisitor;
  26. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  27. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  28. use JMS\SerializerBundle\Serializer\VisitorInterface;
  29. class DateTimeHandler implements SubscribingHandlerInterface
  30. {
  31. private $defaultFormat;
  32. private $defaultTimezone;
  33. public static function getSubscribingMethods()
  34. {
  35. $methods = array();
  36. foreach (array('json', 'xml', 'yml') as $format) {
  37. $methods[] = array(
  38. 'type' => 'DateTime',
  39. 'format' => $format,
  40. );
  41. }
  42. return $methods;
  43. }
  44. public function __construct($defaultFormat = \DateTime::ISO8601, $defaultTimezone = 'UTC')
  45. {
  46. $this->defaultFormat = $defaultFormat;
  47. $this->defaultTimezone = new \DateTimeZone($defaultTimezone);
  48. }
  49. public function serializeDateTimeToXml(XmlSerializationVisitor $visitor, \DateTime $date, array $type)
  50. {
  51. if (null === $visitor->document) {
  52. $visitor->document = $visitor->createDocument(null, null, true);
  53. }
  54. return $visitor->document->createTextNode($date->format($this->getFormat($type)));
  55. }
  56. public function serializeDateTimeToYml(YamlSerializationVisitor $visitor, \DateTime $date, array $type)
  57. {
  58. return Inline::dump($date->format($this->getFormat($type)));
  59. }
  60. public function serializeDateTimeToJson(JsonSerializationVisitor $visitor, \DateTime $date, array $type)
  61. {
  62. return $date->format($this->getFormat($type));
  63. }
  64. public function deserializeDateTimeFromXml(XmlDeserializationVisitor $visitor, $data, array $type)
  65. {
  66. return $this->parseDateTime($data, $type);
  67. }
  68. public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type)
  69. {
  70. return $this->parseDateTime($data, $type);
  71. }
  72. private function parseDateTime($data, array $type)
  73. {
  74. $timezone = isset($type['params'][1]) ? $type['params'][1] : $this->defaultTimezone;
  75. $datetime = \DateTime::createFromFormat($this->getFormat($type), (string) $data, $timezone);
  76. if (false === $datetime) {
  77. throw new RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $this->defaultFormat));
  78. }
  79. return $datetime;
  80. }
  81. private function getFormat(array $type)
  82. {
  83. return isset($type['params'][0]) ? $type['params'][0] : $this->defaultFormat;
  84. }
  85. }