Selaa lähdekoodia

adds handler for PhpCollection\Map

Johannes M. Schmitt 12 vuotta sitten
vanhempi
commit
778542ba8b

+ 16 - 0
src/JMS/Serializer/Handler/PhpCollectionHandler.php

@@ -21,6 +21,7 @@ namespace JMS\Serializer\Handler;
 use JMS\Serializer\Context;
 use JMS\Serializer\GraphNavigator;
 use JMS\Serializer\VisitorInterface;
+use PhpCollection\Map;
 use PhpCollection\Sequence;
 
 class PhpCollectionHandler implements SubscribingHandlerInterface
@@ -31,6 +32,7 @@ class PhpCollectionHandler implements SubscribingHandlerInterface
         $formats = array('json', 'xml', 'yml');
         $collectionTypes = array(
             'PhpCollection\Sequence' => 'Sequence',
+            'PhpCollection\Map' => 'Map',
         );
 
         foreach ($collectionTypes as $type => $shortName) {
@@ -54,6 +56,20 @@ class PhpCollectionHandler implements SubscribingHandlerInterface
         return $methods;
     }
 
+    public function serializeMap(VisitorInterface $visitor, Map $map, array $type, Context $context)
+    {
+        $type['name'] = 'array';
+
+        return $visitor->visitArray(iterator_to_array($map), $type, $context);
+    }
+
+    public function deserializeMap(VisitorInterface $visitor, $data, array $type, Context $context)
+    {
+        $type['name'] = 'array';
+
+        return new Map($visitor->visitArray($data, $type, $context));
+    }
+
     public function serializeSequence(VisitorInterface $visitor, Sequence $sequence, array $type, Context $context)
     {
         // We change the base type, and pass through possible parameters.

+ 15 - 0
tests/JMS/Serializer/Tests/Fixtures/BlogPost.php

@@ -20,11 +20,13 @@ namespace JMS\Serializer\Tests\Fixtures;
 
 use JMS\Serializer\Annotation\Type;
 use JMS\Serializer\Annotation\SerializedName;
+use JMS\Serializer\Annotation\XmlMap;
 use JMS\Serializer\Annotation\XmlRoot;
 use JMS\Serializer\Annotation\XmlAttribute;
 use JMS\Serializer\Annotation\XmlList;
 use JMS\Serializer\Annotation\Groups;
 use Doctrine\Common\Collections\ArrayCollection;
+use PhpCollection\Map;
 use PhpCollection\Sequence;
 
 /** @XmlRoot("blog-post") */
@@ -64,6 +66,12 @@ class BlogPost
      */
     private $comments2;
 
+    /**
+     * @Type("PhpCollection\Map<string,string>")
+     * @XmlMap(keyAttribute = "key")
+     */
+    private $metadata;
+
     /**
      * @Type("JMS\Serializer\Tests\Fixtures\Author")
      * @Groups({"post"})
@@ -77,6 +85,8 @@ class BlogPost
         $this->published = false;
         $this->comments = new ArrayCollection();
         $this->comments2 = new Sequence();
+        $this->metadata = new Map();
+        $this->metadata->set('foo', 'bar');
         $this->createdAt = $createdAt;
     }
 
@@ -85,6 +95,11 @@ class BlogPost
         $this->published = true;
     }
 
+    public function getMetadata()
+    {
+        return $this->metadata;
+    }
+
     public function addComment(Comment $comment)
     {
         $this->comments->add($comment);

+ 2 - 2
tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php

@@ -49,8 +49,8 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['array_floats'] = '[1.34,3,6.42]';
             $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
             $outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
-            $outputs['blog_post'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"comments2":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"author":{"full_name":"Foo Bar"}}';
-            $outputs['blog_post_unauthored'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[],"comments2":[],"author":null}';
+            $outputs['blog_post'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"comments2":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"metadata":{"foo":"bar"},"author":{"full_name":"Foo Bar"}}';
+            $outputs['blog_post_unauthored'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[],"comments2":[],"metadata":{"foo":"bar"},"author":null}';
             $outputs['price'] = '{"price":3}';
             $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
             $outputs['order'] = '{"cost":{"price":12.34}}';

+ 3 - 0
tests/JMS/Serializer/Tests/Serializer/xml/blog_post.xml

@@ -13,6 +13,9 @@
     </author>
     <text><![CDATA[foo]]></text>
   </comment2>
+  <metadata>
+    <entry key="foo"><![CDATA[bar]]></entry>
+  </metadata>
   <author>
     <full_name><![CDATA[Foo Bar]]></full_name>
   </author>

+ 2 - 0
tests/JMS/Serializer/Tests/Serializer/yml/blog_post.yml

@@ -11,5 +11,7 @@ comments2:
         author:
             full_name: 'Foo Bar'
         text: foo
+metadata:
+    foo: bar
 author:
     full_name: 'Foo Bar'

+ 2 - 0
tests/JMS/Serializer/Tests/Serializer/yml/blog_post_unauthored.yml

@@ -1,4 +1,6 @@
 title: 'This is a nice title.'
 created_at: '2011-07-30T00:00:00+0000'
 is_published: false
+metadata:
+    foo: bar
 author: null