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 * * @return SimpleXML */ public function toXml() { $soapNamespace = $this->_version === self::SOAP_1_2 ? self::NS_SOAP_1_2 : self::NS_SOAP_1_1; $nsString = 'xmlns:env="' . $soapNamespace . '"'; foreach ($this->_namespaces as $key => $ns) { if ($key > 0) { $nsString .= sprintf(' xmlns:urn%d="%s"', $key, $ns); } else { $nsString .= sprintf(' xmlns:urn="%s"', $ns); } } $message = sprintf('<env:Envelope %s></env:Envelope>', $nsString); $xml = new SimpleXML($message); if (count($this->_headers)) { $header = $xml->addChild('Header')->addChild('context', null, 'urn:zimbra'); foreach ($this->_headers as $name => $value) { $header->addChild($name, $value); } } $body = $xml->addChild('Body'); $body->append($this->_body, $this->_request->getXmlNamespace()); return $xml; }
/** * 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; }