1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Frame\Value; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\Transport\Frame\Value; |
7
|
|
|
use Innmind\Math\Algebra\Integer; |
8
|
|
|
use Innmind\Immutable\{ |
9
|
|
|
Str, |
10
|
|
|
Sequence as Seq, |
11
|
|
|
MapInterface, |
12
|
|
|
Map |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
final class Table implements Value |
16
|
|
|
{ |
17
|
|
|
private $value; |
18
|
|
|
private $original; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param MapInterface<string, Value> $map |
22
|
|
|
*/ |
23
|
6 |
|
public function __construct(MapInterface $map) |
24
|
|
|
{ |
25
|
|
|
if ( |
26
|
6 |
|
(string) $map->keyType() !== 'string' || |
27
|
6 |
|
(string) $map->valueType() !== Value::class |
28
|
|
|
) { |
29
|
1 |
|
throw new \TypeError(sprintf( |
30
|
1 |
|
'Argument 1 must be of type MapInterface<string, %s>', |
31
|
1 |
|
Value::class |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$data = $map |
36
|
5 |
|
->reduce( |
37
|
5 |
|
new Seq, |
38
|
5 |
|
static function(Seq $sequence, string $key, Value $value): Seq { |
39
|
|
|
return $sequence |
40
|
4 |
|
->add(new ShortString(new Str($key))) |
41
|
4 |
|
->add(Symbols::symbol(get_class($value))) |
42
|
4 |
|
->add($value); |
43
|
5 |
|
} |
44
|
|
|
) |
45
|
5 |
|
->join('') |
46
|
5 |
|
->toEncoding('ASCII'); |
47
|
|
|
|
48
|
5 |
|
$this->value = (string) new UnsignedLongInteger( |
49
|
5 |
|
new Integer($data->length()) |
50
|
|
|
); |
51
|
5 |
|
$this->value .= $data; |
52
|
5 |
|
$this->original = $map; |
53
|
5 |
|
} |
54
|
|
|
|
55
|
2 |
|
public static function fromString(Str $string): Value |
56
|
|
|
{ |
57
|
2 |
|
$string = $string->toEncoding('ASCII'); |
58
|
2 |
|
$length = UnsignedLongInteger::fromString($string->substring(0, 4))->original(); |
59
|
2 |
|
$string = $string->substring(4); |
60
|
|
|
|
61
|
2 |
|
if ($string->length() !== $length->value()) { |
62
|
|
|
throw new StringNotOfExpectedLength($string, $length->value()); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$map = new Map('string', Value::class); |
66
|
|
|
|
67
|
2 |
|
while ($string->length() !== 0) { |
68
|
2 |
|
$key = ShortString::cut($string); |
69
|
2 |
|
$string = $string->toEncoding('ASCII')->substring($key->length()); |
70
|
2 |
|
$key = ShortString::fromString($key)->original(); |
71
|
|
|
|
72
|
2 |
|
$class = Symbols::class((string) $string->substring(0, 1)); |
73
|
2 |
|
$element = [$class, 'cut']($string->substring(1))->toEncoding('ASCII'); |
74
|
|
|
|
75
|
2 |
|
$map = $map->put((string) $key, [$class, 'fromString']($element)); |
|
|
|
|
76
|
|
|
|
77
|
2 |
|
$string = $string->substring($element->length() + 1); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
return new self($map); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public static function cut(Str $string): Str |
84
|
|
|
{ |
85
|
2 |
|
$string = $string->toEncoding('ASCII'); |
86
|
2 |
|
$length = UnsignedLongInteger::fromString( |
87
|
2 |
|
UnsignedLongInteger::cut($string) |
88
|
2 |
|
)->original(); |
89
|
|
|
|
90
|
2 |
|
return $string->substring(0, $length->value() + 4); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return MapInterface<string, Value> |
|
|
|
|
95
|
|
|
*/ |
96
|
4 |
|
public function original(): MapInterface |
97
|
|
|
{ |
98
|
4 |
|
return $this->original; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
public function __toString(): string |
102
|
|
|
{ |
103
|
4 |
|
return $this->value; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: