/** * Read config from xml * * @param string $xmlString * @return $this */ public function readFromXml($xmlString) { $this->xmlReader->fromString($xmlString); while ($this->xmlReader->read()) { if ($this->xmlReader->isGroup()) { $groupTitle = $this->xmlReader->getAttribute('title'); $groupIdentifier = $this->xmlReader->getElementIdentifier(); $this->propertyGroups[$groupIdentifier] = new PropertyGroup($groupTitle); } elseif ($this->xmlReader->isMultipleProperty()) { $property = new Property(); $property->setName($this->xmlReader->getAttribute('name')); $property->setQuestion($this->xmlReader->getElementValue()); $property->setType(Property::TYPE_MULTIPLE); $this->propertyGroups[$groupIdentifier]->addProperty($property); } elseif ($this->xmlReader->isBooleanProperty()) { $property = new Property(); $property->setName($this->xmlReader->getAttribute('name')); $property->setQuestion($this->xmlReader->getElementValue()); $property->setType(Property::TYPE_BOOL); $this->propertyGroups[$groupIdentifier]->addProperty($property); } elseif ($this->xmlReader->isProperty()) { $property = new Property(); $property->setName($this->xmlReader->getAttribute('name')); $property->setQuestion($this->xmlReader->getElementValue()); $property->setType(Property::TYPE_MIXED); $this->propertyGroups[$groupIdentifier]->addProperty($property); } elseif ($this->xmlReader->isPropertyWithOptions()) { $property = new Property(); $property->setName($this->xmlReader->getAttribute('name')); $property->setQuestion($this->xmlReader->getAttribute('title')); foreach ($this->xmlReader->getPropertyOptions() as $value => $name) { $property->addOption($name, $value); } $this->propertyGroups[$groupIdentifier]->addProperty($property); $this->xmlReader->next(); } } return $this; }
/** * @depends testReadingGroupWithProperty */ public function testReadingGroupWithPropertyWithOptions() { $xml = ' <config> <group title="Test Group"> <property title="Make a choice" name="testPropertyWithOption"> <option value="10">First option</option> <option value="20">Second option</option> <option value="30">Third option</option> </property> </group> </config> '; $this->reader->fromString($xml); $this->reader->read(); $this->reader->read(); $this->reader->read(); $this->assertTrue($this->reader->isPropertyWithOptions(), 'Element is expected to be a property with options.'); $this->assertEquals('testPropertyWithOption', $this->reader->getAttribute('name')); $this->assertEquals('Make a choice', $this->reader->getAttribute('title')); $expectedOptions = array('First option' => '10', 'Second option' => '20', 'Third option' => '30'); $this->assertEquals($expectedOptions, $this->reader->getPropertyOptions()); }