Selaa lähdekoodia

Fixed missing support for Accessor in YamlDriver

Phil Moorhouse 12 vuotta sitten
vanhempi
commit
8c3bd7d889

+ 2 - 1
Metadata/Driver/YamlDriver.php

@@ -158,7 +158,8 @@ class YamlDriver extends AbstractFileDriver
 
                     $pMetadata->setAccessor(
                         isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
-                        isset($pConfig['accessor']) ? $pConfig['accessor'] : null
+                        isset($pConfig['accessor']['getter']) ? $pConfig['accessor']['getter'] : null,
+                        isset($pConfig['accessor']['setter']) ? $pConfig['accessor']['setter'] : null
                     );
 
                     if (isset($pConfig['inline'])) {

+ 40 - 37
Resources/doc/reference/yml_reference.rst

@@ -1,37 +1,40 @@
-YAML Reference
---------------
-::
-
-    # MyBundle\Resources\config\serializer\ClassName.yml
-    Fully\Qualified\ClassName:
-        exclusion_policy: ALL
-        xml_root_name: foobar
-        exclude: true
-        access_type: public_method # defaults to property
-        accessor_order: custom
-        custom_accessor_order: [propertyName1, propertyName2, ..., propertyNameN]
-        properties:
-            some-property:
-                exclude: true
-                expose: true
-                access_type: public_method # defaults to property
-                type: string
-                serialized_name: foo
-                since_version: 1.0
-                until_version: 1.1
-                groups: [foo, bar]
-                xml_attribute: true
-                inline: true
-                read_only: true
-                xml_key_value_pairs: true
-                xml_list:
-                    inline: true
-                    entry_name: foo
-                xml_map:
-                    inline: true
-                    key_attribute_name: foo
-                    entry_name: bar
-        callback_methods:
-            pre_serialize: [foo, bar]
-            post_serialize: [foo, bar]
-            post_deserialize: [foo, bar]
+YAML Reference
+--------------
+::
+
+    # MyBundle\Resources\config\serializer\ClassName.yml
+    Fully\Qualified\ClassName:
+        exclusion_policy: ALL
+        xml_root_name: foobar
+        exclude: true
+        access_type: public_method # defaults to property
+        accessor_order: custom
+        custom_accessor_order: [propertyName1, propertyName2, ..., propertyNameN]
+        properties:
+            some-property:
+                exclude: true
+                expose: true
+                access_type: public_method # defaults to property
+                accessor: # access_type must be set to public_method
+                    getter: getSomeOtherProperty
+                    setter: setSomeOtherProperty
+                type: string
+                serialized_name: foo
+                since_version: 1.0
+                until_version: 1.1
+                groups: [foo, bar]
+                xml_attribute: true
+                inline: true
+                read_only: true
+                xml_key_value_pairs: true
+                xml_list:
+                    inline: true
+                    entry_name: foo
+                xml_map:
+                    inline: true
+                    key_attribute_name: foo
+                    entry_name: bar
+        callback_methods:
+            pre_serialize: [foo, bar]
+            post_serialize: [foo, bar]
+            post_deserialize: [foo, bar]

+ 12 - 0
Tests/Metadata/Driver/YamlDriverTest.php

@@ -57,6 +57,18 @@ class YamlDriverTest extends BaseDriverTest
         $this->assertEquals($p, $m->propertyMetadata['title']);
     }
 
+    public function testBlogPostAccessor()
+    {
+        $m = $this->getDriver('accessor')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
+
+        $this->assertArrayHasKey('title', $m->propertyMetadata);
+
+        $p = new PropertyMetadata($m->name, 'title');
+        $p->getter = 'getOtherTitle';
+        $p->setter = 'setOtherTitle';
+        $this->assertEquals($p, $m->propertyMetadata['title']);
+    }
+
     protected function getDriver()
     {
         $append = '';

+ 7 - 0
Tests/Metadata/Driver/yml/accessor/BlogPost.yml

@@ -0,0 +1,7 @@
+JMS\SerializerBundle\Tests\Fixtures\BlogPost:
+    xml_root_name: blog-post
+    properties:
+        title:
+            accessor:
+                getter: getOtherTitle
+                setter: setOtherTitle