99namespace tomverran \Robot ;
1010
1111
12- class RobotsFile
12+ class RobotsFile implements \Iterator
1313{
1414 /**
15- * @var String[]
15+ * @var \ArrayIterator
1616 */
17- private $ lines ;
18-
19- const USER_AGENT = 'user-agent ' ;
20-
21- const DISALLOW = 'disallow ' ;
22-
23- const ALLOW = 'allow ' ;
17+ private $ lineIterator ;
2418
2519 /**
2620 * Construct this Robots file
@@ -29,51 +23,73 @@ class RobotsFile
2923 public function __construct ($ content )
3024 {
3125 $ withoutComments = preg_replace ( '/#.*/ ' , '' , strtolower ($ content ));
26+ $ lines = [];
3227
3328 foreach (explode ("\n" , $ withoutComments ) as $ line ) {
3429 $ lineParts = array_filter (array_map ('trim ' , explode (': ' , $ line )));
35- if ($ this -> lineIsValid ($ lineParts )) {
36- $ this -> lines [] = $ lineParts ;
30+ if (! empty ($ lineParts )) {
31+ $ lines [] = $ lineParts ;
3732 }
3833 }
34+
35+ $ this ->lineIterator = new \ArrayIterator ($ lines );
3936 }
4037
41- private function lineIsValid ($ line ) {
42- $ validDirectives = [self ::USER_AGENT , self ::DISALLOW , self ::ALLOW ];
43- return count ($ line ) == 2 && in_array ($ line [0 ], $ validDirectives );
38+ /**
39+ * Return the current element
40+ * @link http://php.net/manual/en/iterator.current.php
41+ * @return mixed Can return any type.
42+ * @since 5.0.0
43+ */
44+ public function current ()
45+ {
46+ $ cur = $ this ->lineIterator ->current ();
47+ return count ($ cur ) > 1 ? $ cur [1 ] : '' ;
4448 }
4549
4650 /**
47- * Get the first directive in the file
51+ * Move forward to next element
52+ * @link http://php.net/manual/en/iterator.next.php
53+ * @return void Any returned value is ignored.
54+ * @since 5.0.0
4855 */
49- public function firstDirective ()
56+ public function next ()
5057 {
51- return $ this ->lines [ 0 ][ 0 ] ;
58+ $ this ->lineIterator -> next () ;
5259 }
5360
54- public function firstDirectiveIs ($ args )
61+ /**
62+ * Return the key of the current element
63+ * @link http://php.net/manual/en/iterator.key.php
64+ * @return mixed scalar on success, or null on failure.
65+ * @since 5.0.0
66+ */
67+ public function key ()
5568 {
56- if (!$ this ->hasLines ()) {
57- return false ;
58- }
59- return in_array ($ this ->firstDirective (), func_get_args ());
69+ $ cur = $ this ->lineIterator ->current ();
70+ return $ cur [0 ];
6071 }
6172
6273 /**
63- * Get the argument of the first directive,
64- * and shift the file to remove it
74+ * Checks if current position is valid
75+ * @link http://php.net/manual/en/iterator.valid.php
76+ * @return boolean The return value will be casted to boolean and then evaluated.
77+ * Returns true on success or false on failure.
78+ * @since 5.0.0
6579 */
66- public function shiftArgument ()
80+ public function valid ()
6781 {
68- return array_shift ( $ this ->lines )[ 1 ] ;
82+ return $ this ->lineIterator -> valid () ;
6983 }
7084
7185 /**
72- * Does this file have any remaining lines
73- * @return bool
86+ * Rewind the Iterator to the first element
87+ * @link http://php.net/manual/en/iterator.rewind.php
88+ * @return void Any returned value is ignored.
89+ * @since 5.0.0
7490 */
75- public function hasLines ()
91+ public function rewind ()
7692 {
77- return ! empty ( $ this ->lines );
93+ $ this ->lineIterator -> rewind ( );
7894 }
7995}
0 commit comments