Query.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dom
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @see Zend_Dom_Query_Css2Xpath
  22. */
  23. require_once 'Zend/Dom/Query/Css2Xpath.php';
  24. /**
  25. * @see Zend_Dom_Query_Result
  26. */
  27. require_once 'Zend/Dom/Query/Result.php';
  28. /**
  29. * Query DOM structures based on CSS selectors and/or XPath
  30. *
  31. * @package Zend_Dom
  32. * @subpackage Query
  33. * @copyright Copyright (C) 2008 - Present, Zend Technologies, Inc.
  34. * @license New BSD {@link http://framework.zend.com/license/new-bsd}
  35. */
  36. class Zend_Dom_Query
  37. {
  38. /**#@+
  39. * @const string Document types
  40. */
  41. const DOC_XML = 'docXml';
  42. const DOC_HTML = 'docHtml';
  43. const DOC_XHTML = 'docXhtml';
  44. /**#@-*/
  45. /**
  46. * @var string
  47. */
  48. protected $_document;
  49. /**
  50. * Document type
  51. * @var string
  52. */
  53. protected $_docType;
  54. /**
  55. * Constructor
  56. *
  57. * @param null|string $document
  58. * @return void
  59. */
  60. public function __construct($document = null)
  61. {
  62. $this->setDocument($document);
  63. }
  64. /**
  65. * Set document to query
  66. *
  67. * @param string $document
  68. * @return Zend_Dom_Query
  69. */
  70. public function setDocument($document)
  71. {
  72. if (0 === strlen($document)) {
  73. return $this;
  74. }
  75. if ('<?xml' == substr(trim($document), 0, 5)) {
  76. return $this->setDocumentXml($document);
  77. }
  78. if (strstr($document, 'DTD XHTML')) {
  79. return $this->setDocumentXhtml($document);
  80. }
  81. return $this->setDocumentHtml($document);
  82. }
  83. /**
  84. * Register HTML document
  85. *
  86. * @param string $document
  87. * @return Zend_Dom_Query
  88. */
  89. public function setDocumentHtml($document)
  90. {
  91. $this->_document = (string) $document;
  92. $this->_docType = self::DOC_HTML;
  93. return $this;
  94. }
  95. /**
  96. * Register XHTML document
  97. *
  98. * @param string $document
  99. * @return Zend_Dom_Query
  100. */
  101. public function setDocumentXhtml($document)
  102. {
  103. $this->_document = (string) $document;
  104. $this->_docType = self::DOC_XHTML;
  105. return $this;
  106. }
  107. /**
  108. * Register XML document
  109. *
  110. * @param string $document
  111. * @return Zend_Dom_Query
  112. */
  113. public function setDocumentXml($document)
  114. {
  115. $this->_document = (string) $document;
  116. $this->_docType = self::DOC_XML;
  117. return $this;
  118. }
  119. /**
  120. * Retrieve current document
  121. *
  122. * @return string
  123. */
  124. public function getDocument()
  125. {
  126. return $this->_document;
  127. }
  128. /**
  129. * Get document type
  130. *
  131. * @return string
  132. */
  133. public function getDocumentType()
  134. {
  135. return $this->_docType;
  136. }
  137. /**
  138. * Perform a CSS selector query
  139. *
  140. * @param string $query
  141. * @return Zend_Dom_Query_Result
  142. */
  143. public function query($query)
  144. {
  145. $xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
  146. return $this->queryXpath($xpathQuery, $query);
  147. }
  148. /**
  149. * Perform an XPath query
  150. *
  151. * @param string $xpathQuery
  152. * @param string $query CSS selector query
  153. * @return Zend_Dom_Query_Result
  154. */
  155. public function queryXpath($xpathQuery, $query = null)
  156. {
  157. if (null === ($document = $this->getDocument())) {
  158. require_once 'Zend/Dom/Exception.php';
  159. throw new Zend_Dom_Exception('Cannot query; no document registered');
  160. }
  161. $domDoc = new DOMDocument;
  162. $type = $this->getDocumentType();
  163. switch ($type) {
  164. case self::DOC_XML:
  165. $success = @$domDoc->loadXML($document);
  166. break;
  167. case self::DOC_HTML:
  168. case self::DOC_XHTML:
  169. default:
  170. $success = @$domDoc->loadHTML($document);
  171. break;
  172. }
  173. if (!$success) {
  174. require_once 'Zend/Dom/Exception.php';
  175. throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
  176. }
  177. $nodeList = $this->_getNodeList($domDoc, $xpathQuery);
  178. return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
  179. }
  180. /**
  181. * Prepare node list
  182. *
  183. * @param DOMDocument $document
  184. * @param string|array $xpathQuery
  185. * @return array
  186. */
  187. protected function _getNodeList($document, $xpathQuery)
  188. {
  189. $xpath = new DOMXPath($document);
  190. $xpathQuery = (string) $xpathQuery;
  191. if (preg_match_all('|\[contains\((@[a-z0-9_-]+),\s?\' |i', $xpathQuery, $matches)) {
  192. foreach ($matches[1] as $attribute) {
  193. $queryString = '//*[' . $attribute . ']';
  194. $attributeName = substr($attribute, 1);
  195. $nodes = $xpath->query($queryString);
  196. foreach ($nodes as $node) {
  197. $attr = $node->attributes->getNamedItem($attributeName);
  198. $attr->value = ' ' . $attr->value . ' ';
  199. }
  200. }
  201. }
  202. return $xpath->query($xpathQuery);
  203. }
  204. }