Result.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Results for DOM XPath query
  22. *
  23. * @package Zend_Dom
  24. * @subpackage Query
  25. * @uses Iterator
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. * @version $Id$
  29. */
  30. class Zend_Dom_Query_Result implements Iterator,Countable
  31. {
  32. /**
  33. * Number of results
  34. * @var int
  35. */
  36. protected $_count;
  37. /**
  38. * CSS Selector query
  39. * @var string
  40. */
  41. protected $_cssQuery;
  42. /**
  43. * @var DOMDocument
  44. */
  45. protected $_document;
  46. /**
  47. * @var DOMNodeList
  48. */
  49. protected $_nodeList;
  50. /**
  51. * Current iterator position
  52. * @var int
  53. */
  54. protected $_position = 0;
  55. /**
  56. * @var DOMXPath
  57. */
  58. protected $_xpath;
  59. /**
  60. * XPath query
  61. * @var string
  62. */
  63. protected $_xpathQuery;
  64. /**
  65. * Constructor
  66. *
  67. * @param string $cssQuery
  68. * @param string|array $xpathQuery
  69. * @param DOMDocument $document
  70. * @param DOMNodeList $nodeList
  71. * @return void
  72. */
  73. public function __construct($cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList)
  74. {
  75. $this->_cssQuery = $cssQuery;
  76. $this->_xpathQuery = $xpathQuery;
  77. $this->_document = $document;
  78. $this->_nodeList = $nodeList;
  79. }
  80. /**
  81. * Retrieve CSS Query
  82. *
  83. * @return string
  84. */
  85. public function getCssQuery()
  86. {
  87. return $this->_cssQuery;
  88. }
  89. /**
  90. * Retrieve XPath query
  91. *
  92. * @return string
  93. */
  94. public function getXpathQuery()
  95. {
  96. return $this->_xpathQuery;
  97. }
  98. /**
  99. * Retrieve DOMDocument
  100. *
  101. * @return DOMDocument
  102. */
  103. public function getDocument()
  104. {
  105. return $this->_document;
  106. }
  107. /**
  108. * Iterator: rewind to first element
  109. *
  110. * @return void
  111. */
  112. public function rewind()
  113. {
  114. $this->_position = 0;
  115. return $this->_nodeList->item(0);
  116. }
  117. /**
  118. * Iterator: is current position valid?
  119. *
  120. * @return bool
  121. */
  122. public function valid()
  123. {
  124. if (in_array($this->_position, range(0, $this->_nodeList->length - 1)) && $this->_nodeList->length > 0) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. /**
  130. * Iterator: return current element
  131. *
  132. * @return DOMElement
  133. */
  134. public function current()
  135. {
  136. return $this->_nodeList->item($this->_position);
  137. }
  138. /**
  139. * Iterator: return key of current element
  140. *
  141. * @return int
  142. */
  143. public function key()
  144. {
  145. return $this->_position;
  146. }
  147. /**
  148. * Iterator: move to next element
  149. *
  150. * @return void
  151. */
  152. public function next()
  153. {
  154. ++$this->_position;
  155. return $this->_nodeList->item($this->_position);
  156. }
  157. /**
  158. * Countable: get count
  159. *
  160. * @return int
  161. */
  162. public function count()
  163. {
  164. return $this->_nodeList->length;
  165. }
  166. }