Browse Source

Merge pull request #108 from jaapio/accessOrder

Accessor order
Johannes 12 years ago
parent
commit
6c0dfb8eaf

+ 22 - 0
doc/reference/annotations.rst

@@ -147,6 +147,28 @@ default the order is undefined, but you may change it to either "alphabetical",
         private $name;
     }
 
+    /**
+     * @AccessorOrder("custom", custom = {"name", "SomeMethod" ,"id"})
+     *
+     * Resulting Property Order: name, mood, id
+     */
+    class User
+    {
+        private $id;
+        private $name;
+
+        /**
+         * @Serializer\VirtualProperty
+         * @Serializer\SerializedName("mood")
+         *
+         * @return string
+         */
+        public function getSomeMethod()
+        {
+            return 'happy';
+        }
+    }
+
 @VirtualProperty
 ~~~~~~~~~~~~~~~~
 This annotation can be defined on a method to indicate that the data returned by

+ 38 - 0
tests/JMS/Serializer/Tests/Fixtures/AccessorOrderMethod.php

@@ -0,0 +1,38 @@
+<?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\Fixtures;
+
+use JMS\Serializer\Annotation as Serializer;
+
+/** @Serializer\AccessorOrder("custom",  custom = {"method", "b", "a"}) */
+class AccessorOrderMethod
+{
+    private $b = 'b', $a = 'a';
+
+    /**
+     * @Serializer\VirtualProperty
+     * @Serializer\SerializedName("foo")
+     *
+     * @return string
+     */
+    public function getMethod()
+    {
+        return 'c';
+    }
+}

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

@@ -50,6 +50,7 @@ use JMS\Serializer\XmlSerializationVisitor;
 use JMS\Serializer\YamlSerializationVisitor;
 use JMS\Serializer\Tests\Fixtures\AccessorOrderChild;
 use JMS\Serializer\Tests\Fixtures\AccessorOrderParent;
+use JMS\Serializer\Tests\Fixtures\AccessorOrderMethod;
 use JMS\Serializer\Tests\Fixtures\Author;
 use JMS\Serializer\Tests\Fixtures\AuthorList;
 use JMS\Serializer\Tests\Fixtures\AuthorReadOnly;
@@ -549,6 +550,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
     {
         $this->assertEquals($this->getContent('accessor_order_child'), $this->serialize(new AccessorOrderChild()));
         $this->assertEquals($this->getContent('accessor_order_parent'), $this->serialize(new AccessorOrderParent()));
+        $this->assertEquals($this->getContent('accessor_order_methods'), $this->serialize(new AccessorOrderMethod()));
     }
 
     public function testGroups()

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

@@ -67,6 +67,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['mixed_access_types'] = '{"id":1,"name":"Johannes","read_only_property":42}';
             $outputs['accessor_order_child'] = '{"c":"c","d":"d","a":"a","b":"b"}';
             $outputs['accessor_order_parent'] = '{"a":"a","b":"b"}';
+            $outputs['accessor_order_methods'] = '{"foo":"c","b":"b","a":"a"}';
             $outputs['inline'] = '{"c":"c","a":"a","b":"b","d":"d"}';
             $outputs['groups_all'] = '{"foo":"foo","foobar":"foobar","bar":"bar","none":"none"}';
             $outputs['groups_foo'] = '{"foo":"foo","foobar":"foobar"}';

+ 6 - 0
tests/JMS/Serializer/Tests/Serializer/xml/accessor_order_methods.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <foo><![CDATA[c]]></foo>
+  <b><![CDATA[b]]></b>
+  <a><![CDATA[a]]></a>
+</result>

+ 3 - 0
tests/JMS/Serializer/Tests/Serializer/yml/accessor_order_methods.yml

@@ -0,0 +1,3 @@
+foo: c
+b: b
+a: a