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