Source of file Query.php
Size: 8,238 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Dom/Query.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Dom; use DOMDocument; /** * Query DOM structures based on CSS selectors and/or XPath * @deprecated * @see \Zend\Dom\Document\Query */ class Query { /**#@+ * Document types */ const DOC_XML = 'docXml'; const DOC_HTML = 'docHtml'; const DOC_XHTML = 'docXhtml'; /**#@-*/ /** * @var string */ protected $document; /** * DOMDocument errors, if any * @var false|array */ protected $documentErrors = false; /** * Document type * @var string */ protected $docType; /** * Document encoding * @var null|string */ protected $encoding; /** * XPath namespaces * @var array */ protected $xpathNamespaces = array(); /** * XPath PHP Functions * @var mixed */ protected $xpathPhpFunctions; /** * Constructor * * @param null|string $document * @param null|string $encoding */ public function __construct($document = null, $encoding = null) { $this->setEncoding($encoding); $this->setDocument($document); } /** * Set document encoding * * @param string $encoding * @return Query */ public function setEncoding($encoding) { $this->encoding = (null === $encoding) ? null : (string) $encoding; return $this; } /** * Get document encoding * * @return null|string */ public function getEncoding() { return $this->encoding; } /** * Set document to query * * @param string $document * @param null|string $encoding Document encoding * @return Query */ public function setDocument($document, $encoding = null) { if (0 === strlen($document)) { return $this; } // breaking XML declaration to make syntax highlighting work if ('<' . '?xml' == substr(trim($document), 0, 5)) { if (preg_match('/<html[^>]*xmlns="([^"]+)"[^>]*>/i', $document, $matches)) { $this->xpathNamespaces[] = $matches[1]; return $this->setDocumentXhtml($document, $encoding); } return $this->setDocumentXml($document, $encoding); } if (strstr($document, 'DTD XHTML')) { return $this->setDocumentXhtml($document, $encoding); } return $this->setDocumentHtml($document, $encoding); } /** * Register HTML document * * @param string $document * @param null|string $encoding Document encoding * @return Query */ public function setDocumentHtml($document, $encoding = null) { $this->document = (string) $document; $this->docType = self::DOC_HTML; if (null !== $encoding) { $this->setEncoding($encoding); } return $this; } /** * Register XHTML document * * @param string $document * @param null|string $encoding Document encoding * @return Query */ public function setDocumentXhtml($document, $encoding = null) { $this->document = (string) $document; $this->docType = self::DOC_XHTML; if (null !== $encoding) { $this->setEncoding($encoding); } return $this; } /** * Register XML document * * @param string $document * @param null|string $encoding Document encoding * @return Query */ public function setDocumentXml($document, $encoding = null) { $this->document = (string) $document; $this->docType = self::DOC_XML; if (null !== $encoding) { $this->setEncoding($encoding); } return $this; } /** * Retrieve current document * * @return string */ public function getDocument() { return $this->document; } /** * Get document type * * @return string */ public function getDocumentType() { return $this->docType; } /** * Get any DOMDocument errors found * * @return false|array */ public function getDocumentErrors() { return $this->documentErrors; } /** * Perform a CSS selector query * * @param string $query * @return NodeList */ public function execute($query) { $xpathQuery = Document\Query::cssToXpath($query); return $this->queryXpath($xpathQuery, $query); } /** * Perform an XPath query * * @param string|array $xpathQuery * @param string|null $query CSS selector query * @throws Exception\RuntimeException * @return NodeList */ public function queryXpath($xpathQuery, $query = null) { if (null === ($document = $this->getDocument())) { throw new Exception\RuntimeException('Cannot query; no document registered'); } $encoding = $this->getEncoding(); libxml_use_internal_errors(true); libxml_disable_entity_loader(true); if (null === $encoding) { $domDoc = new DOMDocument('1.0'); } else { $domDoc = new DOMDocument('1.0', $encoding); } $type = $this->getDocumentType(); switch ($type) { case self::DOC_XML: $success = $domDoc->loadXML($document); foreach ($domDoc->childNodes as $child) { if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { throw new Exception\RuntimeException( 'Invalid XML: Detected use of illegal DOCTYPE' ); } } break; case self::DOC_HTML: case self::DOC_XHTML: default: $success = $domDoc->loadHTML($document); break; } $errors = libxml_get_errors(); if (!empty($errors)) { $this->documentErrors = $errors; libxml_clear_errors(); } libxml_disable_entity_loader(false); libxml_use_internal_errors(false); if (!$success) { throw new Exception\RuntimeException(sprintf('Error parsing document (type == %s)', $type)); } $nodeList = $this->getNodeList($domDoc, $xpathQuery); return new NodeList($query, $xpathQuery, $domDoc, $nodeList); } /** * Register XPath namespaces * * @param array $xpathNamespaces * @return void */ public function registerXpathNamespaces($xpathNamespaces) { $this->xpathNamespaces = $xpathNamespaces; } /** * Register PHP Functions to use in internal DOMXPath * * @param bool $xpathPhpFunctions * @return void */ public function registerXpathPhpFunctions($xpathPhpFunctions = true) { $this->xpathPhpFunctions = $xpathPhpFunctions; } /** * Prepare node list * * @param DOMDocument $document * @param string|array $xpathQuery * @return array * @throws ErrorException If query cannot be executed */ protected function getNodeList($document, $xpathQuery) { $xpath = new DOMXPath($document); foreach ($this->xpathNamespaces as $prefix => $namespaceUri) { $xpath->registerNamespace($prefix, $namespaceUri); } if ($this->xpathPhpFunctions) { $xpath->registerNamespace("php", "http://php.net/xpath"); ($this->xpathPhpFunctions === true) ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($this->xpathPhpFunctions); } $xpathQuery = (string) $xpathQuery; $nodeList = $xpath->queryWithErrorException($xpathQuery); return $nodeList; } } |