/** * Iterates over a set of data and builds it as XML * * @param \DOMDocument $doc The document being built * @param String $parent The tag name of the parent element * @param Array|\Traversable $data An array or a traversable object * @param Boolean $root Whether the data being parsed is at the root level * @return DOMNode Returns the built node */ protected function iterate(\DOMDocument $doc, $parent, &$data, $root = FALSE) { $node = $this->createElement($doc, $parent); foreach ($data as $key => $value) { if (\r8\isEmpty($value)) { continue; } else { if (\r8\isBasic($value) && $value !== NULL) { $node->setAttribute(self::normalizeName($key), (string) $value); } else { if (is_array($value) || $value instanceof \Traversable) { $node->appendChild($this->iterate($doc, $key, $value, FALSE)); } else { if ($value instanceof \r8\iface\XMLBuilder) { $child = $this->createElement($doc, $key); $child->appendChild(\r8\XMLBuilder::buildNode($value, $doc)); $node->appendChild($child); } else { if (is_object($value)) { // If it is an object that supports "toString" if (\r8\respondTo($value, "__toString")) { $node->setAttribute(self::normalizeName($key), $value->__toString()); } else { $props = get_object_vars($value); $node->appendChild($this->iterate($doc, $key, $props, FALSE)); } } } } } } } return $node; }
/** * Creates and returns a new node to attach to a document * * @param \DOMDocument $doc The root document this node is being created for * @return \DOMElement Returns the created node */ public function buildNode(\DOMDocument $doc) { $parent = \r8\XMLBuilder::buildNode($this->parent, $doc); foreach ($this->children as $child) { $parent->appendChild(\r8\XMLBuilder::buildNode($child, $doc)); } return $parent; }
public function testBuildNode_error() { $doc = new \DOMDocument(); $builder = $this->getMock("r8\\iface\\XMLBuilder", array("buildNode")); $builder->expects($this->once())->method("buildNode")->with($this->isInstanceOf("DOMDocument"))->will($this->returnValue("invalid result")); try { \r8\XMLBuilder::buildNode($builder, $doc); $this->fail("An expected exception was not thrown"); } catch (\r8\Exception\Interaction $err) { $this->assertSame("XMLBuilder did not return a DOMNode object", $err->getMessage()); } }
/** * Creates and returns a new node to attach to a document * * @param \DOMDocument $doc The root document this node is being created for * @return \DOMElement Returns the created node */ public function buildNode(\DOMDocument $doc) { $parent = $doc->createDocumentFragment(); foreach ($this->children as $child) { $child = \r8\XMLBuilder::buildNode($child, $doc); if ($child instanceof \DOMNode) { $parent->appendChild($child); } } if (!$parent->hasChildNodes()) { return $doc->createTextNode(""); } return $parent; }
/** * Creates and returns a new node to attach to a document * * @param \DOMDocument $doc The root document this node is being created for * @return \DOMElement Returns the created node */ public function buildNode(\DOMDocument $doc) { // Put together the root soap envelope node with all the needed namespaces $soapEnv = $doc->createElementNS($this->namespace, "soap:Envelope"); // Add the soap header, if it has been defined if ($this->header) { $soapHeader = $doc->createElementNS($this->namespace, "Header"); $soapEnv->appendChild($soapHeader); $soapHeader->appendChild(\r8\XMLBuilder::buildNode($this->header, $doc)); } // Add the soap body node $soapBody = $doc->createElementNS($this->namespace, "Body"); $soapEnv->appendChild($soapBody); $soapBody->appendChild(\r8\XMLBuilder::buildNode($this->body, $doc)); return $soapEnv; }
/** * Creates and returns a new node to attach to a document * * @param \DOMDocument $doc The root document this node is being created for * @return \DOMElement Returns the created node */ public function buildNode(\DOMDocument $doc) { $node = parent::buildNode($doc); $node->appendChild(\r8\XMLBuilder::buildNode($this->builder, $doc)); return $node; }
/** * Recursively builds an XML tree * * @param \DOMDocument $doc The document being built * @param String $parent The tag name of the parent element * @param Mixed $data The data being pieced together * @param Boolean $root Whether the data being parsed is at the root level * This is used during iteration, for example, to ensure lists are * created properly * @return DOMNode Returns the built node */ protected function build(\DOMDocument $doc, $parent, &$data, $root = FALSE) { if (\r8\isEmpty($data)) { return $this->createElement($doc, $parent); } else { if (\r8\isBasic($data) && $data !== NULL) { $node = $this->createElement($doc, $parent); $node->appendChild($doc->createTextNode((string) $data)); } else { if (is_array($data) || $data instanceof \Traversable) { $node = $this->iterate($doc, $parent, $data, $root); } else { if ($data instanceof \r8\iface\XMLBuilder) { $node = $this->createElement($doc, $parent); $node->appendChild(\r8\XMLBuilder::buildNode($data, $doc)); } else { if (is_object($data)) { // If it is an object that supports "toString" if (\r8\respondTo($data, "__toString")) { $node = $this->createElement($doc, $parent); $node->appendChild($doc->createTextNode($data->__toString())); } else { $props = get_object_vars($data); $node = $this->iterate($doc, $parent, $props, $root); } } } } } } return $node; }