|
18 | 18 | * |
19 | 19 | * @author Fabien Potencier <fabien@symfony.com> |
20 | 20 | */ |
21 | | -class Crawler implements \Countable |
| 21 | +class Crawler implements \Countable, \IteratorAggregate |
22 | 22 | { |
23 | 23 | /** |
24 | 24 | * @var string The current URI |
@@ -46,7 +46,7 @@ class Crawler implements \Countable |
46 | 46 | private $document; |
47 | 47 |
|
48 | 48 | /** |
49 | | - * @var \DOMNode[] |
| 49 | + * @var \DOMElement[] |
50 | 50 | */ |
51 | 51 | private $nodes = array(); |
52 | 52 |
|
@@ -327,25 +327,19 @@ public function addNode(\DOMNode $node) |
327 | 327 | } |
328 | 328 |
|
329 | 329 | if (null !== $this->document && $this->document !== $node->ownerDocument) { |
330 | | - @trigger_error('Attaching DOM nodes from multiple documents in a Crawler is deprecated as of 2.8 and will be forbidden in 3.0.', E_USER_DEPRECATED); |
| 330 | + throw new \InvalidArgumentException('Attaching DOM nodes from multiple documents in the same crawler is forbidden.'); |
331 | 331 | } |
332 | 332 |
|
333 | 333 | if (null === $this->document) { |
334 | 334 | $this->document = $node->ownerDocument; |
335 | 335 | } |
336 | 336 |
|
337 | | - $this->nodes[] = $node; |
338 | | - } |
339 | | - |
340 | | - // Serializing and unserializing a crawler creates DOM objects in a corrupted state. DOM elements are not properly serializable. |
341 | | - public function unserialize($serialized) |
342 | | - { |
343 | | - throw new \BadMethodCallException('A Crawler cannot be serialized.'); |
344 | | - } |
| 337 | + // Don't add duplicate nodes in the Crawler |
| 338 | + if (in_array($node, $this->nodes, true)) { |
| 339 | + return; |
| 340 | + } |
345 | 341 |
|
346 | | - public function serialize() |
347 | | - { |
348 | | - throw new \BadMethodCallException('A Crawler cannot be serialized.'); |
| 342 | + $this->nodes[] = $node; |
349 | 343 | } |
350 | 344 |
|
351 | 345 | /** |
@@ -966,6 +960,14 @@ public function count() |
966 | 960 | return count($this->nodes); |
967 | 961 | } |
968 | 962 |
|
| 963 | + /** |
| 964 | + * @return \ArrayIterator |
| 965 | + */ |
| 966 | + public function getIterator() |
| 967 | + { |
| 968 | + return new \ArrayIterator($this->nodes); |
| 969 | + } |
| 970 | + |
969 | 971 | /** |
970 | 972 | * @param \DOMElement $node |
971 | 973 | * @param string $siblingDir |
|
0 commit comments