Skip to content

Commit 62d4c0e

Browse files
Replaced Generator with iterator. Renamed map to dictionary in unpacker.
1 parent 56f7d92 commit 62d4c0e

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

src/PackStream/IPacker.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Bolt\PackStream;
44

5-
use Generator;
6-
75
/**
86
* Interface IPacker
97
*
@@ -16,7 +14,7 @@ interface IPacker
1614
/**
1715
* @param $signature
1816
* @param mixed ...$params
19-
* @return Generator
17+
* @return iterable
2018
*/
21-
public function pack($signature, ...$params): Generator;
19+
public function pack($signature, ...$params): iterable;
2220
}

src/PackStream/v1/Packer.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Bolt\PackStream\IPacker;
66
use Bolt\error\PackException;
77
use Bolt\PackStream\{IPackListGenerator, IPackDictionaryGenerator};
8-
use Generator;
98
use Bolt\structures\{
109
IStructure,
1110
Date,
@@ -52,10 +51,10 @@ class Packer implements IPacker
5251
* Pack message with parameters
5352
* @param $signature
5453
* @param mixed ...$params
55-
* @return Generator
54+
* @return iterable
5655
* @throws PackException
5756
*/
58-
public function pack($signature, ...$params): Generator
57+
public function pack($signature, ...$params): iterable
5958
{
6059
$this->littleEndian = unpack('S', "\x01\x00")[1] === 1;
6160

@@ -91,9 +90,10 @@ public function pack($signature, ...$params): Generator
9190

9291
/**
9392
* @param mixed $param
93+
* @return iterable
9494
* @throws PackException
9595
*/
96-
private function p($param): Generator
96+
private function p($param): iterable
9797
{
9898
switch (gettype($param)) {
9999
case 'integer':
@@ -139,10 +139,10 @@ private function p($param): Generator
139139

140140
/**
141141
* @param string $str
142-
* @return Generator
142+
* @return iterable
143143
* @throws PackException
144144
*/
145-
private function packString(string $str): Generator
145+
private function packString(string $str): iterable
146146
{
147147
$length = mb_strlen($str, '8bit');
148148

@@ -161,20 +161,20 @@ private function packString(string $str): Generator
161161

162162
/**
163163
* @param float $value
164-
* @return Generator
164+
* @return iterable
165165
*/
166-
private function packFloat(float $value): Generator
166+
private function packFloat(float $value): iterable
167167
{
168168
$packed = pack('d', $value);
169169
yield chr(0xC1) . ($this->littleEndian ? strrev($packed) : $packed);
170170
}
171171

172172
/**
173173
* @param int $value
174-
* @return Generator
174+
* @return iterable
175175
* @throws PackException
176176
*/
177-
private function packInteger(int $value): Generator
177+
private function packInteger(int $value): iterable
178178
{
179179
if ($value >= -16 && $value <= 127) { //TINY_INT
180180
yield pack('c', $value);
@@ -196,10 +196,10 @@ private function packInteger(int $value): Generator
196196

197197
/**
198198
* @param array|IPackDictionaryGenerator $param
199-
* @return Generator
199+
* @return iterable
200200
* @throws PackException
201201
*/
202-
private function packDictionary($param): Generator
202+
private function packDictionary($param): iterable
203203
{
204204
$size = is_array($param) ? count($param) : $param->count();
205205

@@ -223,10 +223,10 @@ private function packDictionary($param): Generator
223223

224224
/**
225225
* @param array|IPackListGenerator $param
226-
* @return Generator
226+
* @return iterable
227227
* @throws PackException
228228
*/
229-
private function packList($param): Generator
229+
private function packList($param): iterable
230230
{
231231
$size = is_array($param) ? count($param) : $param->count();
232232

@@ -249,10 +249,10 @@ private function packList($param): Generator
249249

250250
/**
251251
* @param IStructure $structure
252-
* @return Generator
252+
* @return iterable
253253
* @throws PackException
254254
*/
255-
private function packStructure(IStructure $structure): Generator
255+
private function packStructure(IStructure $structure): iterable
256256
{
257257
$arr = $this->structuresLt[get_class($structure)] ?? null;
258258
if ($arr === null) {
@@ -268,10 +268,10 @@ private function packStructure(IStructure $structure): Generator
268268

269269
/**
270270
* @param Bytes $bytes
271-
* @return Generator
271+
* @return iterable
272272
* @throws PackException
273273
*/
274-
private function packByteArray(Bytes $bytes): Generator
274+
private function packByteArray(Bytes $bytes): iterable
275275
{
276276
$size = count($bytes);
277277
if ($size < self::MEDIUM) {

src/PackStream/v1/Unpacker.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class Unpacker implements IUnpacker
3737
private int $signature;
3838

3939
private array $structuresLt = [
40-
0x4E => [Node::class, 'unpackInteger', 'unpackList', 'unpackMap'],
41-
0x52 => [Relationship::class, 'unpackInteger', 'unpackInteger', 'unpackInteger', 'unpackString', 'unpackMap'],
42-
0x72 => [UnboundRelationship::class, 'unpackInteger', 'unpackString', 'unpackMap'],
40+
0x4E => [Node::class, 'unpackInteger', 'unpackList', 'unpackDictionary'],
41+
0x52 => [Relationship::class, 'unpackInteger', 'unpackInteger', 'unpackInteger', 'unpackString', 'unpackDictionary'],
42+
0x72 => [UnboundRelationship::class, 'unpackInteger', 'unpackString', 'unpackDictionary'],
4343
0x50 => [Path::class, 'unpackList', 'unpackList', 'unpackList'],
4444
0x44 => [Date::class, 'unpackInteger'],
4545
0x54 => [Time::class, 'unpackInteger', 'unpackInteger'],
@@ -123,7 +123,7 @@ private function u()
123123
if ($output !== null) {
124124
return $output;
125125
}
126-
$output = $this->unpackMap($marker);
126+
$output = $this->unpackDictionary($marker);
127127
if ($output !== null) {
128128
return $output;
129129
}
@@ -194,15 +194,15 @@ private function unpackSpecificStructure(string $class, string ...$methods): ISt
194194
* @return array|null
195195
* @throws UnpackException
196196
*/
197-
private function unpackMap(int $marker): ?array
197+
private function unpackDictionary(int $marker): ?array
198198
{
199-
if ($marker >> 4 == 0b1010) { //TINY_MAP
199+
if ($marker >> 4 == 0b1010) { //TINY_DICT
200200
$size = 0b10100000 ^ $marker;
201-
} elseif ($marker == 0xD8) { //MAP_8
201+
} elseif ($marker == 0xD8) { //DICT_8
202202
$size = (int)unpack('C', $this->next(1))[1];
203-
} elseif ($marker == 0xD9) { //MAP_16
203+
} elseif ($marker == 0xD9) { //DICT_16
204204
$size = (int)unpack('n', $this->next(2))[1];
205-
} elseif ($marker == 0xDA) { //MAP_32
205+
} elseif ($marker == 0xDA) { //DICT_32
206206
$size = (int)unpack('N', $this->next(4))[1];
207207
} else {
208208
return null;

src/protocol/AProtocol.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Bolt\protocol;
44

5-
use Generator;
65
use Bolt\PackStream\{IPacker, IUnpacker};
76
use Bolt\connection\IConnection;
87
use Exception;
@@ -40,7 +39,7 @@ public function __construct(IPacker $packer, IUnpacker $unpacker, IConnection $c
4039

4140
/**
4241
* Write to connection
43-
* @param Generator $generator
42+
* @param iterable $generator
4443
* @throws Exception
4544
*/
4645
protected function write(iterable $generator)

0 commit comments

Comments
 (0)