ソースを参照

Add toArray and fromArray methods to the Serializer

Tyler Stroud 10 年 前
コミット
8291086ad9

+ 43 - 0
src/JMS/Serializer/Serializer.php

@@ -121,6 +121,49 @@ class Serializer implements SerializerInterface
         return $visitorResult;
     }
 
+    public function toArray($data, SerializationContext $context = null)
+    {
+        if (null === $context) {
+            $context = new SerializationContext();
+        }
+
+        $context->initialize(
+            'json',
+            $visitor = $this->serializationVisitors->get('json')->get(),
+            $this->navigator,
+            $this->factory
+        );
+
+        $visitor->setNavigator($this->navigator);
+        $this->navigator->accept($visitor->prepare($data), null, $context);
+
+        return (array) $visitor->getRoot();
+    }
+
+    public function fromArray($data, $type, DeserializationContext $context = null)
+    {
+        if (null === $context) {
+            $context = new DeserializationContext();
+        }
+
+        $context->initialize(
+            'json',
+            $visitor = $this->deserializationVisitors->get('json')->get(),
+            $this->navigator,
+            $this->factory
+        );
+
+        $visitor->setNavigator($this->navigator);
+        $navigatorResult = $this->navigator->accept($data, $this->typeParser->parse($type), $context);
+
+        // This is a special case if the root is handled by a callback on the object iself.
+        if ((null === $visitorResult = $visitor->getResult()) && null !== $navigatorResult) {
+            return $navigatorResult;
+        }
+
+        return $visitorResult;
+    }
+
     /**
      * @return MetadataFactoryInterface
      */

+ 87 - 0
tests/JMS/Serializer/Tests/Serializer/ArrayTest.php

@@ -0,0 +1,87 @@
+<?php
+
+/*
+ * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace JMS\Serializer\Tests\Serializer;
+
+use JMS\Serializer\Handler\HandlerRegistry;
+use JMS\Serializer\Tests\Fixtures\Order;
+use JMS\Serializer\Tests\Fixtures\Price;
+use PhpCollection\Map;
+use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
+use Metadata\MetadataFactory;
+use JMS\Serializer\Metadata\Driver\AnnotationDriver;
+use Doctrine\Common\Annotations\AnnotationReader;
+use JMS\Serializer\Construction\UnserializeObjectConstructor;
+use JMS\Serializer\JsonSerializationVisitor;
+use JMS\Serializer\JsonDeserializationVisitor;
+use JMS\Serializer\Serializer;
+use JMS\Serializer\Naming\CamelCaseNamingStrategy;
+
+class ArrayTest extends \PHPUnit_Framework_TestCase
+{
+    protected $serializer;
+
+    public function setUp()
+    {
+        $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
+
+        $this->serializer = new Serializer(
+            new MetadataFactory(new AnnotationDriver(new AnnotationReader())),
+            new HandlerRegistry(),
+            new UnserializeObjectConstructor(),
+            new Map(array('json' => new JsonSerializationVisitor($namingStrategy))),
+            new Map(array('json' => new JsonDeserializationVisitor($namingStrategy)))
+        );
+    }
+
+    public function testToArray()
+    {
+        $order = new Order(new Price(5));
+
+        $expected = array(
+            'cost' => array(
+                'price' => 5
+            )
+        );
+
+        $result = $this->serializer->toArray($order);
+
+        $this->assertEquals($expected, $result);
+    }
+
+    public function testFromArray()
+    {
+        $data = array(
+            'cost' => array(
+                'price' => 2.5
+            )
+        );
+
+        $expected = new Order(new Price(2.5));
+        $result = $this->serializer->fromArray($data, 'JMS\Serializer\Tests\Fixtures\Order');
+
+        $this->assertEquals($expected, $result);
+    }
+
+    public function testToArrayReturnsArrayObjectAsArray()
+    {
+        $result = $this->serializer->toArray(new \ArrayObject());
+
+        $this->assertEquals(array(), $result);
+    }
+}