You are here

protected function QueryPath::prepareInsert in QueryPath 7.3

Same name and namespace in other branches
  1. 6 QueryPath/QueryPath.php \QueryPath::prepareInsert()
  2. 7.2 QueryPath/QueryPath.php \QueryPath::prepareInsert()

Prepare an item for insertion into a DOM.

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 QueryPath, then the first item is retrieved and this call function is called recursivel.
  • 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.

Parameters

mixed $item: Item to prepare for insert.

Return value

mixed Returns the prepared item.

Throws

QueryPathException Thrown if the object passed in is not of a supprted object type.

8 calls to QueryPath::prepareInsert()
QueryPath::after in QueryPath/QueryPath.php
QueryPath::append in QueryPath/QueryPath.php
QueryPath::before in QueryPath/QueryPath.php
QueryPath::prepend in QueryPath/QueryPath.php
QueryPath::replaceWith in QueryPath/QueryPath.php

... See full list

File

QueryPath/QueryPath.php, line 909

Class

QueryPath

Code

protected function prepareInsert($item) {
  if (empty($item)) {
    return;
  }
  elseif (is_string($item)) {
    if ($this->options['replace_entities']) {
      $item = QueryPathEntities::replaceAllEntities($item);
    }
    $frag = $this->document
      ->createDocumentFragment();
    try {
      set_error_handler(array(
        'QueryPathParseException',
        'initializeFromError',
      ), $this->errTypes);
      $frag
        ->appendXML($item);
    } catch (Exception $e) {
      restore_error_handler();
      throw $e;
    }
    restore_error_handler();
    return $frag;
  }
  elseif ($item instanceof QueryPath) {
    if ($item
      ->size() == 0) {
      return;
    }
    return $this
      ->prepareInsert($item
      ->get(0));
  }
  elseif ($item instanceof DOMNode) {
    if ($item->ownerDocument !== $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);
  }
  throw new QueryPathException("Cannot prepare item of unsupported type: " . gettype($item));
}