1515class StreamCollection implements Countable, Iterator
1616{
1717 protected ErrorHandler $ handler ;
18- /** @var array<string, StreamInterface> */
18+ /** @var array<string, StreamInterface|StreamContainerInterface > */
1919 private array $ streams = [];
2020
2121 /**
@@ -31,12 +31,12 @@ public function __construct()
3131
3232 /**
3333 * Attach stream to collection.
34- * @param StreamInterface $attach Stream to attach.
34+ * @param StreamInterface|StreamContainerInterface $attach Stream to attach.
3535 * @param string|null $key Definable name of stream.
3636 * @return string Name of stream.
3737 * @throws StreamException If already attached.
3838 */
39- public function attach (StreamInterface $ attach , string |null $ key = null ): string
39+ public function attach (StreamInterface | StreamContainerInterface $ attach , string |null $ key = null ): string
4040 {
4141 if ($ key && array_key_exists ($ key , $ this ->streams )) {
4242 throw new StreamException (StreamException::COLLECT_KEY_CONFLICT , ['key ' => $ key ]);
@@ -48,10 +48,10 @@ public function attach(StreamInterface $attach, string|null $key = null): string
4848
4949 /**
5050 * Detach stream from collection.
51- * @param StreamInterface|string $detach Stream or name of stream to detach.
51+ * @param StreamInterface|StreamContainerInterface| string $detach Stream or name of stream to detach.
5252 * @return bool If a stream was detached.
5353 */
54- public function detach (StreamInterface |string $ detach ): bool
54+ public function detach (StreamInterface |StreamContainerInterface | string $ detach ): bool
5555 {
5656 if (is_string ($ detach )) {
5757 if (array_key_exists ($ detach , $ this ->streams )) {
@@ -78,7 +78,7 @@ public function getReadable(): self
7878 {
7979 $ readables = new self ();
8080 foreach ($ this ->streams as $ key => $ stream ) {
81- if ($ stream ->isReadable ()) {
81+ if ($ this -> getStream ( $ stream) ->isReadable ()) {
8282 $ readables ->attach ($ stream , $ key );
8383 }
8484 }
@@ -93,7 +93,7 @@ public function getWritable(): self
9393 {
9494 $ writables = new self ();
9595 foreach ($ this ->streams as $ key => $ stream ) {
96- if ($ stream ->isWritable ()) {
96+ if ($ this -> getStream ( $ stream) ->isWritable ()) {
9797 $ writables ->attach ($ stream , $ key );
9898 }
9999 }
@@ -116,8 +116,8 @@ public function waitRead(int|float $timeout = 60): self
116116
117117 $ read = [];
118118 foreach ($ this ->streams as $ key => $ stream ) {
119- if ($ stream ->isReadable ()) {
120- $ read [$ key ] = $ stream ->getResource ();
119+ if ($ this -> getStream ( $ stream) ->isReadable ()) {
120+ $ read [$ key ] = $ this -> getStream ( $ stream) ->getResource ();
121121 }
122122 }
123123 if (empty ($ read )) {
@@ -157,9 +157,9 @@ public function count(): int
157157
158158 /**
159159 * Return the current stream.
160- * @return StreamInterface|null Current stream.
160+ * @return StreamInterface|StreamContainerInterface| null Current stream.
161161 */
162- public function current (): StreamInterface |null
162+ public function current (): StreamInterface |StreamContainerInterface | null
163163 {
164164 return current ($ this ->streams ) ?: null ;
165165 }
@@ -212,4 +212,9 @@ protected function createKey(): string
212212 } while (array_key_exists ($ key , $ this ->streams ));
213213 return $ key ;
214214 }
215+
216+ protected function getStream (StreamInterface |StreamContainerInterface $ stream ): StreamInterface
217+ {
218+ return $ stream instanceof StreamInterface ? $ stream : $ stream ->getStream ();
219+ }
215220}
0 commit comments