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