1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ButterAMQP; |
4
|
|
|
|
5
|
|
|
class Message |
6
|
|
|
{ |
7
|
|
|
const FLAG_MANDATORY = 0b01; |
8
|
|
|
const FLAG_IMMEDIATE = 0b10; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $body; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $properties = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $body |
22
|
|
|
* @param array $properties |
23
|
|
|
*/ |
24
|
34 |
|
public function __construct($body, array $properties = []) |
25
|
|
|
{ |
26
|
34 |
|
$this->body = $body; |
27
|
34 |
|
$this->properties = $properties; |
28
|
34 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
19 |
|
public function getBody() |
34
|
|
|
{ |
35
|
19 |
|
return $this->body; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
21 |
|
public function getProperties() |
42
|
|
|
{ |
43
|
21 |
|
return $this->properties; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
5 |
|
public function hasProperty($name) |
52
|
|
|
{ |
53
|
5 |
|
return isset($this->properties[$name]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name |
58
|
|
|
* @param mixed $default |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
5 |
|
public function getProperty($name, $default = null) |
63
|
|
|
{ |
64
|
5 |
|
return $this->hasProperty($name) ? $this->properties[$name] : $default; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @param mixed $value |
70
|
|
|
* |
71
|
|
|
* @return Message |
72
|
|
|
*/ |
73
|
3 |
|
public function withProperty($name, $value) |
74
|
|
|
{ |
75
|
3 |
|
return $this->withProperties([$name => $value]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* |
81
|
|
|
* @return Message |
82
|
|
|
*/ |
83
|
1 |
|
public function withoutProperty($name) |
84
|
|
|
{ |
85
|
1 |
|
$properties = $this->properties; |
86
|
|
|
|
87
|
1 |
|
unset($properties[$name]); |
88
|
|
|
|
89
|
1 |
|
return new self($this->body, $properties); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $properties |
94
|
|
|
* |
95
|
|
|
* @return Message |
96
|
|
|
*/ |
97
|
4 |
|
public function withProperties(array $properties) |
98
|
|
|
{ |
99
|
4 |
|
return new self($this->body, array_merge($this->properties, $properties)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
4 |
|
public function getHeaders() |
106
|
|
|
{ |
107
|
4 |
|
return $this->getProperty('headers', []); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $name |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
1 |
|
public function hasHeader($name) |
116
|
|
|
{ |
117
|
1 |
|
$headers = $this->getHeaders(); |
118
|
|
|
|
119
|
1 |
|
return isset($headers[$name]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $name |
124
|
|
|
* @param mixed $default |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
1 |
|
public function getHeader($name, $default = null) |
129
|
|
|
{ |
130
|
1 |
|
$headers = $this->getHeaders(); |
131
|
|
|
|
132
|
1 |
|
return isset($headers[$name]) ? $headers[$name] : $default; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $name |
137
|
|
|
* @param mixed $value |
138
|
|
|
* |
139
|
|
|
* @return Message |
140
|
|
|
*/ |
141
|
1 |
|
public function withHeader($name, $value) |
142
|
|
|
{ |
143
|
1 |
|
return $this->withHeaders([$name => $value]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $name |
148
|
|
|
* |
149
|
|
|
* @return Message |
150
|
|
|
*/ |
151
|
1 |
|
public function withoutHeader($name) |
152
|
|
|
{ |
153
|
1 |
|
$headers = $this->getHeaders(); |
154
|
|
|
|
155
|
1 |
|
unset($headers[$name]); |
156
|
|
|
|
157
|
1 |
|
$properties = $this->properties; |
158
|
1 |
|
$properties['headers'] = $headers; |
159
|
|
|
|
160
|
1 |
|
return new self($this->body, $properties); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $headers |
165
|
|
|
* |
166
|
|
|
* @return Message |
167
|
|
|
*/ |
168
|
2 |
|
public function withHeaders(array $headers) |
169
|
|
|
{ |
170
|
2 |
|
$headers = array_merge($this->getHeaders(), $headers); |
171
|
|
|
|
172
|
2 |
|
return $this->withProperty('headers', $headers); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Define how to print object when dumping. |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
2 |
|
public function __debugInfo() |
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
2 |
|
'properties' => $this->getProperties(), |
184
|
2 |
|
'body' => $this->getBody(), |
185
|
2 |
|
]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|