ソースを参照

infer accessor order if not explicitly set in YML

The accessor order is now inferred if a ``custom_accessor_order`` is given and
``accessor_order`` has not been explicitly defined:

```
SomeClass:
    accessor_order: custom # Can be ommitted now if custom_accessor_order is set.
    custom_accessor_order: ["a", "b", "c"]
```
Johannes M. Schmitt 11 年 前
コミット
740c63a780

+ 4 - 0
src/JMS/Serializer/Metadata/Driver/YamlDriver.php

@@ -224,6 +224,10 @@ class YamlDriver extends AbstractFileDriver
 
 
     private function addClassProperties(ClassMetadata $metadata, array $config)
     private function addClassProperties(ClassMetadata $metadata, array $config)
     {
     {
+        if (isset($config['custom_accessor_order']) && ! isset($config['accessor_order'])) {
+            $config['accessor_order'] = 'custom';
+        }
+
         if (isset($config['accessor_order'])) {
         if (isset($config['accessor_order'])) {
             $metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
             $metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
         }
         }

+ 6 - 0
tests/JMS/Serializer/Tests/Metadata/Driver/YamlDriverTest.php

@@ -24,6 +24,12 @@ use JMS\Serializer\Metadata\Driver\YamlDriver;
 
 
 class YamlDriverTest extends BaseDriverTest
 class YamlDriverTest extends BaseDriverTest
 {
 {
+    public function testAccessorOrderIsInferred()
+    {
+        $m = $this->getDriverForSubDir('accessor_inferred')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
+        $this->assertEquals(array('age', 'name'), array_keys($m->propertyMetadata));
+    }
+
     public function testShortExposeSyntax()
     public function testShortExposeSyntax()
     {
     {
         $m = $this->getDriverForSubDir('short_expose')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
         $m = $this->getDriverForSubDir('short_expose')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));

+ 6 - 0
tests/JMS/Serializer/Tests/Metadata/Driver/yml/accessor_inferred/Person.yml

@@ -0,0 +1,6 @@
+JMS\Serializer\Tests\Fixtures\Person:
+    custom_accessor_order: ["age", "name"]
+
+    properties:
+        age: ~
+        name: ~