|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Async sockets |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace AsyncSockets\RequestExecutor\Pipeline; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PushbackIterator |
|
15
|
|
|
*/ |
|
16
|
|
|
class PushbackIterator implements \Iterator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Nested iterator |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Iterator |
|
22
|
|
|
*/ |
|
23
|
|
|
private $nestedIterator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Unread item from previous iteration |
|
27
|
|
|
* |
|
28
|
|
|
* @var string[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $unreadItems; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Result of previous iteration |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $lastIterationResult; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* String length to return from this iterator |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
private $chunkSize; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Iteration key |
|
48
|
|
|
* |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
private $key; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Length of last result push back |
|
55
|
|
|
* |
|
56
|
|
|
* @var int |
|
57
|
|
|
*/ |
|
58
|
|
|
private $backLength; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* PushbackIterator constructor. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \Iterator $nestedIterator Nested iterator |
|
64
|
|
|
* @param int $chunkSize Max length of string to return |
|
65
|
|
|
*/ |
|
66
|
36 |
|
public function __construct(\Iterator $nestedIterator, $chunkSize) |
|
67
|
|
|
{ |
|
68
|
36 |
|
$this->nestedIterator = $nestedIterator; |
|
69
|
36 |
|
$this->chunkSize = $chunkSize; |
|
70
|
36 |
|
$this->unreadItems = []; |
|
71
|
36 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritDoc |
|
75
|
|
|
*/ |
|
76
|
18 |
|
public function current() |
|
77
|
|
|
{ |
|
78
|
18 |
|
return $this->lastIterationResult; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritDoc |
|
83
|
|
|
*/ |
|
84
|
15 |
|
public function next() |
|
85
|
|
|
{ |
|
86
|
15 |
|
++$this->key; |
|
87
|
|
|
|
|
88
|
15 |
|
$hasUnreadItem = $this->hasUnreadItem(); |
|
89
|
15 |
|
if (!$hasUnreadItem && !$this->backLength) { |
|
90
|
8 |
|
$this->nestedIterator->next(); |
|
91
|
8 |
|
if (!$this->nestedIterator->valid()) { |
|
92
|
8 |
|
$this->lastIterationResult = ''; |
|
93
|
8 |
|
return; |
|
94
|
|
|
} |
|
95
|
4 |
|
} |
|
96
|
|
|
|
|
97
|
11 |
|
$this->lastIterationResult = $this->buildChunk(); |
|
98
|
11 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
4 |
|
public function key() |
|
104
|
|
|
{ |
|
105
|
4 |
|
return $this->key; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
40 |
|
public function valid() |
|
112
|
|
|
{ |
|
113
|
40 |
|
return !empty($this->lastIterationResult) || $this->hasUnreadItem() || $this->nestedIterator->valid(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritDoc |
|
118
|
|
|
*/ |
|
119
|
36 |
|
public function rewind() |
|
120
|
|
|
{ |
|
121
|
36 |
|
$this->key = 0; |
|
122
|
36 |
|
$this->backLength = 0; |
|
123
|
36 |
|
$this->unreadItems = []; |
|
124
|
36 |
|
$this->nestedIterator->rewind(); |
|
125
|
36 |
|
$this->lastIterationResult = $this->buildChunk(); |
|
126
|
36 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Return given length of previously read string back into iterator |
|
130
|
|
|
* |
|
131
|
|
|
* @param int $length Length of string to return back |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
7 |
|
public function unread($length) |
|
136
|
|
|
{ |
|
137
|
7 |
|
if (empty($this->lastIterationResult) || $length <= 0) { |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
7 |
|
$itemLength = strlen($this->lastIterationResult); |
|
142
|
7 |
|
$this->backLength = min($itemLength, $this->backLength + $length); |
|
143
|
7 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Test if iterator has unread item |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
40 |
|
private function hasUnreadItem() |
|
151
|
|
|
{ |
|
152
|
40 |
|
return !empty($this->unreadItems); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Build chunk to return to user |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
36 |
|
private function buildChunk() |
|
161
|
|
|
{ |
|
162
|
36 |
|
$result = ''; |
|
163
|
36 |
|
if ($this->backLength) { |
|
164
|
7 |
|
$backItem = substr( |
|
165
|
7 |
|
$this->lastIterationResult, |
|
166
|
7 |
|
strlen($this->lastIterationResult) - $this->backLength, |
|
167
|
7 |
|
$this->backLength |
|
168
|
7 |
|
); |
|
169
|
|
|
|
|
170
|
7 |
|
array_unshift($this->unreadItems, $backItem); |
|
171
|
7 |
|
$this->backLength = 0; |
|
172
|
7 |
|
} |
|
173
|
|
|
|
|
174
|
36 |
|
$result .= $this->hasUnreadItem() ? |
|
175
|
36 |
|
array_pop($this->unreadItems) : |
|
176
|
36 |
|
(string) $this->nestedIterator->current(); |
|
177
|
|
|
|
|
178
|
36 |
|
while ($this->hasUnreadItem() && strlen($result) < $this->chunkSize) { |
|
179
|
2 |
|
$result .= array_pop($this->unreadItems); |
|
180
|
2 |
|
} |
|
181
|
|
|
|
|
182
|
36 |
|
while (strlen($result) < $this->chunkSize && $this->nestedIterator->valid()) { |
|
183
|
15 |
|
$this->nestedIterator->next(); |
|
184
|
15 |
|
$result .= (string) $this->nestedIterator->current(); |
|
185
|
15 |
|
} |
|
186
|
|
|
|
|
187
|
36 |
|
$split = str_split($result, $this->chunkSize); |
|
188
|
36 |
|
$result = array_shift($split); |
|
189
|
36 |
|
if (!empty($split)) { |
|
190
|
4 |
|
$this->unreadItems = array_merge($split, $this->unreadItems); |
|
|
|
|
|
|
191
|
4 |
|
} |
|
192
|
|
|
|
|
193
|
36 |
|
return $result; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @inheritDoc |
|
198
|
|
|
*/ |
|
199
|
3 |
|
public function __toString() |
|
200
|
|
|
{ |
|
201
|
3 |
|
return $this->valid() ? $this->current() : ''; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..