1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\Transport\Frame\{ |
7
|
|
|
Type, |
8
|
|
|
Channel, |
9
|
|
|
Method, |
10
|
|
|
Value, |
11
|
|
|
Value\UnsignedOctet, |
12
|
|
|
Value\UnsignedShortInteger, |
13
|
|
|
Value\UnsignedLongInteger, |
14
|
|
|
Value\Text |
15
|
|
|
}; |
16
|
|
|
use Innmind\Math\Algebra\Integer; |
17
|
|
|
use Innmind\Immutable\{ |
18
|
|
|
Sequence, |
19
|
|
|
Stream, |
20
|
|
|
StreamInterface, |
21
|
|
|
Str |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
final class Frame |
25
|
|
|
{ |
26
|
|
|
private $type; |
27
|
|
|
private $channel; |
28
|
|
|
private $method; |
29
|
|
|
private $values; |
30
|
|
|
private $string; |
31
|
|
|
|
32
|
60 |
|
private function __construct( |
33
|
|
|
Type $type, |
34
|
|
|
Channel $channel, |
35
|
|
|
Value ...$values |
36
|
|
|
) { |
37
|
60 |
|
$this->type = $type; |
38
|
60 |
|
$this->channel = $channel; |
39
|
60 |
|
$this->values = new Stream(Value::class); |
40
|
|
|
|
41
|
60 |
|
$values = new Sequence(...$values); |
42
|
60 |
|
$payload = $values->join('')->toEncoding('ASCII'); |
43
|
|
|
|
44
|
60 |
|
$frame = new Sequence( |
45
|
60 |
|
new UnsignedOctet(new Integer($type->toInt())), |
46
|
60 |
|
new UnsignedShortInteger(new Integer($channel->toInt())), |
47
|
60 |
|
new UnsignedLongInteger(new Integer($payload->length())), |
48
|
60 |
|
...$values |
49
|
|
|
); |
50
|
60 |
|
$this->string = (string) $frame |
51
|
60 |
|
->add(new UnsignedOctet(new Integer(0xCE))) |
52
|
60 |
|
->join(''); |
53
|
60 |
|
} |
54
|
|
|
|
55
|
57 |
|
public static function command( |
56
|
|
|
Channel $channel, |
57
|
|
|
Method $method, |
58
|
|
|
Value ...$values |
59
|
|
|
): self { |
60
|
57 |
|
$self = new self( |
61
|
57 |
|
Type::method(), |
62
|
57 |
|
$channel, |
63
|
57 |
|
new UnsignedShortInteger(new Integer($method->class())), |
64
|
57 |
|
new UnsignedShortInteger(new Integer($method->method())), |
65
|
57 |
|
...$values |
66
|
|
|
); |
67
|
57 |
|
$self->method = $method; |
68
|
57 |
|
$self->values = (new Sequence(...$values))->reduce( |
69
|
57 |
|
$self->values, |
70
|
57 |
|
static function(Stream $stream, Value $value): Stream { |
71
|
51 |
|
return $stream->add($value); |
72
|
57 |
|
} |
73
|
|
|
); |
74
|
|
|
|
75
|
57 |
|
return $self; |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
public static function header( |
79
|
|
|
Channel $channel, |
80
|
|
|
int $class, |
81
|
|
|
Value ...$values |
82
|
|
|
): self { |
83
|
8 |
|
$self = new self( |
84
|
8 |
|
Type::header(), |
85
|
8 |
|
$channel, |
86
|
8 |
|
new UnsignedShortInteger(new Integer($class)), |
87
|
8 |
|
new UnsignedShortInteger(new Integer(0)), //weight |
88
|
8 |
|
...$values |
89
|
|
|
); |
90
|
8 |
|
$self->values = (new Sequence(...$values))->reduce( |
91
|
8 |
|
$self->values, |
92
|
8 |
|
static function(Stream $stream, Value $value): Stream { |
93
|
8 |
|
return $stream->add($value); |
94
|
8 |
|
} |
95
|
|
|
); |
96
|
|
|
|
97
|
8 |
|
return $self; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
public static function body(Channel $channel, Str $payload): self |
101
|
|
|
{ |
102
|
4 |
|
$self = new self( |
103
|
4 |
|
Type::body(), |
104
|
4 |
|
$channel, |
105
|
4 |
|
$value = new Text($payload) |
106
|
|
|
); |
107
|
4 |
|
$self->values = $self->values->add($value); |
108
|
|
|
|
109
|
4 |
|
return $self; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public static function heartbeat(): self |
113
|
|
|
{ |
114
|
1 |
|
return new self( |
115
|
1 |
|
Type::heartbeat(), |
116
|
1 |
|
new Channel(0) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
33 |
|
public function type(): Type |
121
|
|
|
{ |
122
|
33 |
|
return $this->type; |
123
|
|
|
} |
124
|
|
|
|
125
|
52 |
|
public function channel(): Channel |
126
|
|
|
{ |
127
|
52 |
|
return $this->channel; |
128
|
|
|
} |
129
|
|
|
|
130
|
47 |
|
public function method(): Method |
131
|
|
|
{ |
132
|
47 |
|
return $this->method; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return StreamInterface<Value> |
|
|
|
|
137
|
|
|
*/ |
138
|
56 |
|
public function values(): StreamInterface |
139
|
|
|
{ |
140
|
56 |
|
return $this->values; |
141
|
|
|
} |
142
|
|
|
|
143
|
25 |
|
public function __toString(): string |
144
|
|
|
{ |
145
|
25 |
|
return $this->string; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.