DateTimeHandler.php 3.4 KB

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