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\StringNotOfExpectedLength, |
9
|
|
|
Exception\UnboundedTextCannotBeWrapped |
10
|
|
|
}; |
11
|
|
|
use Innmind\Math\Algebra\Integer; |
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
|
59 |
|
return $value instanceof Text; |
41
|
65 |
|
}); |
42
|
|
|
|
43
|
65 |
|
if ($texts->size() > 0) { |
44
|
1 |
|
throw new UnboundedTextCannotBeWrapped; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$data = $map |
48
|
64 |
|
->reduce( |
49
|
64 |
|
new Seq, |
50
|
64 |
|
static function(Seq $sequence, string $key, Value $value): Seq { |
51
|
|
|
return $sequence |
52
|
58 |
|
->add(new ShortString(new Str($key))) |
53
|
58 |
|
->add(Symbols::symbol(get_class($value))) |
54
|
58 |
|
->add($value); |
55
|
64 |
|
} |
56
|
|
|
) |
57
|
64 |
|
->join('') |
58
|
64 |
|
->toEncoding('ASCII'); |
59
|
|
|
|
60
|
64 |
|
$this->value = (string) new UnsignedLongInteger( |
61
|
64 |
|
new Integer($data->length()) |
62
|
|
|
); |
63
|
64 |
|
$this->value .= $data; |
64
|
64 |
|
$this->original = $map; |
65
|
64 |
|
} |
66
|
|
|
|
67
|
52 |
|
public static function fromString(Str $string): Value |
68
|
|
|
{ |
69
|
52 |
|
$string = $string->toEncoding('ASCII'); |
70
|
52 |
|
$length = UnsignedLongInteger::fromString($string->substring(0, 4))->original(); |
71
|
52 |
|
$string = $string->substring(4); |
72
|
|
|
|
73
|
52 |
|
if ($string->length() !== $length->value()) { |
74
|
1 |
|
throw new StringNotOfExpectedLength($string, $length->value()); |
75
|
|
|
} |
76
|
|
|
|
77
|
51 |
|
$map = new Map('string', Value::class); |
78
|
|
|
|
79
|
51 |
|
while ($string->length() !== 0) { |
80
|
48 |
|
$key = ShortString::cut($string); |
81
|
48 |
|
$string = $string->toEncoding('ASCII')->substring($key->length()); |
82
|
48 |
|
$key = ShortString::fromString($key)->original(); |
83
|
|
|
|
84
|
48 |
|
$class = Symbols::class((string) $string->substring(0, 1)); |
85
|
48 |
|
$element = [$class, 'cut']($string->substring(1))->toEncoding('ASCII'); |
86
|
|
|
|
87
|
48 |
|
$map = $map->put((string) $key, [$class, 'fromString']($element)); |
88
|
|
|
|
89
|
48 |
|
$string = $string->substring($element->length() + 1); |
90
|
|
|
} |
91
|
|
|
|
92
|
51 |
|
return new self($map); |
93
|
|
|
} |
94
|
|
|
|
95
|
51 |
|
public static function cut(Str $string): Str |
96
|
|
|
{ |
97
|
51 |
|
$string = $string->toEncoding('ASCII'); |
98
|
51 |
|
$length = UnsignedLongInteger::fromString( |
99
|
51 |
|
UnsignedLongInteger::cut($string) |
100
|
51 |
|
)->original(); |
101
|
|
|
|
102
|
51 |
|
return $string->substring(0, $length->value() + 4); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return MapInterface<string, Value> |
|
|
|
|
107
|
|
|
*/ |
108
|
13 |
|
public function original(): MapInterface |
109
|
|
|
{ |
110
|
13 |
|
return $this->original; |
111
|
|
|
} |
112
|
|
|
|
113
|
63 |
|
public function __toString(): string |
114
|
|
|
{ |
115
|
63 |
|
return $this->value; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.