QueryPath\DOMQuery::prepareInsert PHP Method

prepareInsert() protected method

This handles a variety of boilerplate tasks that need doing before an indeterminate object can be inserted into a DOM tree. - If item is a string, this is converted into a document fragment and returned. - If item is a DOMQuery, then all items are retrieved and converted into a document fragment and returned. - If the item is a DOMNode, it is imported into the current DOM if necessary. - If the item is a SimpleXMLElement, it is converted into a DOM node and then imported.
protected prepareInsert ( mixed $item ) : mixed
$item mixed Item to prepare for insert.
return mixed Returns the prepared item.
    protected function prepareInsert($item)
    {
        if (empty($item)) {
            return;
        } elseif (is_string($item)) {
            // If configured to do so, replace all entities.
            if ($this->options['replace_entities']) {
                $item = \QueryPath\Entities::replaceAllEntities($item);
            }
            $frag = $this->document->createDocumentFragment();
            try {
                set_error_handler(array('\\QueryPath\\ParseException', 'initializeFromError'), $this->errTypes);
                $frag->appendXML($item);
            } catch (Exception $e) {
                restore_error_handler();
                throw $e;
            }
            restore_error_handler();
            return $frag;
        } elseif ($item instanceof self) {
            if ($item->size() == 0) {
                return;
            }
            $frag = $this->document->createDocumentFragment();
            foreach ($item->matches as $m) {
                $frag->appendXML($item->document->saveXML($m));
            }
            return $frag;
        } elseif ($item instanceof \DOMNode) {
            if ($item->ownerDocument !== $this->document) {
                // Deep clone this and attach it to this document
                $item = $this->document->importNode($item, true);
            }
            return $item;
        } elseif ($item instanceof \SimpleXMLElement) {
            $element = dom_import_simplexml($item);
            return $this->document->importNode($element, true);
        }
        // What should we do here?
        //var_dump($item);
        throw new \QueryPath\Exception('Cannot prepare item of unsupported type: ' . gettype($item));
    }