Przeglądaj źródła

Merge pull request #125 from matthiasnoback/fix_nested_element_with_attribute_and_value

Added fix for nested element with attribute and value
Johannes 13 lat temu
rodzic
commit
71643cb9c0

+ 2 - 0
Serializer/XmlSerializationVisitor.php

@@ -203,6 +203,8 @@ class XmlSerializationVisitor extends AbstractSerializationVisitor
                 $this->currentNode->appendChild($element);
             }
         }
+
+        $this->hasValue = false;
     }
 
     public function endVisitingObject(ClassMetadata $metadata, $data, $type)

+ 26 - 0
Tests/Fixtures/Person.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace JMS\SerializerBundle\Tests\Fixtures;
+
+use JMS\SerializerBundle\Annotation\XmlAttribute;
+use JMS\SerializerBundle\Annotation\XmlValue;
+use JMS\SerializerBundle\Annotation\XmlRoot;
+use JMS\SerializerBundle\Annotation\Type;
+
+/**
+ * @XmlRoot("child")
+ */
+class Person
+{
+    /**
+     * @Type("string")
+     * @XmlValue
+     */
+    public $name;
+
+    /**
+     * @Type("integer")
+     * @XmlAttribute
+     */
+    public $age;
+}

+ 30 - 0
Tests/Fixtures/PersonCollection.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace JMS\SerializerBundle\Tests\Fixtures;
+
+use JMS\SerializerBundle\Annotation\XmlRoot;
+use JMS\SerializerBundle\Annotation\XmlList;
+use Doctrine\Common\Collections\ArrayCollection;
+use JMS\SerializerBundle\Annotation\Type;
+
+/**
+ * @XmlRoot("person_collection")
+ */
+class PersonCollection
+{
+    /**
+     * @Type("ArrayCollection<JMS\SerializerBundle\Tests\Fixtures\Person>")
+     * @XmlList(entry = "person", inline = true)
+     */
+    public $persons;
+
+    /**
+     * @Type("string")
+     */
+    public $location;
+
+    public function __construct()
+    {
+        $this->persons = new ArrayCollection;
+    }
+}

+ 22 - 0
Tests/Fixtures/PersonLocation.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace JMS\SerializerBundle\Tests\Fixtures;
+
+use JMS\SerializerBundle\Annotation\XmlRoot;
+use JMS\SerializerBundle\Annotation\Type;
+
+/**
+ * @XmlRoot("person_location")
+ */
+class PersonLocation
+{
+    /**
+     * @Type("JMS\SerializerBundle\Tests\Fixtures\Person")
+     */
+    public $person;
+
+    /**
+     * @Type("string")
+     */
+    public $location;
+}

+ 27 - 0
Tests/Serializer/XmlSerializationTest.php

@@ -22,6 +22,9 @@ use JMS\SerializerBundle\Tests\Fixtures\InvalidUsageOfXmlValue;
 use JMS\SerializerBundle\Exception\InvalidArgumentException;
 use JMS\SerializerBundle\Annotation\Type;
 use JMS\SerializerBundle\Annotation\XmlValue;
+use JMS\SerializerBundle\Tests\Fixtures\PersonCollection;
+use JMS\SerializerBundle\Tests\Fixtures\PersonLocation;
+use JMS\SerializerBundle\Tests\Fixtures\Person;
 
 class XmlSerializationTest extends BaseSerializationTest
 {
@@ -34,6 +37,30 @@ class XmlSerializationTest extends BaseSerializationTest
         $this->serialize($obj);
     }
 
+    public function testPropertyIsObjectWithAttributeAndValue()
+    {
+        $personCollection = new PersonLocation;
+        $person = new Person;
+        $person->name = 'Matthias Noback';
+        $person->age = 28;
+        $personCollection->person = $person;
+        $personCollection->location = 'The Netherlands';
+
+        $this->assertEquals($this->getContent('person_location'), $this->serialize($personCollection));
+    }
+
+    public function testPropertyIsCollectionOfObjectsWithAttributeAndValue()
+    {
+        $personCollection = new PersonCollection;
+        $person = new Person;
+        $person->name = 'Matthias Noback';
+        $person->age = 28;
+        $personCollection->persons->add($person);
+        $personCollection->location = 'The Netherlands';
+
+        $this->assertEquals($this->getContent('person_collection'), $this->serialize($personCollection));
+    }
+
     public function testExternalEntitiesAreDisabledByDefault()
     {
         $currentDir = getcwd();

+ 5 - 0
Tests/Serializer/xml/person_collection.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<person_collection>
+  <person age="28"><![CDATA[Matthias Noback]]></person>
+  <location><![CDATA[The Netherlands]]></location>
+</person_collection>

+ 5 - 0
Tests/Serializer/xml/person_location.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<person_location>
+  <person age="28"><![CDATA[Matthias Noback]]></person>
+  <location><![CDATA[The Netherlands]]></location>
+</person_location>