1 | <?php |
||
19 | class LineStreamIterator implements Iterator |
||
20 | { |
||
21 | use GetOptionTrait; |
||
22 | |||
23 | const OPTION_ENDING = 'ending'; |
||
24 | const OPTION_IGNORE_BLANK = 'ignoreBlank'; |
||
25 | const OPTION_INCLUDE_ENDING = 'includeEnding'; |
||
26 | |||
27 | const DEFAULT_ENDING = PHP_EOL; |
||
28 | const DEFAULT_IGNORE_BLANK = true; |
||
29 | const DEFAULT_INCLUDE_ENDING = false; |
||
30 | |||
31 | /** @var string */ |
||
32 | private $ending; |
||
33 | /** @var int */ |
||
34 | private $position = 0; |
||
35 | /** @var string|bool */ |
||
36 | private $current; |
||
37 | /** @var bool */ |
||
38 | private $ignoreBlank = true; |
||
39 | /** @var StreamInterface */ |
||
40 | private $stream; |
||
41 | /** @var bool */ |
||
42 | private $includeEnding; |
||
43 | |||
44 | /** |
||
45 | * LineStreamIterator constructor. |
||
46 | * |
||
47 | * @param StreamInterface $stream |
||
48 | * @param array $options |
||
49 | */ |
||
50 | 9 | public function __construct(StreamInterface $stream, array $options = []) |
|
58 | |||
59 | /** |
||
60 | * @param bool $ignore |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | 4 | public function setIgnoreBlank($ignore) |
|
69 | |||
70 | /** |
||
71 | * Return the current element |
||
72 | * |
||
73 | * @link http://php.net/manual/en/iterator.current.php |
||
74 | * @return mixed Can return any type. |
||
75 | * @since 5.0.0 |
||
76 | */ |
||
77 | 6 | public function current() |
|
81 | |||
82 | /** |
||
83 | * Move forward to next element |
||
84 | * |
||
85 | * @link http://php.net/manual/en/iterator.next.php |
||
86 | * @since 5.0.0 |
||
87 | */ |
||
88 | 6 | public function next() |
|
94 | |||
95 | /** |
||
96 | * @param string $ending |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 6 | private function readStreamTill($ending) |
|
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | */ |
||
114 | 6 | private function readStream() |
|
115 | { |
||
116 | 6 | if ($this->stream->eof()) { |
|
117 | 5 | $this->current = false; |
|
118 | 5 | return false; |
|
119 | } else { |
||
120 | 6 | $buffer = $this->readStreamTill($this->ending); |
|
121 | |||
122 | 6 | if ($this->isBlankBuffer($buffer)) { |
|
123 | 1 | return $this->readStream(); |
|
124 | } else { |
||
125 | 6 | $this->stripEnding($buffer); |
|
126 | 6 | $this->current = $buffer; |
|
127 | 6 | return true; |
|
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $buffer Reference as we want speed here! |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | 6 | private function isBlankBuffer(&$buffer) |
|
138 | { |
||
139 | 6 | return (($buffer == '' || $buffer == $this->ending) && $this->ignoreBlank); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param string $buffer Reference as we want speed here! |
||
144 | */ |
||
145 | 6 | private function stripEnding(&$buffer) |
|
146 | { |
||
147 | 6 | $len = strlen($this->ending) * -1; |
|
148 | 6 | if (!$this->includeEnding && substr($buffer, $len) == $this->ending) { |
|
149 | 5 | $buffer = substr($buffer, 0, $len); |
|
150 | 5 | } |
|
151 | 6 | } |
|
152 | |||
153 | /** |
||
154 | * Return the key of the current element |
||
155 | * |
||
156 | * @link http://php.net/manual/en/iterator.key.php |
||
157 | * @return mixed scalar on success, or null on failure. |
||
158 | * @since 5.0.0 |
||
159 | */ |
||
160 | 6 | public function key() |
|
161 | { |
||
162 | 6 | return $this->position; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Checks if current position is valid |
||
167 | * |
||
168 | * @link http://php.net/manual/en/iterator.valid.php |
||
169 | * @return bool The return value will be casted to boolean and then evaluated. |
||
170 | * Returns true on success or false on failure. |
||
171 | * @since 5.0.0 |
||
172 | */ |
||
173 | 5 | public function valid() |
|
174 | { |
||
175 | 5 | return $this->current !== false; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | 6 | public function rewind() |
|
182 | { |
||
183 | 6 | $this->position = 0; |
|
184 | 6 | $this->stream->seek(0); |
|
185 | 6 | $this->readStream(); |
|
186 | 6 | } |
|
187 | |||
188 | /** |
||
189 | * @return bool |
||
190 | */ |
||
191 | 3 | public function isIgnoreBlank() |
|
195 | |||
196 | /** |
||
197 | * @return bool |
||
198 | */ |
||
199 | 3 | public function isIncludeEnding() |
|
200 | { |
||
203 | |||
204 | /** |
||
205 | * @param bool $includeEnding |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | 1 | public function setIncludeEnding($includeEnding) |
|
214 | |||
215 | /** |
||
216 | * @param string $ending |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | 1 | public function setEnding($ending) |
|
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | 3 | public function getEnding() |
|
233 | } |
||
234 |