You are here

public function QueryPath::__construct in QueryPath 6

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

File

QueryPath/QueryPath.php, line 97

Class

QueryPath

Code

public function __construct($document = NULL, $string = NULL, $options = array()) {
  $string = trim($string);
  $this->options = $options + QueryPathOptions::get() + $this->options;
  $parser_flags = isset($options['parser_flags']) ? $options['parser_flags'] : self::DEFAULT_PARSER_FLAGS;
  if (!empty($this->options['ignore_parser_warnings'])) {
    $this->errTypes = 257;
  }
  elseif (isset($this->options['exception_level'])) {
    $this->errTypes = $this->options['exception_level'];
  }
  if (empty($document)) {
    $this->document = isset($this->options['encoding']) ? new DOMDocument('1.0', $this->options['encoding']) : new DOMDocument();
    $this
      ->setMatches(new SplObjectStorage());
  }
  elseif (is_object($document)) {
    if ($document instanceof QueryPath) {
      $this->matches = $document
        ->get(NULL, TRUE);
      if ($this->matches
        ->count() > 0) {
        $this->document = $this
          ->getFirstMatch()->ownerDocument;
      }
    }
    elseif ($document instanceof DOMDocument) {
      $this->document = $document;
      $this
        ->setMatches($document->documentElement);
    }
    elseif ($document instanceof DOMNode) {
      $this->document = $document->ownerDocument;
      $this
        ->setMatches($document);
    }
    elseif ($document instanceof SimpleXMLElement) {
      $import = dom_import_simplexml($document);
      $this->document = $import->ownerDocument;
      $this
        ->setMatches($import);
    }
    elseif ($document instanceof SplObjectStorage) {
      if ($document
        ->count() == 0) {
        throw new QueryPathException('Cannot initialize QueryPath from an empty SplObjectStore');
      }
      $this->matches = $document;
      $this->document = $this
        ->getFirstMatch()->ownerDocument;
    }
    else {
      throw new QueryPathException('Unsupported class type: ' . get_class($document));
    }
  }
  elseif (is_array($document)) {
    if (!empty($document) && $document[0] instanceof DOMNode) {
      $found = new SplObjectStorage();
      foreach ($document as $item) {
        $found
          ->attach($item);
      }
      $this
        ->setMatches($found);
      $this->document = $this
        ->getFirstMatch()->ownerDocument;
    }
  }
  elseif ($this
    ->isXMLish($document)) {
    $this->document = $this
      ->parseXMLString($document);
    $this
      ->setMatches($this->document->documentElement);
  }
  else {
    $context = empty($options['context']) ? NULL : $options['context'];
    $this->document = $this
      ->parseXMLFile($document, $parser_flags, $context);
    $this
      ->setMatches($this->document->documentElement);
  }
  if (isset($string) && strlen($string) > 0) {
    $this
      ->find($string);
  }
}