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); } }
public function testQPMerge() { $options = array('test1' => 'val1', 'test2' => 'val2'); $options2 = array('test1' => 'val3', 'test4' => 'val4'); QueryPathOptions::set($options); QueryPathOptions::merge($options2); $results = QueryPathOptions::get(); $this->assertTrue(QueryPathOptions::has('test4')); $this->assertEquals('val3', $results['test1']); }
/** * Constructor. * * This should not be called directly. Use the {@link qp()} factory function * instead. * * @param mixed $document * A document-like object. * @param string $string * A CSS 3 Selector * @param array $options * An associative array of options. * @see qp() */ 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'])) { // 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)) { 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->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); } 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)) { //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); } // Do a find if the second param was set. if (isset($string) && strlen($string) > 0) { $this->find($string); } }