1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Frame\Value; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Transport\Frame\Value, |
8
|
|
|
Exception\UnboundedTextCannotBeWrapped, |
9
|
|
|
}; |
10
|
|
|
use Innmind\Math\Algebra\Integer; |
11
|
|
|
use Innmind\Stream\Readable; |
12
|
|
|
use Innmind\Immutable\{ |
13
|
|
|
Str, |
14
|
|
|
Sequence as Seq, |
15
|
|
|
MapInterface, |
16
|
|
|
Map, |
17
|
|
|
}; |
18
|
|
|
use function Innmind\Immutable\assertMap; |
19
|
|
|
|
20
|
|
|
final class Table implements Value |
21
|
|
|
{ |
22
|
|
|
private $value; |
23
|
|
|
private $original; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param MapInterface<string, Value> $map |
27
|
|
|
*/ |
28
|
132 |
|
public function __construct(MapInterface $map) |
29
|
|
|
{ |
30
|
132 |
|
assertMap('string', Value::class, $map, 1); |
31
|
|
|
|
32
|
|
|
$texts = $map->filter(static function(string $key, Value $value): bool { |
33
|
120 |
|
return $value instanceof Text; |
34
|
130 |
|
}); |
35
|
|
|
|
36
|
130 |
|
if ($texts->size() > 0) { |
37
|
2 |
|
throw new UnboundedTextCannotBeWrapped; |
38
|
|
|
} |
39
|
|
|
|
40
|
128 |
|
$this->original = $map; |
41
|
128 |
|
} |
42
|
|
|
|
43
|
104 |
|
public static function fromStream(Readable $stream): Value |
44
|
|
|
{ |
45
|
104 |
|
$length = UnsignedLongInteger::fromStream($stream)->original(); |
46
|
104 |
|
$position = $stream->position()->toInt(); |
47
|
104 |
|
$boundary = $position + $length->value(); |
48
|
|
|
|
49
|
104 |
|
$map = new Map('string', Value::class); |
50
|
|
|
|
51
|
104 |
|
while ($position < $boundary) { |
52
|
98 |
|
$key = ShortString::fromStream($stream)->original(); |
53
|
98 |
|
$class = Symbols::class((string) $stream->read(1)); |
54
|
|
|
|
55
|
98 |
|
$map = $map->put((string) $key, [$class, 'fromStream']($stream)); |
56
|
|
|
|
57
|
98 |
|
$position = $stream->position()->toInt(); |
58
|
|
|
} |
59
|
|
|
|
60
|
104 |
|
return new self($map); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return MapInterface<string, Value> |
65
|
|
|
*/ |
66
|
26 |
|
public function original(): MapInterface |
67
|
|
|
{ |
68
|
26 |
|
return $this->original; |
69
|
|
|
} |
70
|
|
|
|
71
|
126 |
|
public function __toString(): string |
72
|
|
|
{ |
73
|
126 |
|
if (\is_null($this->value)) { |
74
|
|
|
$data = $this |
75
|
126 |
|
->original |
76
|
126 |
|
->reduce( |
77
|
126 |
|
new Seq, |
78
|
|
|
static function(Seq $sequence, string $key, Value $value): Seq { |
79
|
|
|
return $sequence |
80
|
118 |
|
->add(new ShortString(new Str($key))) |
81
|
118 |
|
->add(Symbols::symbol(\get_class($value))) |
82
|
118 |
|
->add($value); |
83
|
126 |
|
} |
84
|
|
|
) |
85
|
126 |
|
->join('') |
86
|
126 |
|
->toEncoding('ASCII'); |
87
|
|
|
|
88
|
126 |
|
$this->value = (string) UnsignedLongInteger::of( |
89
|
126 |
|
new Integer($data->length()) |
90
|
|
|
); |
91
|
126 |
|
$this->value .= $data; |
92
|
|
|
} |
93
|
|
|
|
94
|
126 |
|
return $this->value; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|