Completed
Push — master ( 16c8a5...965897 )
by Michael
02:58
created
src/Stream.php 1 patch
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -20,223 +20,223 @@
 block discarded – undo
20 20
  */
21 21
 class Stream implements StreamInterface
22 22
 {
23
-	/** @var resource The resource. */
24
-	private $resource;
25
-
26
-	/** @var array The metadata. */
27
-	private $metadata;
28
-
29
-	/** @var string[] The read modes. */
30
-	private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+'];
31
-
32
-	/** @var string[] The write modes. */
33
-	private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+'];
34
-
35
-	/**
36
-	 * Construct a Stream object with the given resource.
37
-	 *
38
-	 * @param resource $resource
39
-	 */
40
-	public function __construct($resource)
41
-	{
42
-		if (!is_resource($resource)) {
43
-			throw new \InvalidArgumentException('Invalid resource');
44
-		}
45
-
46
-		$this->resource = $resource;
47
-		$this->metadata = stream_get_meta_data($resource);
48
-	}
49
-
50
-	/**
51
-	 * Destruct the Stream object.
52
-	 */
53
-	public function __destruct()
54
-	{
55
-		$this->close();
56
-	}
57
-
58
-	/**
59
-	 * {@inheritdoc}
60
-	 */
61
-	public function __toString()
62
-	{
63
-		try {
64
-			$this->seek(0);
65
-
66
-			return $this->getContents();
67
-		} catch (\Exception $e) {
68
-			return '';
69
-		}
70
-	}
71
-
72
-	/**
73
-	 * {@inheritdoc}
74
-	 */
75
-	public function close()
76
-	{
77
-		if (isset($this->resource)) {
78
-			if (is_resource($this->resource)) {
79
-				fclose($this->resource);
80
-			}
81
-
82
-			$this->detach();
83
-		}
84
-	}
85
-
86
-	/**
87
-	 * {@inheritdoc}
88
-	 */
89
-	public function detach()
90
-	{
91
-		if (!isset($this->resource)) {
92
-			return null;
93
-		}
94
-
95
-		$result = $this->resource;
96
-		unset($this->resource);
97
-
98
-		return $result;
99
-	}
100
-
101
-	/**
102
-	 * {@inheritdoc}
103
-	 */
104
-	public function getSize()
105
-	{
106
-		if (!isset($this->resource)) {
107
-			return null;
108
-		}
109
-
110
-		if ($this->getMetadata('uri')) {
111
-			clearstatcache(true, $this->getMetadata('uri'));
112
-		}
113
-
114
-		$stats = fstat($this->resource);
115
-
116
-		return isset($stats['size']) ? $stats['size'] : null;
117
-	}
118
-
119
-	/**
120
-	 * {@inheritdoc}
121
-	 */
122
-	public function tell()
123
-	{
124
-		$result = ftell($this->resource);
125
-
126
-		if ($result === false) {
127
-			throw new \RuntimeException('Error while getting the position of the pointer');
128
-		}
129
-
130
-		return $result;
131
-	}
132
-
133
-	/**
134
-	 * {@inheritdoc}
135
-	 */
136
-	public function eof()
137
-	{
138
-		return isset($this->resource) && feof($this->resource);
139
-	}
140
-
141
-	/**
142
-	 * {@inheritdoc}
143
-	 */
144
-	public function isSeekable()
145
-	{
146
-		return isset($this->resource) && $this->getMetadata('seekable');
147
-	}
148
-
149
-	/**
150
-	 * {@inheritdoc}
151
-	 */
152
-	public function seek($offset, $whence = SEEK_SET)
153
-	{
154
-		if (!$this->isSeekable()) {
155
-			throw new \RuntimeException('Stream is not seekable');
156
-		}
157
-
158
-		if (fseek($this->resource, $offset, $whence) === false) {
159
-			throw new \RuntimeException('Error while seeking the stream');
160
-		}
161
-	}
162
-
163
-	/**
164
-	 * {@inheritdoc}
165
-	 */
166
-	public function rewind()
167
-	{
168
-		$this->seek(0);
169
-	}
170
-
171
-	/**
172
-	 * {@inheritdoc}
173
-	 */
174
-	public function isWritable()
175
-	{
176
-		return isset($this->resource) && in_array($this->getMetadata('mode'), self::$writeModes);
177
-	}
178
-
179
-	/**
180
-	 * {@inheritdoc}
181
-	 */
182
-	public function write($string)
183
-	{
184
-		if (!$this->isWritable()) {
185
-			throw new \RuntimeException('Stream is not writable');
186
-		}
187
-
188
-		$result = fwrite($this->resource, $string);
189
-
190
-		if ($result === false) {
191
-			throw new \RuntimeException('Error while writing the stream');
192
-		}
193
-
194
-		return $result;
195
-	}
196
-
197
-	/**
198
-	 * {@inheritdoc}
199
-	 */
200
-	public function isReadable()
201
-	{
202
-		return isset($this->resource) && in_array($this->getMetadata('mode'), self::$readModes);
203
-	}
204
-
205
-	/**
206
-	 * {@inheritdoc}
207
-	 */
208
-	public function read($length)
209
-	{
210
-		if (!$this->isReadable()) {
211
-			throw new \RuntimeException('Stream is not readable');
212
-		}
213
-
214
-		$result = stream_get_contents($this->resource, $length);
215
-
216
-		if ($result === false) {
217
-			throw new \RuntimeException('Error while reading the stream');
218
-		}
219
-
220
-		return $result;
221
-	}
222
-
223
-	/**
224
-	 * {@inheritdoc}
225
-	 */
226
-	public function getContents()
227
-	{
228
-		return $this->read(-1);
229
-	}
230
-
231
-	/**
232
-	 * {@inheritdoc}
233
-	 */
234
-	public function getMetadata($key = null)
235
-	{
236
-		if ($key === null) {
237
-			return $this->metadata;
238
-		}
239
-
240
-		return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
241
-	}
23
+    /** @var resource The resource. */
24
+    private $resource;
25
+
26
+    /** @var array The metadata. */
27
+    private $metadata;
28
+
29
+    /** @var string[] The read modes. */
30
+    private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+'];
31
+
32
+    /** @var string[] The write modes. */
33
+    private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+'];
34
+
35
+    /**
36
+     * Construct a Stream object with the given resource.
37
+     *
38
+     * @param resource $resource
39
+     */
40
+    public function __construct($resource)
41
+    {
42
+        if (!is_resource($resource)) {
43
+            throw new \InvalidArgumentException('Invalid resource');
44
+        }
45
+
46
+        $this->resource = $resource;
47
+        $this->metadata = stream_get_meta_data($resource);
48
+    }
49
+
50
+    /**
51
+     * Destruct the Stream object.
52
+     */
53
+    public function __destruct()
54
+    {
55
+        $this->close();
56
+    }
57
+
58
+    /**
59
+     * {@inheritdoc}
60
+     */
61
+    public function __toString()
62
+    {
63
+        try {
64
+            $this->seek(0);
65
+
66
+            return $this->getContents();
67
+        } catch (\Exception $e) {
68
+            return '';
69
+        }
70
+    }
71
+
72
+    /**
73
+     * {@inheritdoc}
74
+     */
75
+    public function close()
76
+    {
77
+        if (isset($this->resource)) {
78
+            if (is_resource($this->resource)) {
79
+                fclose($this->resource);
80
+            }
81
+
82
+            $this->detach();
83
+        }
84
+    }
85
+
86
+    /**
87
+     * {@inheritdoc}
88
+     */
89
+    public function detach()
90
+    {
91
+        if (!isset($this->resource)) {
92
+            return null;
93
+        }
94
+
95
+        $result = $this->resource;
96
+        unset($this->resource);
97
+
98
+        return $result;
99
+    }
100
+
101
+    /**
102
+     * {@inheritdoc}
103
+     */
104
+    public function getSize()
105
+    {
106
+        if (!isset($this->resource)) {
107
+            return null;
108
+        }
109
+
110
+        if ($this->getMetadata('uri')) {
111
+            clearstatcache(true, $this->getMetadata('uri'));
112
+        }
113
+
114
+        $stats = fstat($this->resource);
115
+
116
+        return isset($stats['size']) ? $stats['size'] : null;
117
+    }
118
+
119
+    /**
120
+     * {@inheritdoc}
121
+     */
122
+    public function tell()
123
+    {
124
+        $result = ftell($this->resource);
125
+
126
+        if ($result === false) {
127
+            throw new \RuntimeException('Error while getting the position of the pointer');
128
+        }
129
+
130
+        return $result;
131
+    }
132
+
133
+    /**
134
+     * {@inheritdoc}
135
+     */
136
+    public function eof()
137
+    {
138
+        return isset($this->resource) && feof($this->resource);
139
+    }
140
+
141
+    /**
142
+     * {@inheritdoc}
143
+     */
144
+    public function isSeekable()
145
+    {
146
+        return isset($this->resource) && $this->getMetadata('seekable');
147
+    }
148
+
149
+    /**
150
+     * {@inheritdoc}
151
+     */
152
+    public function seek($offset, $whence = SEEK_SET)
153
+    {
154
+        if (!$this->isSeekable()) {
155
+            throw new \RuntimeException('Stream is not seekable');
156
+        }
157
+
158
+        if (fseek($this->resource, $offset, $whence) === false) {
159
+            throw new \RuntimeException('Error while seeking the stream');
160
+        }
161
+    }
162
+
163
+    /**
164
+     * {@inheritdoc}
165
+     */
166
+    public function rewind()
167
+    {
168
+        $this->seek(0);
169
+    }
170
+
171
+    /**
172
+     * {@inheritdoc}
173
+     */
174
+    public function isWritable()
175
+    {
176
+        return isset($this->resource) && in_array($this->getMetadata('mode'), self::$writeModes);
177
+    }
178
+
179
+    /**
180
+     * {@inheritdoc}
181
+     */
182
+    public function write($string)
183
+    {
184
+        if (!$this->isWritable()) {
185
+            throw new \RuntimeException('Stream is not writable');
186
+        }
187
+
188
+        $result = fwrite($this->resource, $string);
189
+
190
+        if ($result === false) {
191
+            throw new \RuntimeException('Error while writing the stream');
192
+        }
193
+
194
+        return $result;
195
+    }
196
+
197
+    /**
198
+     * {@inheritdoc}
199
+     */
200
+    public function isReadable()
201
+    {
202
+        return isset($this->resource) && in_array($this->getMetadata('mode'), self::$readModes);
203
+    }
204
+
205
+    /**
206
+     * {@inheritdoc}
207
+     */
208
+    public function read($length)
209
+    {
210
+        if (!$this->isReadable()) {
211
+            throw new \RuntimeException('Stream is not readable');
212
+        }
213
+
214
+        $result = stream_get_contents($this->resource, $length);
215
+
216
+        if ($result === false) {
217
+            throw new \RuntimeException('Error while reading the stream');
218
+        }
219
+
220
+        return $result;
221
+    }
222
+
223
+    /**
224
+     * {@inheritdoc}
225
+     */
226
+    public function getContents()
227
+    {
228
+        return $this->read(-1);
229
+    }
230
+
231
+    /**
232
+     * {@inheritdoc}
233
+     */
234
+    public function getMetadata($key = null)
235
+    {
236
+        if ($key === null) {
237
+            return $this->metadata;
238
+        }
239
+
240
+        return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
241
+    }
242 242
 }
Please login to merge, or discard this patch.