Result.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  72. public function __construct($cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList)
  73. {
  74. $this->_cssQuery = $cssQuery;
  75. $this->_xpathQuery = $xpathQuery;
  76. $this->_document = $document;
  77. $this->_nodeList = $nodeList;
  78. }
  79. /**
  80. * Retrieve CSS Query
  81. *
  82. * @return string
  83. */
  84. public function getCssQuery()
  85. {
  86. return $this->_cssQuery;
  87. }
  88. /**
  89. * Retrieve XPath query
  90. *
  91. * @return string
  92. */
  93. public function getXpathQuery()
  94. {
  95. return $this->_xpathQuery;
  96. }
  97. /**
  98. * Retrieve DOMDocument
  99. *
  100. * @return DOMDocument
  101. */
  102. public function getDocument()
  103. {
  104. return $this->_document;
  105. }
  106. /**
  107. * Iterator: rewind to first element
  108. *
  109. * @return DOMNode|null
  110. */
  111. public function rewind()
  112. {
  113. $this->_position = 0;
  114. return $this->_nodeList->item(0);
  115. }
  116. /**
  117. * Iterator: is current position valid?
  118. *
  119. * @return bool
  120. */
  121. public function valid()
  122. {
  123. if (in_array($this->_position, range(0, $this->_nodeList->length - 1)) && $this->_nodeList->length > 0) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * Iterator: return current element
  130. *
  131. * @return DOMElement
  132. */
  133. public function current()
  134. {
  135. return $this->_nodeList->item($this->_position);
  136. }
  137. /**
  138. * Iterator: return key of current element
  139. *
  140. * @return int
  141. */
  142. public function key()
  143. {
  144. return $this->_position;
  145. }
  146. /**
  147. * Iterator: move to next element
  148. *
  149. * @return DOMNode|null
  150. */
  151. public function next()
  152. {
  153. ++$this->_position;
  154. return $this->_nodeList->item($this->_position);
  155. }
  156. /**
  157. * Countable: get count
  158. *
  159. * @return int
  160. */
  161. public function count()
  162. {
  163. return $this->_nodeList->length;
  164. }
  165. }