3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * Nodelist for DOM XPath query
22 class NodeList
implements Iterator
, Countable
, ArrayAccess
41 * Current iterator position
44 protected $position = 0;
50 protected $xpathQuery;
55 * @param string $cssQuery
56 * @param string|array $xpathQuery
57 * @param DOMDocument $document
58 * @param DOMNodeList $nodeList
60 public function __construct($cssQuery, $xpathQuery, DOMDocument
$document, DOMNodeList
$nodeList)
62 $this->cssQuery
= $cssQuery;
63 $this->xpathQuery
= $xpathQuery;
64 $this->document
= $document;
65 $this->nodeList
= $nodeList;
73 public function getCssQuery()
75 return $this->cssQuery
;
79 * Retrieve XPath query
83 public function getXpathQuery()
85 return $this->xpathQuery
;
89 * Retrieve DOMDocument
93 public function getDocument()
95 return $this->document
;
99 * Iterator: rewind to first element
103 public function rewind()
107 return $this->nodeList
->item(0);
111 * Iterator: is current position valid?
115 public function valid()
117 if (in_array($this->position
, range(0, $this->nodeList
->length
- 1)) && $this->nodeList
->length
> 0) {
125 * Iterator: return current element
129 public function current()
131 return $this->nodeList
->item($this->position
);
135 * Iterator: return key of current element
139 public function key()
141 return $this->position
;
145 * Iterator: move to next element
149 public function next()
153 return $this->nodeList
->item($this->position
);
157 * Countable: get count
161 public function count()
163 return $this->nodeList
->length
;
167 * ArrayAccess: offset exists
172 public function offsetExists($key)
174 if (in_array($key, range(0, $this->nodeList
->length
- 1)) && $this->nodeList
->length
> 0) {
181 * ArrayAccess: get offset
186 public function offsetGet($key)
188 return $this->nodeList
->item($key);
192 * ArrayAccess: set offset
195 * @param mixed $value
196 * @throws Exception\BadMethodCallException when attemptingn to write to a read-only item
198 public function offsetSet($key, $value)
200 throw new Exception\
BadMethodCallException('Attempting to write to a read-only list');
204 * ArrayAccess: unset offset
207 * @throws Exception\BadMethodCallException when attemptingn to unset a read-only item
209 public function offsetUnset($key)
211 throw new Exception\
BadMethodCallException('Attempting to unset on a read-only list');