1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the stream package |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Stream\Reader; |
9
|
|
|
|
10
|
|
|
use GravityMedia\Stream\Exception; |
11
|
|
|
use GravityMedia\Stream\StreamInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* String reader |
15
|
|
|
* |
16
|
|
|
* @package GravityMedia\Stream\Reader |
17
|
|
|
*/ |
18
|
|
|
class StringReader |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var StreamInterface |
22
|
|
|
*/ |
23
|
|
|
protected $stream; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $length; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get stream |
32
|
|
|
* |
33
|
|
|
* @throws Exception\BadMethodCallException An exception will be thrown when the stream was not set |
34
|
|
|
* |
35
|
|
|
* @return StreamInterface |
36
|
|
|
*/ |
37
|
9 |
|
public function getStream() |
38
|
|
|
{ |
39
|
9 |
|
if (null === $this->stream) { |
40
|
3 |
|
throw new Exception\BadMethodCallException('Stream not set'); |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
return $this->stream; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set stream |
48
|
|
|
* |
49
|
|
|
* @param StreamInterface $stream |
50
|
|
|
* |
51
|
|
|
* @throws Exception\BadMethodCallException An exception will be thrown for non-readable streams |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
9 |
|
public function setStream(StreamInterface $stream) |
56
|
|
|
{ |
57
|
9 |
|
if (!$stream->isReadable()) { |
58
|
3 |
|
throw new Exception\BadMethodCallException('Stream not readable'); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
$this->stream = $stream; |
62
|
|
|
|
63
|
6 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get length |
68
|
|
|
* |
69
|
|
|
* @throws Exception\BadMethodCallException An exception will be thrown when the length was not set |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
9 |
|
public function getLength() |
74
|
|
|
{ |
75
|
9 |
|
if (null === $this->length) { |
76
|
3 |
|
throw new Exception\BadMethodCallException('Length not set'); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
return $this->length; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set length |
84
|
|
|
* |
85
|
|
|
* @param int $length |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
6 |
|
public function setLength($length) |
90
|
|
|
{ |
91
|
6 |
|
$this->length = $length; |
92
|
|
|
|
93
|
6 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Read string data from the stream |
98
|
|
|
* |
99
|
|
|
* @throws Exception\BadMethodCallException An exception will be thrown when the stream or length was not set |
100
|
|
|
* @throws Exception\IOException An exception will be thrown for invalid stream resources or when the |
101
|
|
|
* data could not be read |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
3 |
|
public function read() |
106
|
|
|
{ |
107
|
3 |
|
$length = $this->getLength(); |
108
|
3 |
|
$stream = $this->getStream(); |
109
|
|
|
|
110
|
3 |
|
return $stream->read($length); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|