1 | <?php |
||
34 | class IteratorStream implements Streamable |
||
35 | { |
||
36 | use IsReadable, IsWritable; |
||
37 | |||
38 | /** |
||
39 | * Buffer stream |
||
40 | * @var \CSVelte\IO\BufferStream A BufferStream object |
||
41 | */ |
||
42 | protected $buffer; |
||
43 | |||
44 | protected $overflow; |
||
45 | |||
46 | /** |
||
47 | * Is stream readable? |
||
48 | * |
||
49 | * @var boolean Whether stream is readable |
||
50 | */ |
||
51 | protected $readable = true; |
||
52 | |||
53 | /** |
||
54 | * Is stream writable? |
||
55 | * |
||
56 | * @var boolean Whether stream is writable |
||
57 | */ |
||
58 | protected $writable = false; |
||
59 | |||
60 | /** |
||
61 | * Is stream seekable? |
||
62 | * |
||
63 | * @var boolean Whether stream is seekable |
||
64 | */ |
||
65 | protected $seekable = false; |
||
66 | |||
67 | /** |
||
68 | * @var array Any additional options / meta data |
||
69 | */ |
||
70 | protected $meta = [ |
||
71 | |||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * Instantiate an iterator stream |
||
76 | * |
||
77 | * Instantiate a new iterator stream. The iterator is used to continually |
||
78 | * refill a buffer as it is drained by read operations. |
||
79 | * |
||
80 | * @param \Iterator The iterator to stream data from |
||
81 | * @param \CSVelte\IO\BufferIterator|null Either a buffer or null (to use |
||
82 | * default buffer) |
||
83 | */ |
||
84 | 15 | public function __construct(Iterator $iter, $buffer = null) |
|
92 | |||
93 | /** |
||
94 | * Readability accessor. |
||
95 | * |
||
96 | * Despite the fact that any class that implements this interface must also |
||
97 | * define methods such as read and readLine, that is no guarantee that an |
||
98 | * object will necessarily be readable. This method should tell the user |
||
99 | * whether a stream is, in fact, readable. |
||
100 | * |
||
101 | * @return boolean True if readable, false otherwise |
||
102 | */ |
||
103 | 2 | public function isReadable() |
|
107 | |||
108 | 11 | public function read($bytes) |
|
125 | |||
126 | 10 | protected function inflateBuffer() |
|
134 | |||
135 | /** |
||
136 | * Read the entire stream, beginning to end. |
||
137 | * |
||
138 | * In most stream implementations, __toString() differs from getContents() |
||
139 | * in that it returns the entire stream rather than just the remainder, but |
||
140 | * due to the way this stream works (sort of like a conveyor belt), this |
||
141 | * method is an alias to getContents() |
||
142 | * |
||
143 | * @return string The entire stream, beginning to end |
||
144 | */ |
||
145 | 3 | public function __toString() |
|
150 | |||
151 | /** |
||
152 | * Read the remainder of the stream |
||
153 | * |
||
154 | * @return string The remainder of the stream |
||
155 | */ |
||
156 | 4 | public function getContents() |
|
168 | |||
169 | /** |
||
170 | * Return the size (in bytes) of this stream (if known). |
||
171 | * |
||
172 | * @return int|null Size (in bytes) of this stream |
||
173 | */ |
||
174 | 1 | public function getSize() |
|
178 | |||
179 | /** |
||
180 | * Return the current position within the stream/readable |
||
181 | * |
||
182 | * @return int The current position within readable |
||
183 | */ |
||
184 | public function tell() |
||
188 | |||
189 | /** |
||
190 | * Determine whether the end of the stream has been reached |
||
191 | * |
||
192 | * @return boolean Whether we're at the end of the stream |
||
193 | */ |
||
194 | 4 | public function eof() |
|
201 | |||
202 | /** |
||
203 | * Rewind to beginning of stream |
||
204 | */ |
||
205 | 3 | public function rewind() |
|
210 | |||
211 | /** |
||
212 | * Get stream metadata as an associative array or retrieve a specific key. |
||
213 | * |
||
214 | * The keys returned are identical to the keys returned from PHP's |
||
215 | * stream_get_meta_data() function. |
||
216 | * |
||
217 | * @param string $key Specific metadata to retrieve. |
||
218 | * @return array|mixed|null Returns an associative array if no key is |
||
219 | * provided. Returns a specific key value if a key is provided and the |
||
220 | * value is found, or null if the key is not found. |
||
221 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
222 | */ |
||
223 | 2 | public function getMetadata($key = null) |
|
230 | |||
231 | /** |
||
232 | * Closes the stream and any underlying resources. |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | public function close() |
||
245 | |||
246 | /** |
||
247 | * Separates any underlying resources from the stream. |
||
248 | * |
||
249 | * After the stream has been detached, the stream is in an unusable state. |
||
250 | * |
||
251 | * @return string|null Underlying PHP stream, if any |
||
252 | * @todo I'm not sure what detach is for so I don't know whether what I'm |
||
253 | * doing here is right. The reason I have the method at all is because |
||
254 | * psr7 StreamInterface has one.f |
||
255 | */ |
||
256 | 1 | public function detach() |
|
265 | |||
266 | /** |
||
267 | * Writability accessor. |
||
268 | * |
||
269 | * Despite the fact that any class that implements this interface must also |
||
270 | * define methods such as write and writeLine, that is no guarantee that an |
||
271 | * object will necessarily be writable. This method should tell the user |
||
272 | * whether a stream is, in fact, writable. |
||
273 | * |
||
274 | * @return boolean True if writable, false otherwise |
||
275 | */ |
||
276 | 1 | public function isWritable() |
|
280 | |||
281 | /** |
||
282 | * Write data to the output. |
||
283 | * |
||
284 | * @param string The data to write |
||
285 | * @return int The number of bytes written |
||
286 | */ |
||
287 | public function write($data) |
||
291 | |||
292 | /** |
||
293 | * Seekability accessor. |
||
294 | * |
||
295 | * Despite the fact that any class that implements this interface must also |
||
296 | * define methods such as seek, that is no guarantee that an |
||
297 | * object will necessarily be seekable. This method should tell the user |
||
298 | * whether a stream is, in fact, seekable. |
||
299 | * |
||
300 | * @return boolean True if seekable, false otherwise |
||
301 | */ |
||
302 | 1 | public function isSeekable() |
|
306 | |||
307 | /** |
||
308 | * Seek to specified offset. |
||
309 | * |
||
310 | * @param integer Offset to seek to |
||
311 | * @param integer Position from whence the offset should be applied |
||
312 | * @return boolean True if seek was successful |
||
313 | */ |
||
314 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
318 | |||
319 | } |
||
320 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: