Skip to content

Commit d0c936a

Browse files
committed
Update Stream class.
1 parent e98a757 commit d0c936a

1 file changed

Lines changed: 23 additions & 23 deletions

File tree

src/Stream.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Swlib\Http;
34

45
use Exception;
@@ -13,6 +14,17 @@
1314
*/
1415
class Stream implements StreamInterface
1516
{
17+
/**
18+
* Resource modes.
19+
*
20+
* @var string
21+
*
22+
* @see http://php.net/manual/function.fopen.php
23+
* @see http://php.net/manual/en/function.gzopen.php
24+
*/
25+
const READABLE_MODES = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/';
26+
const WRITABLE_MODES = '/a|w|r\+|rb\+|rw|x|c/';
27+
1628
private $stream;
1729
private $size;
1830
private $seekable;
@@ -21,22 +33,6 @@ class Stream implements StreamInterface
2133
private $uri;
2234
private $customMetadata;
2335

24-
/** @var array Hash of readable and writable stream types */
25-
private static $readWriteHash = [
26-
'read' => [
27-
'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
28-
'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
29-
'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
30-
'x+t' => true, 'c+t' => true, 'a+' => true
31-
],
32-
'write' => [
33-
'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
34-
'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
35-
'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
36-
'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true
37-
]
38-
];
39-
4036
/**
4137
* This constructor accepts an associative array of options.
4238
*
@@ -46,8 +42,8 @@ class Stream implements StreamInterface
4642
* - metadata: (array) Any additional metadata to return when the metadata
4743
* of the stream is accessed.
4844
*
49-
* @param resource $stream Stream resource to wrap.
50-
* @param array $options Associative array of options.
45+
* @param resource $stream Stream resource to wrap.
46+
* @param array $options Associative array of options.
5147
*
5248
* @throws InvalidArgumentException if the stream is not a stream resource
5349
*/
@@ -68,8 +64,8 @@ public function __construct($stream, $options = [])
6864
$this->stream = $stream;
6965
$meta = stream_get_meta_data($this->stream);
7066
$this->seekable = $meta['seekable'];
71-
$this->readable = isset(self::$readWriteHash['read'][$meta['mode']]);
72-
$this->writable = isset(self::$readWriteHash['write'][$meta['mode']]);
67+
$this->readable = (bool)preg_match(self::READABLE_MODES, $meta['mode']);
68+
$this->writable = (bool)preg_match(self::WRITABLE_MODES, $meta['mode']);
7369
$this->uri = $this->getMetadata('uri');
7470
}
7571

@@ -85,7 +81,7 @@ public function __toString()
8581
{
8682
try {
8783
$this->seek(0);
88-
return (string) stream_get_contents($this->stream);
84+
return (string)stream_get_contents($this->stream);
8985
} catch (Exception $e) {
9086
return '';
9187
}
@@ -200,15 +196,19 @@ public function rewind()
200196

201197
public function seek($offset, $whence = SEEK_SET)
202198
{
199+
$whence = (int)$whence;
200+
203201
if (!isset($this->stream)) {
204202
throw new RuntimeException('Stream is detached');
205203
}
206204
if (!$this->seekable) {
207205
throw new RuntimeException('Stream is not seekable');
208206
}
209207
if (fseek($this->stream, $offset, $whence) === -1) {
210-
throw new RuntimeException('Unable to seek to stream position '
211-
. $offset . ' with whence ' . var_export($whence, true));
208+
throw new RuntimeException(
209+
'Unable to seek to stream position '
210+
. $offset . ' with whence ' . var_export($whence, true)
211+
);
212212
}
213213
}
214214

0 commit comments

Comments
 (0)