|
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
|
|
|
Sequence as Seq, |
|
14
|
|
|
StreamInterface, |
|
15
|
|
|
Stream, |
|
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
|
7 |
|
public function __construct(Value ...$values) |
|
27
|
|
|
{ |
|
28
|
7 |
|
$values = Stream::of(Value::class, ...$values); |
|
29
|
|
|
|
|
30
|
7 |
|
$texts = $values->filter(static function(Value $value): bool { |
|
31
|
6 |
|
return $value instanceof Text; |
|
32
|
7 |
|
}); |
|
33
|
|
|
|
|
34
|
7 |
|
if ($texts->size() > 0) { |
|
35
|
1 |
|
throw new UnboundedTextCannotBeWrapped; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
$this->original = $values; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
public static function fromStream(Readable $stream): Value |
|
42
|
|
|
{ |
|
43
|
3 |
|
$length = UnsignedLongInteger::fromStream($stream)->original(); |
|
44
|
3 |
|
$position = $stream->position()->toInt(); |
|
45
|
3 |
|
$boundary = $position + $length->value(); |
|
46
|
|
|
|
|
47
|
3 |
|
$values = []; |
|
48
|
|
|
|
|
49
|
3 |
|
while ($position < $boundary) { |
|
50
|
3 |
|
$class = Symbols::class((string) $stream->read(1)); |
|
51
|
3 |
|
$values[] = [$class, 'fromStream']($stream); |
|
52
|
3 |
|
$position = $stream->position()->toInt(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return new self(...$values); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return StreamInterface<Value> |
|
60
|
|
|
*/ |
|
61
|
5 |
|
public function original(): StreamInterface |
|
62
|
|
|
{ |
|
63
|
5 |
|
return $this->original; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
public function __toString(): string |
|
67
|
|
|
{ |
|
68
|
5 |
|
if (\is_null($this->value)) { |
|
69
|
|
|
$data = $this |
|
70
|
5 |
|
->original |
|
71
|
5 |
|
->reduce( |
|
72
|
5 |
|
new Seq, |
|
73
|
5 |
|
static function(Seq $carry, Value $value): Seq { |
|
74
|
|
|
return $carry |
|
75
|
5 |
|
->add(Symbols::symbol(\get_class($value))) |
|
76
|
5 |
|
->add($value); |
|
77
|
5 |
|
} |
|
78
|
|
|
) |
|
79
|
5 |
|
->join('') |
|
80
|
5 |
|
->toEncoding('ASCII'); |
|
81
|
5 |
|
$this->value = (string) new UnsignedLongInteger( |
|
82
|
5 |
|
new Integer($data->length()) |
|
83
|
|
|
); |
|
84
|
5 |
|
$this->value .= $data; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
return $this->value; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|