Version 1
[yaffs-website] / vendor / caxy / php-htmldiff / tests / Caxy / Tests / HtmlDiff / HtmlFileIterator.php
1 <?php
2
3 namespace Caxy\Tests\HtmlDiff;
4
5 class HtmlFileIterator implements \Iterator
6 {
7     protected $files = array();
8     protected $key = 0;
9     protected $loadedDiffs = array();
10
11     public function __construct($directory)
12     {
13         $this->files = glob($directory.DIRECTORY_SEPARATOR."*.html");
14     }
15
16     /**
17      * Return the current element
18      * @link  http://php.net/manual/en/iterator.current.php
19      * @return mixed Can return any type.
20      * @since 5.0.0
21      */
22     public function current()
23     {
24         return $this->loadHtmlFile($this->key);
25     }
26
27     /**
28      * Move forward to next element
29      * @link  http://php.net/manual/en/iterator.next.php
30      * @return void Any returned value is ignored.
31      * @since 5.0.0
32      */
33     public function next()
34     {
35         $this->key++;
36     }
37
38     /**
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.
42      * @since 5.0.0
43      */
44     public function key()
45     {
46         return basename($this->files[$this->key]);
47     }
48
49     /**
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.
54      * @since 5.0.0
55      */
56     public function valid()
57     {
58         return isset($this->files[$this->key]);
59     }
60
61     /**
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.
65      * @since 5.0.0
66      */
67     public function rewind()
68     {
69         $this->key = 0;
70     }
71
72     protected function loadHtmlFile($key)
73     {
74         $filename = $this->files[$key];
75
76         if (!isset($this->loadedDiffs[$filename])) {
77
78             $html = file_get_contents($filename);
79
80             $oldText = $this->parseTagContent('oldText', $html);
81             $newText = $this->parseTagContent('newText', $html);
82             $expected = $this->parseTagContent('expected', $html);
83
84             if (null === $expected) {
85                 throw new \Exception('HTML fixture content should have an <expected> tag.');
86             }
87
88             $this->loadedDiffs[$filename] = array($oldText, $newText, $expected);
89         }
90
91         return $this->loadedDiffs[$filename];
92     }
93
94     protected function parseTagContent($tagName, $html)
95     {
96         $matches = array();
97         if (preg_match(sprintf('/<%s\s*[^>]*>(.*)<\/%s\s*>/is', $tagName, $tagName), $html, $matches)) {
98             return $matches[1];
99         }
100
101         return null;
102     }
103 }