public function testAppend() { $xmlString = '<?xml version="1.0"?>' . "\n" . '<books>' . '<book title="Book Title" public="1999" publisher="Book Publisher">' . '<author><name>Author 01</name></author>' . '<author name="Author 02" />' . '</book>' . '</books>'; $xml = new SimpleXML('<books />'); $book = new SimpleXML('<book />'); $book->addAttribute('title', 'Book Title')->addAttribute('public', '1999')->addAttribute('publisher', 'Book Publisher'); $book->addChild('author')->addChild('name', 'Author 01'); $book->addChild('author')->addAttribute('name', 'Author 02'); $xml->append($book); $this->assertXmlStringEqualsXmlString($xmlString, $xml->asXml()); }
/** * Method returning the xml representation of this class * * @param string $name * @return SimpleXML */ public function toXml($name = null) { $this->emit('before', [$this]); $name = !empty($name) ? $name : $this->className(); if (null !== $this->_value) { $xml = new SimpleXML('<' . $name . '>' . $this->_value . '</' . $name . '>'); } else { $xml = new SimpleXML('<' . $name . ' />'); } foreach ($this->_properties as $key => $value) { if ($value instanceof \Zimbra\Enum\Base) { $xml->addAttribute($key, $value->value()); } elseif (is_bool($value)) { $xml->addAttribute($key, Text::boolToString($value)); } else { $xml->addAttribute($key, $value); } } if (count($this->_children)) { foreach ($this->_children as $key => $value) { if ($value instanceof \Zimbra\Struct\Base) { $xml->append($value->toXml($key), $value->GetXmlNamespace()); } elseif ($value instanceof \Zimbra\Enum\Base) { $xml->addChild($key, $value->value()); } elseif (is_bool($value)) { $xml->addChild($key, Text::boolToString($value)); } elseif (is_array($value)) { foreach ($value as $child) { if ($child instanceof \Zimbra\Struct\Base) { $xml->append($child->toXml($key), $child->GetXmlNamespace()); } elseif ($child instanceof \Zimbra\Enum\Base) { $xml->addChild($key, $child->value()); } elseif (is_bool($child)) { $xml->addChild($key, Text::boolToString($child)); } else { $xml->addChild($key, $child); } } } else { $xml->addChild($key, $value); } } } $this->emit('after.xml', [$xml]); return $xml; }
/** * Method returning the xml representation of this class * * @param string $name * @return SimpleXML */ public function toXml($name = null) { $name = empty($name) ? $this->requestName() : $name; $xml = new SimpleXML('<' . $name . ' />'); foreach ($this->_requests as $key => $request) { $requestXml = $request->toXml(); $requestXml->addAttribute('requestId', $key); $xml->append($requestXml, $request->getXmlNamespace()); } return $xml; }