|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saxulum\HttpMessage; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
|
6
|
|
|
|
|
7
|
|
|
final class SimpleStream implements StreamInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $stream = ''; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
private $pointer = 0; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __toString(): string |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->stream; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function close() |
|
31
|
|
|
{ |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function detach() |
|
38
|
|
|
{ |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getSize(): int |
|
45
|
|
|
{ |
|
46
|
|
|
return strlen($this->stream); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function tell(): int |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->pointer; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function eof(): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->pointer + 1 === $this->getSize(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isSeekable(): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function seek($offset, $whence = SEEK_SET) |
|
77
|
|
|
{ |
|
78
|
|
|
if (SEEK_SET === $whence) { |
|
79
|
|
|
$this->pointer = $offset; |
|
80
|
|
|
} elseif (SEEK_CUR === $whence) { |
|
81
|
|
|
$this->pointer += $offset; |
|
82
|
|
|
} elseif (SEEK_END === $whence) { |
|
83
|
|
|
$this->pointer = $this->getSize() - 1 + $offset; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function rewind() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->pointer = 0; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isWritable(): bool |
|
99
|
|
|
{ |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function write($string): int |
|
107
|
|
|
{ |
|
108
|
|
|
$stringLength = strlen($string); |
|
109
|
|
|
for ($i = 0; $i < $stringLength; ++$i) { |
|
110
|
|
|
$this->stream[$this->pointer] = $string[$i]; |
|
111
|
|
|
++$this->pointer; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $stringLength; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function isReadable(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
|
|
public function read($length): string |
|
129
|
|
|
{ |
|
130
|
|
|
$read = ''; |
|
131
|
|
|
for ($i = 0; $i < $length; ++$i) { |
|
132
|
|
|
$read .= $this->stream[$this->pointer + $i] ?? ''; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $read; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getContents(): string |
|
142
|
|
|
{ |
|
143
|
|
|
$contents = ''; |
|
144
|
|
|
$size = $this->getSize(); |
|
145
|
|
|
|
|
146
|
|
|
while ($this->pointer < $size) { |
|
147
|
|
|
$contents .= $this->stream[$this->pointer]; |
|
148
|
|
|
++$this->pointer; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $contents; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getMetadata($key = null) |
|
158
|
|
|
{ |
|
159
|
|
|
$metadata = [ |
|
160
|
|
|
'timed_out' => false, |
|
161
|
|
|
'blocked' => false, |
|
162
|
|
|
'eof' => $this->eof(), |
|
163
|
|
|
'unread_bytes' => $this->getSize() - $this->pointer - 1, |
|
164
|
|
|
'mode' => 'rw', |
|
165
|
|
|
'seekable' => true, |
|
166
|
|
|
]; |
|
167
|
|
|
|
|
168
|
|
|
if (null !== $key) { |
|
169
|
|
|
return $metadata[$key] ?? null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $metadata; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.