1 | <?php |
||
37 | class IteratorStream implements Streamable |
||
38 | { |
||
39 | use IsReadable, IsWritable; |
||
40 | |||
41 | /** |
||
42 | * Buffer stream |
||
43 | * @var \CSVelte\IO\BufferStream A BufferStream object |
||
44 | */ |
||
45 | protected $buffer; |
||
46 | |||
47 | /** |
||
48 | * Iterator to read from |
||
49 | * |
||
50 | * @var Traversable |
||
51 | */ |
||
52 | protected $iter; |
||
53 | |||
54 | /** |
||
55 | * Is stream readable? |
||
56 | * |
||
57 | * @var boolean Whether stream is readable |
||
58 | */ |
||
59 | protected $readable = true; |
||
60 | |||
61 | /** |
||
62 | * Is stream writable? |
||
63 | * |
||
64 | * @var boolean Whether stream is writable |
||
65 | */ |
||
66 | protected $writable = false; |
||
67 | |||
68 | /** |
||
69 | * Is stream seekable? |
||
70 | * |
||
71 | * @var boolean Whether stream is seekable |
||
72 | */ |
||
73 | protected $seekable = false; |
||
74 | |||
75 | /** |
||
76 | * @var array Any additional options / meta data |
||
77 | */ |
||
78 | protected $meta = [ |
||
79 | |||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Instantiate an iterator stream |
||
84 | * |
||
85 | * Instantiate a new iterator stream. The iterator is used to continually |
||
86 | * refill a buffer as it is drained by read operations. |
||
87 | * |
||
88 | * @param \Iterator The iterator to stream data from |
||
89 | * @param \CSVelte\IO\BufferIterator|null Either a buffer or null (to use |
||
90 | * default buffer) |
||
91 | * @todo this should expect a BufferInterface or a Bufferable rather than |
||
92 | * a BufferStream |
||
93 | */ |
||
94 | 21 | public function __construct(Traversable $iter, $buffer = null) |
|
110 | |||
111 | /** |
||
112 | * Readability accessor. |
||
113 | * |
||
114 | * Despite the fact that any class that implements this interface must also |
||
115 | * define methods such as read and readLine, that is no guarantee that an |
||
116 | * object will necessarily be readable. This method should tell the user |
||
117 | * whether a stream is, in fact, readable. |
||
118 | * |
||
119 | * @return boolean True if readable, false otherwise |
||
120 | */ |
||
121 | 2 | public function isReadable() |
|
125 | |||
126 | 13 | public function read($bytes) |
|
144 | |||
145 | 12 | protected function inflateBuffer() |
|
153 | |||
154 | /** |
||
155 | * Read the entire stream, beginning to end. |
||
156 | * |
||
157 | * In most stream implementations, __toString() differs from getContents() |
||
158 | * in that it returns the entire stream rather than just the remainder, but |
||
159 | * due to the way this stream works (sort of like a conveyor belt), this |
||
160 | * method is an alias to getContents() |
||
161 | * |
||
162 | * @return string The entire stream, beginning to end |
||
163 | */ |
||
164 | 4 | public function __toString() |
|
169 | |||
170 | /** |
||
171 | * Read the remainder of the stream |
||
172 | * |
||
173 | * @return string The remainder of the stream |
||
174 | */ |
||
175 | 5 | public function getContents() |
|
187 | |||
188 | /** |
||
189 | * Return the size (in bytes) of this stream (if known). |
||
190 | * |
||
191 | * @return int|null Size (in bytes) of this stream |
||
192 | */ |
||
193 | 1 | public function getSize() |
|
197 | |||
198 | /** |
||
199 | * Return the current position within the stream/readable |
||
200 | * |
||
201 | * I can't decide whether there is any meaningful way to "tell" the |
||
202 | * current position within this type of stream. For now I'm just |
||
203 | * going to return false because the nature of this type of stream |
||
204 | * means that it technically has no definitive beginning or end and |
||
205 | * therefor no absolute position. If a "tell" is truly needed, I |
||
206 | * suppose I could keep track of how many bytes have been read over |
||
207 | * the lifetime of the object, but I don't think that is meaningful |
||
208 | * and/or useful. |
||
209 | * |
||
210 | * @return int|false The current position within readable |
||
211 | */ |
||
212 | 1 | public function tell() |
|
216 | |||
217 | /** |
||
218 | * Determine whether the end of the stream has been reached |
||
219 | * |
||
220 | * @return boolean Whether we're at the end of the stream |
||
221 | */ |
||
222 | 5 | public function eof() |
|
229 | |||
230 | /** |
||
231 | * Rewind to beginning of stream |
||
232 | */ |
||
233 | 4 | public function rewind() |
|
238 | |||
239 | /** |
||
240 | * Get stream metadata as an associative array or retrieve a specific key. |
||
241 | * |
||
242 | * The keys returned are identical to the keys returned from PHP's |
||
243 | * stream_get_meta_data() function. |
||
244 | * |
||
245 | * @param string $key Specific metadata to retrieve. |
||
246 | * @return array|mixed|null Returns an associative array if no key is |
||
247 | * provided. Returns a specific key value if a key is provided and the |
||
248 | * value is found, or null if the key is not found. |
||
249 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
250 | */ |
||
251 | 2 | public function getMetadata($key = null) |
|
258 | |||
259 | /** |
||
260 | * Closes the stream and any underlying resources. |
||
261 | * |
||
262 | * @return void |
||
263 | */ |
||
264 | 1 | public function close() |
|
274 | |||
275 | /** |
||
276 | * Separates any underlying resources from the stream. |
||
277 | * |
||
278 | * After the stream has been detached, the stream is in an unusable state. |
||
279 | * |
||
280 | * @return array|Resource Underlying PHP stream, if any |
||
281 | * @todo I'm not sure what detach is for so I don't know whether what I'm |
||
282 | * doing here is right. The reason I have the method at all is because |
||
283 | * psr7 StreamInterface has one.f |
||
284 | */ |
||
285 | 1 | public function detach() |
|
293 | |||
294 | /** |
||
295 | * Writability accessor. |
||
296 | * |
||
297 | * Despite the fact that any class that implements this interface must also |
||
298 | * define methods such as write and writeLine, that is no guarantee that an |
||
299 | * object will necessarily be writable. This method should tell the user |
||
300 | * whether a stream is, in fact, writable. |
||
301 | * |
||
302 | * @return boolean True if writable, false otherwise |
||
303 | */ |
||
304 | 1 | public function isWritable() |
|
308 | |||
309 | /** |
||
310 | * Write data to the output. |
||
311 | * |
||
312 | * @param string The data to write |
||
313 | * @return int|false The number of bytes written |
||
314 | */ |
||
315 | 2 | public function write($data) |
|
319 | |||
320 | /** |
||
321 | * Seekability accessor. |
||
322 | * |
||
323 | * Despite the fact that any class that implements this interface must also |
||
324 | * define methods such as seek, that is no guarantee that an |
||
325 | * object will necessarily be seekable. This method should tell the user |
||
326 | * whether a stream is, in fact, seekable. |
||
327 | * |
||
328 | * @return boolean True if seekable, false otherwise |
||
329 | */ |
||
330 | 2 | public function isSeekable() |
|
334 | |||
335 | /** |
||
336 | * Seek to specified offset. |
||
337 | * |
||
338 | * @param integer Offset to seek to |
||
339 | * @param integer Position from whence the offset should be applied |
||
340 | * @return boolean True if seek was successful |
||
341 | */ |
||
342 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
346 | |||
347 | } |
||
348 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: