3 namespace Caxy\Tests\HtmlDiff;
5 class HtmlFileIterator implements \Iterator
7 protected $files = array();
9 protected $loadedDiffs = array();
11 public function __construct($directory)
13 $this->files = glob($directory.DIRECTORY_SEPARATOR."*.html");
17 * Return the current element
18 * @link http://php.net/manual/en/iterator.current.php
19 * @return mixed Can return any type.
22 public function current()
24 return $this->loadHtmlFile($this->key);
28 * Move forward to next element
29 * @link http://php.net/manual/en/iterator.next.php
30 * @return void Any returned value is ignored.
33 public function next()
39 * Return the key of the current element
40 * @link http://php.net/manual/en/iterator.key.php
41 * @return mixed scalar on success, or null on failure.
46 return basename($this->files[$this->key]);
50 * Checks if current position is valid
51 * @link http://php.net/manual/en/iterator.valid.php
52 * @return boolean The return value will be casted to boolean and then evaluated.
53 * Returns true on success or false on failure.
56 public function valid()
58 return isset($this->files[$this->key]);
62 * Rewind the Iterator to the first element
63 * @link http://php.net/manual/en/iterator.rewind.php
64 * @return void Any returned value is ignored.
67 public function rewind()
72 protected function loadHtmlFile($key)
74 $filename = $this->files[$key];
76 if (!isset($this->loadedDiffs[$filename])) {
78 $html = file_get_contents($filename);
80 $oldText = $this->parseTagContent('oldText', $html);
81 $newText = $this->parseTagContent('newText', $html);
82 $expected = $this->parseTagContent('expected', $html);
84 if (null === $expected) {
85 throw new \Exception('HTML fixture content should have an <expected> tag.');
88 $this->loadedDiffs[$filename] = array($oldText, $newText, $expected);
91 return $this->loadedDiffs[$filename];
94 protected function parseTagContent($tagName, $html)
97 if (preg_match(sprintf('/<%s\s*[^>]*>(.*)<\/%s\s*>/is', $tagName, $tagName), $html, $matches)) {