Bladeren bron

Merge pull request #5 from shima5/dateinterval

Add DateInterval serialization to DateHandler formerly DateTimeHandler
Johannes 12 jaren geleden
bovenliggende
commit
8208d71dd8

+ 57 - 7
src/JMS/Serializer/Handler/DateTimeHandler.php

@@ -28,7 +28,7 @@ use JMS\Serializer\XmlSerializationVisitor;
 use JMS\Serializer\VisitorInterface;
 use JMS\Serializer\GraphNavigator;
 
-class DateTimeHandler implements SubscribingHandlerInterface
+class DateHandler implements SubscribingHandlerInterface
 {
     private $defaultFormat;
     private $defaultTimezone;
@@ -36,6 +36,8 @@ class DateTimeHandler implements SubscribingHandlerInterface
     public static function getSubscribingMethods()
     {
         $methods = array();
+        $types = array('DateTime', 'DateInterval');
+
         foreach (array('json', 'xml', 'yml') as $format) {
             $methods[] = array(
                 'type' => 'DateTime',
@@ -43,12 +45,14 @@ class DateTimeHandler implements SubscribingHandlerInterface
                 'format' => $format,
             );
 
-            $methods[] = array(
-                'type' => 'DateTime',
-                'format' => $format,
-                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
-                'method' => 'serializeDateTime',
-            );
+            foreach ($types as $type) {
+                $methods[] = array(
+                    'type' => $type,
+                    'format' => $format,
+                    'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
+                    'method' => 'serialize'.$type,
+                );
+            }
         }
 
         return $methods;
@@ -65,6 +69,13 @@ class DateTimeHandler implements SubscribingHandlerInterface
         return $visitor->visitString($date->format($this->getFormat($type)), $type);
     }
 
+    public function serializeDateInterval(VisitorInterface $visitor, \DateInterval $date, array $type)
+    {
+        $iso8601DateIntervalString = $this->format($date);
+
+        return $visitor->visitString($iso8601DateIntervalString, $type);
+    }
+
     public function deserializeDateTimeFromXml(XmlDeserializationVisitor $visitor, $data, array $type)
     {
         $attributes = $data->attributes();
@@ -103,4 +114,43 @@ class DateTimeHandler implements SubscribingHandlerInterface
     {
         return isset($type['params'][0]) ? $type['params'][0] : $this->defaultFormat;
     }
+
+    /**
+     * @param \DateInterval $dateInterval
+     * @return string
+     */
+    public function format(\DateInterval $dateInterval)
+    {
+        $format = 'P';
+
+        if (0 < $dateInterval->y) {
+            $format .= $dateInterval->y.'Y';
+        }
+
+        if (0 < $dateInterval->m) {
+            $format .= $dateInterval->m.'M';
+        }
+
+        if (0 < $dateInterval->d) {
+            $format .= $dateInterval->d.'D';
+        }
+
+        if (0 < $dateInterval->h || 0 < $dateInterval->i || 0 < $dateInterval->s) {
+            $format .= 'T';
+        }
+
+        if (0 < $dateInterval->h) {
+            $format .= $dateInterval->h.'H';
+        }
+
+        if (0 < $dateInterval->i) {
+            $format .= $dateInterval->i.'M';
+        }
+
+        if (0 < $dateInterval->s) {
+            $format .= $dateInterval->s.'S';
+        }
+
+        return $format;
+    }
 }

+ 2 - 2
src/JMS/Serializer/SerializerBuilder.php

@@ -12,7 +12,7 @@ use Metadata\Driver\DriverChain;
 use JMS\Serializer\Metadata\Driver\YamlDriver;
 use JMS\Serializer\Metadata\Driver\XmlDriver;
 use Metadata\Driver\FileLocator;
-use JMS\Serializer\Handler\DateTimeHandler;
+use JMS\Serializer\Handler\DateHandler;
 use JMS\Serializer\Handler\ArrayCollectionHandler;
 use JMS\Serializer\Construction\ObjectConstructorInterface;
 use JMS\Serializer\EventDispatcher\Subscriber\DoctrineProxySubscriber;
@@ -90,7 +90,7 @@ class SerializerBuilder
     public function addDefaultHandlers()
     {
         $this->handlersConfigured = true;
-        $this->handlerRegistry->registerSubscribingHandler(new DateTimeHandler());
+        $this->handlerRegistry->registerSubscribingHandler(new DateHandler());
         $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler());
 
         return $this;

+ 9 - 2
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -30,7 +30,7 @@ use JMS\Serializer\Metadata\Driver\AnnotationDriver;
 use JMS\Serializer\Construction\UnserializeObjectConstructor;
 use JMS\Serializer\Handler\ArrayCollectionHandler;
 use JMS\Serializer\Handler\ConstraintViolationHandler;
-use JMS\Serializer\Handler\DateTimeHandler;
+use JMS\Serializer\Handler\DateHandler;
 use JMS\Serializer\Handler\FormErrorHandler;
 use JMS\Serializer\JsonDeserializationVisitor;
 use JMS\Serializer\JsonSerializationVisitor;
@@ -267,6 +267,13 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+    public function testDateInterval()
+    {
+        $duration = new \DateInterval('PT45M');
+
+        $this->assertEquals($this->getContent('date_interval'), $this->serializer->serialize($duration, $this->getFormat()));
+    }
+
     public function testBlogPost()
     {
         $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
@@ -646,7 +653,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
         $this->handlerRegistry = new HandlerRegistry();
         $this->handlerRegistry->registerSubscribingHandler(new ConstraintViolationHandler());
-        $this->handlerRegistry->registerSubscribingHandler(new DateTimeHandler());
+        $this->handlerRegistry->registerSubscribingHandler(new DateHandler());
         $this->handlerRegistry->registerSubscribingHandler(new FormErrorHandler(new IdentityTranslator(new MessageSelector())));
         $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler());
         $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'AuthorList', $this->getFormat(),

+ 25 - 0
tests/JMS/Serializer/Tests/Serializer/DateIntervalFormatTest.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace JMS\Serializer\Tests\Serializer;
+
+use JMS\Serializer\Handler\DateHandler;
+
+class DateIntervalFormatTest extends \PHPUnit_Framework_TestCase
+{
+    public function testFormat()
+    {
+        $dtf = new DateHandler();
+
+        $iso8601DateIntervalString = $dtf->format(new \DateInterval('PT45M'));
+
+        $this->assertEquals($iso8601DateIntervalString, 'PT45M');
+
+        $iso8601DateIntervalString = $dtf->format(new \DateInterval('P2YT45M'));
+
+        $this->assertEquals($iso8601DateIntervalString, 'P2YT45M');
+
+        $iso8601DateIntervalString = $dtf->format(new \DateInterval('P2Y4DT6H8M16S'));
+
+        $this->assertEquals($iso8601DateIntervalString, 'P2Y4DT6H8M16S');
+    }
+}

+ 1 - 0
tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php

@@ -85,6 +85,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['object_when_null'] = '{"text":"foo"}';
             $outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}';
             $outputs['date_time'] = '"2011-08-30T00:00:00+0000"';
+            $outputs['date_interval'] = '"PT45M"';
         }
 
         if (!isset($outputs[$key])) {

+ 2 - 0
tests/JMS/Serializer/Tests/Serializer/xml/date_interval.xml

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result><![CDATA[PT45M]]></result>

+ 1 - 0
tests/JMS/Serializer/Tests/Serializer/yml/date_interval.yml

@@ -0,0 +1 @@
+PT45M