QueryPath\DOMQuery::__construct PHP Method

__construct() public method

Typically, a new DOMQuery is created by QueryPath::with(), QueryPath::withHTML(), qp(), or htmlqp().
See also: qp()
public __construct ( mixed $document = null, string $string = null, array $options = [] )
$document mixed A document-like object.
$string string A CSS 3 Selector
$options array An associative array of options.
    public function __construct($document = null, $string = null, $options = array())
    {
        $string = trim($string);
        $this->options = $options + Options::get() + $this->options;
        $parser_flags = isset($options['parser_flags']) ? $options['parser_flags'] : self::DEFAULT_PARSER_FLAGS;
        if (!empty($this->options['ignore_parser_warnings'])) {
            // Don't convert parser warnings into exceptions.
            $this->errTypes = 257;
            //E_ERROR | E_USER_ERROR;
        } elseif (isset($this->options['exception_level'])) {
            // Set the error level at which exceptions will be thrown. By default,
            // QueryPath will throw exceptions for
            // E_ERROR | E_USER_ERROR | E_WARNING | E_USER_WARNING.
            $this->errTypes = $this->options['exception_level'];
        }
        // Empty: Just create an empty QP.
        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)) {
            // This is the most frequent object type.
            if ($document instanceof \SplObjectStorage) {
                $this->matches = $document;
                if ($document->count() != 0) {
                    $first = $this->getFirstMatch();
                    if (!empty($first->ownerDocument)) {
                        $this->document = $first->ownerDocument;
                    }
                }
            } elseif ($document instanceof self) {
                //$this->matches = $document->get(NULL, TRUE);
                $this->setMatches($document->get(null, true));
                if ($this->matches->count() > 0) {
                    $this->document = $this->getFirstMatch()->ownerDocument;
                }
            } elseif ($document instanceof \DOMDocument) {
                $this->document = $document;
                //$this->matches = $this->matches($document->documentElement);
                $this->setMatches($document->documentElement);
            } elseif ($document instanceof \DOMNode) {
                $this->document = $document->ownerDocument;
                //$this->matches = array($document);
                $this->setMatches($document);
            } elseif ($document instanceof \SimpleXMLElement) {
                $import = dom_import_simplexml($document);
                $this->document = $import->ownerDocument;
                //$this->matches = array($import);
                $this->setMatches($import);
            } else {
                throw new \QueryPath\Exception('Unsupported class type: ' . get_class($document));
            }
        } elseif (is_array($document)) {
            //trigger_error('Detected deprecated array support', E_USER_NOTICE);
            if (!empty($document) && $document[0] instanceof \DOMNode) {
                $found = new \SplObjectStorage();
                foreach ($document as $item) {
                    $found->attach($item);
                }
                //$this->matches = $found;
                $this->setMatches($found);
                $this->document = $this->getFirstMatch()->ownerDocument;
            }
        } elseif ($this->isXMLish($document)) {
            // $document is a string with XML
            $this->document = $this->parseXMLString($document);
            $this->setMatches($this->document->documentElement);
        } else {
            // $document is a filename
            $context = empty($options['context']) ? null : $options['context'];
            $this->document = $this->parseXMLFile($document, $parser_flags, $context);
            $this->setMatches($this->document->documentElement);
        }
        // Globally set the output option.
        if (isset($this->options['format_output']) && $this->options['format_output'] == false) {
            $this->document->formatOutput = false;
        } else {
            $this->document->formatOutput = true;
        }
        // Do a find if the second param was set.
        if (isset($string) && strlen($string) > 0) {
            // We don't issue a find because that creates a new DOMQuery.
            //$this->find($string);
            $query = new \QueryPath\CSS\DOMTraverser($this->matches);
            $query->find($string);
            $this->setMatches($query->matches());
        }
    }