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
|
|
|
}; |
10
|
|
|
use Innmind\Math\Algebra\Integer; |
11
|
|
|
use Innmind\Immutable\{ |
12
|
|
|
Sequence as Seq, |
13
|
|
|
StreamInterface, |
14
|
|
|
Stream, |
15
|
|
|
Str |
16
|
|
|
}; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* It's an array, but "array" is a reserved keyword in PHP |
20
|
|
|
*/ |
21
|
|
|
final class Sequence implements Value |
22
|
|
|
{ |
23
|
|
|
private $value; |
24
|
|
|
private $original; |
25
|
|
|
|
26
|
5 |
|
public function __construct(Value ...$values) |
27
|
|
|
{ |
28
|
5 |
|
$sequence = new Seq(...$values); |
29
|
|
|
$data = $sequence |
30
|
5 |
|
->reduce( |
31
|
5 |
|
new Seq, |
32
|
5 |
|
static function(Seq $carry, Value $value): Seq { |
33
|
|
|
return $carry |
34
|
4 |
|
->add(Symbols::symbol(get_class($value))) |
35
|
4 |
|
->add($value); |
36
|
5 |
|
} |
37
|
|
|
) |
38
|
5 |
|
->join('') |
39
|
5 |
|
->toEncoding('ASCII'); |
40
|
5 |
|
$this->value = (string) new UnsignedLongInteger( |
41
|
5 |
|
new Integer($data->length()) |
42
|
|
|
); |
43
|
5 |
|
$this->value .= $data; |
44
|
5 |
|
$this->original = $sequence->reduce( |
45
|
5 |
|
new Stream(Value::class), |
46
|
5 |
|
static function(Stream $stream, Value $value): Stream { |
47
|
4 |
|
return $stream->add($value); |
|
|
|
|
48
|
5 |
|
} |
49
|
|
|
); |
50
|
5 |
|
} |
51
|
|
|
|
52
|
2 |
|
public static function fromString(Str $string): Value |
53
|
|
|
{ |
54
|
2 |
|
$string = $string->toEncoding('ASCII'); |
55
|
2 |
|
$length = UnsignedLongInteger::fromString($string->substring(0, 4))->original(); |
56
|
2 |
|
$string = $string->substring(4); |
57
|
|
|
|
58
|
2 |
|
if ($string->length() !== $length->value()) { |
59
|
|
|
throw new StringNotOfExpectedLength($string, $length->value()); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$values = []; |
63
|
|
|
|
64
|
2 |
|
while ($string->length() !== 0) { |
65
|
2 |
|
$class = Symbols::class((string) $string->substring(0, 1)); |
66
|
2 |
|
$element = [$class, 'cut']($string->substring(1))->toEncoding('ASCII'); |
67
|
2 |
|
$values[] = [$class, 'fromString']($element); |
68
|
2 |
|
$string = $string->substring($element->length() + 1); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return new self(...$values); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public static function cut(Str $string): Str |
75
|
|
|
{ |
76
|
2 |
|
$string = $string->toEncoding('ASCII'); |
77
|
2 |
|
$length = UnsignedLongInteger::fromString( |
78
|
2 |
|
UnsignedLongInteger::cut($string) |
79
|
2 |
|
)->original(); |
80
|
|
|
|
81
|
2 |
|
return $string->substring(0, $length->value() + 4); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return StreamInterface<Value> |
|
|
|
|
86
|
|
|
*/ |
87
|
4 |
|
public function original(): StreamInterface |
88
|
|
|
{ |
89
|
4 |
|
return $this->original; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
public function __toString(): string |
93
|
|
|
{ |
94
|
4 |
|
return $this->value; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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: