/** * Append to content nodes to the target nodes. * * @param array|\Traversable $contentNodes * @return array new nodes */ public function appendChildren($contentNodes) { $result = array(); if ($this->_node instanceof \DOMElement) { foreach ($contentNodes as $contentNode) { /** @var \DOMNode $contentNode */ if (Constraints::isNode($contentNode)) { $result[] = $this->_node->appendChild($contentNode->cloneNode(TRUE)); } } } return $result; }
/** * Convert a given content string into and array of nodes * * @param string $xml * @param string $contentType * @param boolean $includeTextNodes * @param integer $limit * @throws \UnexpectedValueException * @return array */ public function getFragment($xml, $contentType = 'text/xml', $includeTextNodes = TRUE, $limit = -1) { $xml = $this->getContentAsString($xml); $loader = $this->getOwner()->loaders(); if (!$loader->supports($contentType)) { throw new Exceptions\InvalidFragmentLoader(get_class($loader)); } if (!$xml) { return array(); } $result = array(); $fragment = $loader->loadFragment($xml, $contentType, $this->getOwner()->getLoadingOptions($contentType)); if ($fragment) { $fragment = $this->getOwner()->document->importNode($fragment, TRUE); for ($i = $fragment->childNodes->length - 1; $i >= 0; $i--) { $element = $fragment->childNodes->item($i); if ($element instanceof \DOMElement || $includeTextNodes && Constraints::isNode($element)) { array_unshift($result, $element); $element->parentNode->removeChild($element); } } } return $this->getLimitedArray($result, $limit); }
/** * Push new element(s) an the internal element list * * @param \DOMNode|\Traversable|array|NULL $elements * @param boolean $ignoreTextNodes ignore text nodes * @throws \OutOfBoundsException * @throws \InvalidArgumentException * @return $this */ public function push($elements, $ignoreTextNodes = FALSE) { if (Constraints::isNode($elements, $ignoreTextNodes)) { if ($elements->ownerDocument !== $this->_document) { throw new \OutOfBoundsException('Node is not a part of this document'); } $this->_nodes[] = $elements; } elseif ($nodes = Constraints::isNodeList($elements)) { $this->_useDocumentContext = FALSE; foreach ($nodes as $index => $node) { if ($node->ownerDocument !== $this->_document) { throw new \OutOfBoundsException(sprintf('Node #%d is not a part of this document', $index)); } $this->_nodes[] = $node; } } elseif (NULL !== $elements) { throw new \InvalidArgumentException('Invalid elements variable.'); } return $this; }
/** * Wrap the inner child contents of each matched element * (including text nodes) with an XML structure. * * @example wrapInner.php Usage Example: FluentDOM\Query::wrapInner() * @param string|array|\DOMNode|\Traversable $content * @return Query */ public function wrapInner($content) { $elements = array(); foreach ($this->_nodes as $node) { foreach ($node->childNodes as $childNode) { if (Constraints::isNode($childNode)) { $elements[] = $childNode; } } } return $this->spawn($this->wrapNodes($elements, $content)); }