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
|
|
|
}; |
15
|
|
|
use Innmind\Immutable\{ |
16
|
|
|
Sequence, |
17
|
|
|
Stream, |
18
|
|
|
StreamInterface |
19
|
|
|
}; |
20
|
|
|
|
21
|
|
|
final class Frame |
22
|
|
|
{ |
23
|
|
|
private $type; |
24
|
|
|
private $values; |
25
|
|
|
private $string; |
26
|
|
|
|
27
|
1 |
|
public function __construct( |
28
|
|
|
Type $type, |
29
|
|
|
Channel $channel, |
30
|
|
|
Method $method, |
31
|
|
|
Value ...$values |
32
|
|
|
) { |
33
|
1 |
|
$this->type = $type; |
34
|
1 |
|
$this->channel = $channel; |
|
|
|
|
35
|
1 |
|
$this->method = $method; |
|
|
|
|
36
|
1 |
|
$values = new Sequence(...$values); |
37
|
1 |
|
$payload = $values->join('')->toEncoding('ASCII'); |
38
|
1 |
|
$frame = new Sequence( |
39
|
1 |
|
new UnsignedOctet($type->toInt()), |
40
|
1 |
|
new UnsignedShortInteger($channel->toInt()), |
41
|
1 |
|
new UnsignedLongInteger( |
42
|
1 |
|
$payload->length() + 4 // 4 is the size for the method |
43
|
|
|
), |
44
|
1 |
|
new UnsignedShortInteger($method->class()), |
45
|
1 |
|
new UnsignedShortInteger($method->method()), |
46
|
1 |
|
$payload, |
47
|
1 |
|
new UnsignedOctet(0xCE) |
48
|
|
|
); |
49
|
1 |
|
$this->values = $values->reduce( |
50
|
1 |
|
new Stream(Value::class), |
51
|
1 |
|
static function(Stream $stream, Value $value): Stream { |
52
|
1 |
|
return $stream->add($value); |
|
|
|
|
53
|
1 |
|
} |
54
|
|
|
); |
55
|
1 |
|
$this->string = (string) $frame->join(''); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
public function type(): Type |
59
|
|
|
{ |
60
|
1 |
|
return $this->type; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function channel(): Channel |
64
|
|
|
{ |
65
|
1 |
|
return $this->channel; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function method(): Method |
69
|
|
|
{ |
70
|
1 |
|
return $this->method; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return StreamInterface<Value> |
|
|
|
|
75
|
|
|
*/ |
76
|
1 |
|
public function values(): StreamInterface |
77
|
|
|
{ |
78
|
1 |
|
return $this->values; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function __toString(): string |
82
|
|
|
{ |
83
|
1 |
|
return $this->string; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: