|
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
|
93 |
|
private function __construct( |
|
33
|
|
|
Type $type, |
|
34
|
|
|
Channel $channel, |
|
35
|
|
|
Value ...$values |
|
36
|
|
|
) { |
|
37
|
93 |
|
$this->type = $type; |
|
38
|
93 |
|
$this->channel = $channel; |
|
39
|
93 |
|
$this->values = new Stream(Value::class); |
|
40
|
|
|
|
|
41
|
93 |
|
$values = new Sequence(...$values); |
|
42
|
93 |
|
$payload = $values->join('')->toEncoding('ASCII'); |
|
43
|
|
|
|
|
44
|
93 |
|
$frame = new Sequence( |
|
45
|
93 |
|
new UnsignedOctet(new Integer($type->toInt())), |
|
46
|
93 |
|
new UnsignedShortInteger(new Integer($channel->toInt())), |
|
47
|
93 |
|
new UnsignedLongInteger(new Integer($payload->length())), |
|
48
|
93 |
|
...$values |
|
49
|
|
|
); |
|
50
|
93 |
|
$this->string = (string) $frame |
|
51
|
93 |
|
->add(new UnsignedOctet(new Integer(self::end()))) |
|
52
|
93 |
|
->join(''); |
|
53
|
93 |
|
} |
|
54
|
|
|
|
|
55
|
86 |
|
public static function method( |
|
56
|
|
|
Channel $channel, |
|
57
|
|
|
Method $method, |
|
58
|
|
|
Value ...$values |
|
59
|
|
|
): self { |
|
60
|
86 |
|
$self = new self( |
|
61
|
86 |
|
Type::method(), |
|
62
|
86 |
|
$channel, |
|
63
|
86 |
|
new UnsignedShortInteger(new Integer($method->class())), |
|
64
|
86 |
|
new UnsignedShortInteger(new Integer($method->method())), |
|
65
|
86 |
|
...$values |
|
66
|
|
|
); |
|
67
|
86 |
|
$self->method = $method; |
|
68
|
86 |
|
$self->values = (new Sequence(...$values))->reduce( |
|
69
|
86 |
|
$self->values, |
|
70
|
86 |
|
static function(Stream $stream, Value $value): Stream { |
|
71
|
80 |
|
return $stream->add($value); |
|
72
|
86 |
|
} |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
86 |
|
return $self; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
27 |
|
public static function header( |
|
79
|
|
|
Channel $channel, |
|
80
|
|
|
int $class, |
|
81
|
|
|
Value ...$values |
|
82
|
|
|
): self { |
|
83
|
27 |
|
$self = new self( |
|
84
|
27 |
|
Type::header(), |
|
85
|
27 |
|
$channel, |
|
86
|
27 |
|
new UnsignedShortInteger(new Integer($class)), |
|
87
|
27 |
|
new UnsignedShortInteger(new Integer(0)), //weight |
|
88
|
27 |
|
...$values |
|
89
|
|
|
); |
|
90
|
27 |
|
$self->values = (new Sequence(...$values))->reduce( |
|
91
|
27 |
|
$self->values, |
|
92
|
27 |
|
static function(Stream $stream, Value $value): Stream { |
|
93
|
27 |
|
return $stream->add($value); |
|
94
|
27 |
|
} |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
27 |
|
return $self; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
24 |
|
public static function body(Channel $channel, Str $payload): self |
|
101
|
|
|
{ |
|
102
|
24 |
|
$self = new self( |
|
103
|
24 |
|
Type::body(), |
|
104
|
24 |
|
$channel, |
|
105
|
24 |
|
$value = new Text($payload) |
|
106
|
|
|
); |
|
107
|
24 |
|
$self->values = $self->values->add($value); |
|
108
|
|
|
|
|
109
|
24 |
|
return $self; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
12 |
|
public static function heartbeat(): self |
|
113
|
|
|
{ |
|
114
|
12 |
|
return new self( |
|
115
|
12 |
|
Type::heartbeat(), |
|
116
|
12 |
|
new Channel(0) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
84 |
|
public function type(): Type |
|
121
|
|
|
{ |
|
122
|
84 |
|
return $this->type; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
83 |
|
public function channel(): Channel |
|
126
|
|
|
{ |
|
127
|
83 |
|
return $this->channel; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
77 |
|
public function is(Method $method): bool |
|
131
|
|
|
{ |
|
132
|
77 |
|
if ($this->type() !== Type::method()) { |
|
133
|
3 |
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
74 |
|
return $this->method->equals($method); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return StreamInterface<Value> |
|
|
|
|
|
|
141
|
|
|
*/ |
|
142
|
86 |
|
public function values(): StreamInterface |
|
143
|
|
|
{ |
|
144
|
86 |
|
return $this->values; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
57 |
|
public function __toString(): string |
|
148
|
|
|
{ |
|
149
|
57 |
|
return $this->string; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
93 |
|
public static function end(): int |
|
153
|
|
|
{ |
|
154
|
93 |
|
return 0xCE; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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.