1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Http\Psr\Stream; |
3
|
|
|
|
4
|
|
|
use IteratorAggregate; |
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use Traversable; |
8
|
|
|
|
9
|
|
|
class IteratorStream implements StreamInterface, IteratorAggregate |
10
|
|
|
{ |
11
|
|
|
/** @var \Iterator */ |
12
|
|
|
protected $iterator; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $cachedContents; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
protected $isEof = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \Traversable $iterator |
22
|
|
|
*/ |
23
|
9 |
|
public function __construct(Traversable $iterator) |
24
|
|
|
{ |
25
|
9 |
|
$this->iterator = $iterator; |
|
|
|
|
26
|
9 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
5 |
|
public function __toString() |
32
|
|
|
{ |
33
|
5 |
|
$this->rewind(); |
34
|
5 |
|
return $this->getContents(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function getIterator() |
41
|
|
|
{ |
42
|
|
|
return $this->iterator; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function close() |
49
|
|
|
{ |
50
|
|
|
throw new RuntimeException('can not use the close method.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function detach() |
57
|
|
|
{ |
58
|
|
|
throw new RuntimeException('can not use the detach method.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getSize() |
65
|
|
|
{ |
66
|
|
|
throw new RuntimeException('IteratorStream cannot getSize.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function tell() |
73
|
|
|
{ |
74
|
|
|
throw new RuntimeException('IteratorStream cannot tell.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
6 |
|
public function eof() |
81
|
|
|
{ |
82
|
6 |
|
return $this->isEof; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
1 |
|
public function isSeekable() |
89
|
|
|
{ |
90
|
1 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function seek($offset, $whence = SEEK_SET) |
97
|
|
|
{ |
98
|
1 |
|
throw new RuntimeException('IteratorStream cannot seek.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
6 |
|
public function rewind() |
105
|
|
|
{ |
106
|
6 |
|
$this->isEof = false; |
107
|
6 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function isWritable() |
113
|
|
|
{ |
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function write($string) |
121
|
|
|
{ |
122
|
1 |
|
throw new RuntimeException('IteratorStream cannot write.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function isReadable() |
129
|
|
|
{ |
130
|
1 |
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function read($length) |
137
|
|
|
{ |
138
|
1 |
|
throw new RuntimeException('IteratorStream cannot read.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
6 |
|
public function getContents() |
145
|
|
|
{ |
146
|
6 |
|
if (!$this->eof()) { |
147
|
6 |
|
if (!isset($this->cachedContents)) { |
148
|
6 |
|
$contents = ''; |
149
|
6 |
|
foreach ($this->iterator as $value) { |
150
|
6 |
|
$contents .= $value; |
151
|
|
|
} |
152
|
6 |
|
$this->cachedContents = $contents; |
153
|
|
|
} |
154
|
6 |
|
$this->isEof = true; |
155
|
6 |
|
return $this->cachedContents; |
156
|
|
|
} |
157
|
1 |
|
return ''; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function getMetadata($key = null) |
164
|
|
|
{ |
165
|
|
|
$metadata = [ |
166
|
|
|
'eof' => $this->eof(), |
167
|
|
|
'stream_type' => 'iterator', |
168
|
|
|
'seekable' => false |
169
|
|
|
]; |
170
|
|
|
if (!isset($key)) { |
171
|
|
|
return $metadata; |
172
|
|
|
} |
173
|
|
|
if (!array_key_exists($key, $metadata)) { |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
return $metadata[$key]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.