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
|
|
|
|
19
|
|
|
final class Table implements Value |
20
|
|
|
{ |
21
|
|
|
private $value; |
22
|
|
|
private $original; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param MapInterface<string, Value> $map |
26
|
|
|
*/ |
27
|
66 |
|
public function __construct(MapInterface $map) |
28
|
|
|
{ |
29
|
|
|
if ( |
30
|
66 |
|
(string) $map->keyType() !== 'string' || |
31
|
66 |
|
(string) $map->valueType() !== Value::class |
32
|
|
|
) { |
33
|
1 |
|
throw new \TypeError(sprintf( |
34
|
1 |
|
'Argument 1 must be of type MapInterface<string, %s>', |
35
|
1 |
|
Value::class |
36
|
|
|
)); |
37
|
|
|
} |
38
|
|
|
|
39
|
65 |
|
$texts = $map->filter(static function(string $key, Value $value): bool { |
40
|
60 |
|
return $value instanceof Text; |
41
|
65 |
|
}); |
42
|
|
|
|
43
|
65 |
|
if ($texts->size() > 0) { |
44
|
1 |
|
throw new UnboundedTextCannotBeWrapped; |
45
|
|
|
} |
46
|
|
|
|
47
|
64 |
|
$this->original = $map; |
48
|
64 |
|
} |
49
|
|
|
|
50
|
52 |
|
public static function fromStream(Readable $stream): Value |
51
|
|
|
{ |
52
|
52 |
|
$length = UnsignedLongInteger::fromStream($stream)->original(); |
53
|
52 |
|
$position = $stream->position()->toInt(); |
54
|
52 |
|
$boundary = $position + $length->value(); |
55
|
|
|
|
56
|
52 |
|
$map = new Map('string', Value::class); |
57
|
|
|
|
58
|
52 |
|
while ($position < $boundary) { |
59
|
49 |
|
$key = ShortString::fromStream($stream)->original(); |
60
|
49 |
|
$class = Symbols::class((string) $stream->read(1)); |
61
|
|
|
|
62
|
49 |
|
$map = $map->put((string) $key, [$class, 'fromStream']($stream)); |
63
|
|
|
|
64
|
49 |
|
$position = $stream->position()->toInt(); |
65
|
|
|
} |
66
|
|
|
|
67
|
52 |
|
return new self($map); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return MapInterface<string, Value> |
72
|
|
|
*/ |
73
|
13 |
|
public function original(): MapInterface |
74
|
|
|
{ |
75
|
13 |
|
return $this->original; |
76
|
|
|
} |
77
|
|
|
|
78
|
63 |
|
public function __toString(): string |
79
|
|
|
{ |
80
|
63 |
|
if (\is_null($this->value)) { |
81
|
|
|
$data = $this |
82
|
63 |
|
->original |
83
|
63 |
|
->reduce( |
84
|
63 |
|
new Seq, |
85
|
63 |
|
static function(Seq $sequence, string $key, Value $value): Seq { |
86
|
|
|
return $sequence |
87
|
59 |
|
->add(new ShortString(new Str($key))) |
88
|
59 |
|
->add(Symbols::symbol(\get_class($value))) |
89
|
59 |
|
->add($value); |
90
|
63 |
|
} |
91
|
|
|
) |
92
|
63 |
|
->join('') |
93
|
63 |
|
->toEncoding('ASCII'); |
94
|
|
|
|
95
|
63 |
|
$this->value = (string) new UnsignedLongInteger( |
96
|
63 |
|
new Integer($data->length()) |
97
|
|
|
); |
98
|
63 |
|
$this->value .= $data; |
99
|
|
|
} |
100
|
|
|
|
101
|
63 |
|
return $this->value; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|