Explorar el Código

-- add XmlValue support

root hace 13 años
padre
commit
982f907c17

+ 27 - 0
Annotation/XmlValue.php

@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * Copyright 2011 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\SerializerBundle\Annotation;
+
+/**
+ * @Annotation
+ * @Target("PROPERTY")
+ */
+final class XmlValue
+{
+}

+ 3 - 0
Metadata/Driver/AnnotationDriver.php

@@ -22,6 +22,7 @@ use JMS\SerializerBundle\Annotation\XmlMap;
 use JMS\SerializerBundle\Annotation\XmlRoot;
 use JMS\SerializerBundle\Annotation\XmlAttribute;
 use JMS\SerializerBundle\Annotation\XmlList;
+use JMS\SerializerBundle\Annotation\XmlValue;
 use JMS\SerializerBundle\Annotation\PostSerialize;
 use JMS\SerializerBundle\Annotation\PostDeserialize;
 use JMS\SerializerBundle\Annotation\PreSerialize;
@@ -96,6 +97,8 @@ class AnnotationDriver implements DriverInterface
                         $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
                     } else if ($annot instanceof XmlAttribute) {
                         $propertyMetadata->xmlAttribute = true;
+                    } else if ($annot instanceof XmlValue) {
+                        $propertyMetadata->xmlValue = true;
                     }
                 }
 

+ 4 - 0
Metadata/Driver/XmlDriver.php

@@ -109,6 +109,10 @@ class XmlDriver extends AbstractFileDriver
                     if (isset($pElem->attributes()->{'xml-attribute'})) {
                         $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
                     }
+                    
+                    if (isset($pElem->attributes()->{'xml-value'})) {
+                        $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'};
+                    }
 
                     if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
                         || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {

+ 4 - 0
Metadata/Driver/YamlDriver.php

@@ -98,6 +98,10 @@ class YamlDriver extends AbstractFileDriver
                     if (isset($pConfig['xml_attribute'])) {
                         $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
                     }
+                    
+                    if (isset($pConfig['xml_value'])) {
+                        $pMetadata->xmlValue = (Boolean) $pConfig['xml_value'];
+                    }
 
                     if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
                     || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {

+ 3 - 0
Metadata/PropertyMetadata.php

@@ -31,6 +31,7 @@ class PropertyMetadata extends BasePropertyMetadata
     public $xmlEntryName;
     public $xmlKeyAttribute;
     public $xmlAttribute = false;
+    public $xmlValue = false;
 
     public function serialize()
     {
@@ -44,6 +45,7 @@ class PropertyMetadata extends BasePropertyMetadata
             $this->xmlEntryName,
             $this->xmlKeyAttribute,
             $this->xmlAttribute,
+            $this->xmlValue,
             parent::serialize(),
         ));
     }
@@ -60,6 +62,7 @@ class PropertyMetadata extends BasePropertyMetadata
             $this->xmlEntryName,
             $this->xmlKeyAttribute,
             $this->xmlAttribute,
+            $this->xmlValue,
             $parentStr
         ) = unserialize($str);
 

+ 7 - 0
Serializer/XmlDeserializationVisitor.php

@@ -205,6 +205,13 @@ class XmlDeserializationVisitor extends AbstractVisitor
 
             return;
         }
+        
+        if ($metadata->xmlValue) {
+            $v = $this->navigator->accept($data, $metadata->type, $this);
+            $metadata->reflection->setValue($this->currentObject, $v);
+
+            return;
+        }
 
         if ($metadata->xmlCollection) {
             $enclosingElem = $data;

+ 36 - 0
Tests/Fixtures/Price.php

@@ -0,0 +1,36 @@
+<?php
+
+/*
+ * Copyright 2011 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\SerializerBundle\Tests\Fixtures;
+
+use JMS\SerializerBundle\Annotation\Type;
+use JMS\SerializerBundle\Annotation\XmlValue;
+
+class Price
+{
+    /**
+     * @Type("double")
+     * @XmlValue
+     */
+    private $price;
+    
+    function __construct($price)
+    {
+        $this->price = $price;
+    }
+}

+ 8 - 0
Tests/Metadata/Driver/BaseDriverTest.php

@@ -39,6 +39,14 @@ abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
         $p = new PropertyMetadata($m->name, 'author');
         $p->type = 'JMS\SerializerBundle\Tests\Fixtures\Author';
         $this->assertEquals($p, $m->propertyMetadata['author']);
+        
+        $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\Price'));
+        $this->assertNotNull($m);
+        
+        $p = new PropertyMetadata($m->name, 'price');
+        $p->type = 'double';
+        $p->xmlValue = true;
+        $this->assertEquals($p, $m->propertyMetadata['price']);
     }
 
     abstract protected function getDriver();

+ 13 - 0
Tests/Metadata/Driver/php/Price.php

@@ -0,0 +1,13 @@
+<?php
+
+use JMS\SerializerBundle\Metadata\ClassMetadata;
+use JMS\SerializerBundle\Metadata\PropertyMetadata;
+
+$metadata = new ClassMetadata('JMS\SerializerBundle\Tests\Fixtures\Price');
+
+$pMetadata = new PropertyMetadata('JMS\SerializerBundle\Tests\Fixtures\Price', 'price');
+$pMetadata->type = 'double';
+$pMetadata->xmlValue = true;
+$metadata->addPropertyMetadata($pMetadata);
+
+return $metadata;

+ 6 - 0
Tests/Metadata/Driver/xml/Price.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<serializer>
+    <class name="JMS\SerializerBundle\Tests\Fixtures\Price">
+        <property name="price" xml-value="true" type="double" />
+    </class>
+</serializer>

+ 5 - 0
Tests/Metadata/Driver/yml/Price.yml

@@ -0,0 +1,5 @@
+JMS\SerializerBundle\Tests\Fixtures\Price:
+    properties:
+        price:
+            type: double
+            xml_value: true