This class stores the default options for QueryPath. When a new QueryPath object is constructed, options specified here will be used. Details This class defines no options of its own. Instead, it provides a central tool for developers to override options set by QueryPath. When a QueryPath object is created, it will evaluate options in the following order: - Options passed into qp() have highest priority. - Options in QueryPath::Options (this class) have the next highest priority. - If the option is not specified elsewhere, QueryPath will use its own defaults.
See also: qp()
 public function testQPMerge()
 {
     $options = array('test1' => 'val1', 'test2' => 'val2');
     $options2 = array('test1' => 'val3', 'test4' => 'val4');
     Options::set($options);
     Options::merge($options2);
     $results = Options::get();
     $this->assertTrue(Options::has('test4'));
     $this->assertEquals('val3', $results['test1']);
 }
Exemple #2
0
 /**
  * Constructor.
  *
  * Typically, a new DOMQuery is created by QueryPath::with(), QueryPath::withHTML(),
  * qp(), or htmlqp().
  *
  * @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 + 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 DOMQuery) {
             //$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());
     }
 }
Exemple #3
0
 public function testReplaceEntitiesOption()
 {
     $path = '<?xml version="1.0"?><root/>';
     $xml = qp($path, NULL, array('replace_entities' => TRUE))->xml('<foo>&</foo>')->xml();
     $this->assertTrue(strpos($xml, '<foo>&amp;</foo>') !== FALSE);
     $xml = qp($path, NULL, array('replace_entities' => TRUE))->html('<foo>&</foo>')->xml();
     $this->assertTrue(strpos($xml, '<foo>&amp;</foo>') !== FALSE);
     $xml = qp($path, NULL, array('replace_entities' => TRUE))->xhtml('<foo>&</foo>')->xml();
     $this->assertTrue(strpos($xml, '<foo>&amp;</foo>') !== FALSE);
     \QueryPath\Options::set(array('replace_entities' => TRUE));
     $this->assertTrue(strpos($xml, '<foo>&amp;</foo>') !== FALSE);
     \QueryPath\Options::set(array());
 }