1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ~~summary~~ |
5
|
|
|
* |
6
|
|
|
* ~~description~~ |
7
|
|
|
* |
8
|
|
|
* PHP version 5 |
9
|
|
|
* |
10
|
|
|
* @category Net |
11
|
|
|
* @package PEAR2_Net_RouterOS |
12
|
|
|
* @author Vasil Rangelov <[email protected]> |
13
|
|
|
* @copyright 2011 Vasil Rangelov |
14
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
15
|
|
|
* @version GIT: $Id$ |
16
|
|
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The namespace declaration. |
20
|
|
|
*/ |
21
|
|
|
namespace PEAR2\Net\RouterOS; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implements this interface. |
25
|
|
|
*/ |
26
|
|
|
use Countable; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Implements this interface. |
30
|
|
|
*/ |
31
|
|
|
use IteratorAggregate; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Required for IteratorAggregate::getIterator() to work properly with foreach. |
35
|
|
|
*/ |
36
|
|
|
use ArrayObject; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Represents a RouterOS message. |
40
|
|
|
* |
41
|
|
|
* @category Net |
42
|
|
|
* @package PEAR2_Net_RouterOS |
43
|
|
|
* @author Vasil Rangelov <[email protected]> |
44
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
45
|
|
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS |
46
|
|
|
*/ |
47
|
|
|
abstract class Message implements IteratorAggregate, Countable |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* An array with message attributes. |
52
|
|
|
* |
53
|
|
|
* Each array key is the the name of an attribute, |
54
|
|
|
* and the corresponding array value is the value for that attribute. |
55
|
|
|
* |
56
|
|
|
* @var array<string,string|resource> |
57
|
|
|
*/ |
58
|
|
|
protected $attributes = array(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* An optional tag to associate the message with. |
62
|
|
|
* |
63
|
|
|
* @var string|null |
64
|
|
|
*/ |
65
|
|
|
private $_tag = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* A shorthand gateway. |
69
|
|
|
* |
70
|
|
|
* This is a magic PHP method that allows you to call the object as a |
71
|
|
|
* function. Depending on the argument given, one of the other functions in |
72
|
|
|
* the class is invoked and its returned value is returned by this function. |
73
|
|
|
* |
74
|
|
|
* @param string|null $name The name of an attribute to get the value of, |
75
|
|
|
* or NULL to get the tag. |
76
|
|
|
* |
77
|
|
|
* @return string|resource The value of the specified attribute, |
78
|
|
|
* or the tag if NULL is provided. |
79
|
|
|
*/ |
80
|
|
|
public function __invoke($name = null) |
81
|
|
|
{ |
82
|
|
|
if (null === $name) { |
83
|
|
|
return $this->getTag(); |
84
|
|
|
} |
85
|
|
|
return $this->getAttribute($name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sanitizes a name of an attribute (message or query one). |
90
|
|
|
* |
91
|
|
|
* @param mixed $name The name to sanitize. |
92
|
|
|
* |
93
|
|
|
* @return string The sanitized name. |
94
|
|
|
*/ |
95
|
|
|
public static function sanitizeAttributeName($name) |
96
|
|
|
{ |
97
|
|
|
$name = (string) $name; |
98
|
|
|
if ((empty($name) && $name !== '0') |
99
|
|
|
|| preg_match('/[=\s]/s', $name) |
100
|
|
|
) { |
101
|
|
|
throw new InvalidArgumentException( |
102
|
|
|
'Invalid name of argument supplied.', |
103
|
|
|
InvalidArgumentException::CODE_NAME_INVALID |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
return $name; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sanitizes a value of an attribute (message or query one). |
111
|
|
|
* |
112
|
|
|
* @param mixed $value The value to sanitize. |
113
|
|
|
* |
114
|
|
|
* @return string|resource The sanitized value. |
115
|
|
|
*/ |
116
|
|
|
public static function sanitizeAttributeValue($value) |
117
|
|
|
{ |
118
|
|
|
if (Communicator::isSeekableStream($value)) { |
119
|
|
|
return $value; |
120
|
|
|
} else { |
121
|
|
|
return (string) $value; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the tag that the message is associated with. |
127
|
|
|
* |
128
|
|
|
* @return string|null The current tag or NULL if there isn't a tag. |
129
|
|
|
* |
130
|
|
|
* @see setTag() |
131
|
|
|
*/ |
132
|
|
|
public function getTag() |
133
|
|
|
{ |
134
|
|
|
return $this->_tag; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the tag to associate the request with. |
139
|
|
|
* |
140
|
|
|
* Sets the tag to associate the message with. Setting NULL erases the |
141
|
|
|
* currently set tag. |
142
|
|
|
* |
143
|
|
|
* @param string|null $tag The tag to set. |
144
|
|
|
* |
145
|
|
|
* @return $this The message object. |
146
|
|
|
* |
147
|
|
|
* @see getTag() |
148
|
|
|
*/ |
149
|
|
|
protected function setTag($tag) |
150
|
|
|
{ |
151
|
|
|
$this->_tag = (null === $tag) ? null : (string) $tag; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Gets the value of an attribute. |
157
|
|
|
* |
158
|
|
|
* @param string $name The name of the attribute. |
159
|
|
|
* |
160
|
|
|
* @return string|resource|null The value of the specified attribute. |
161
|
|
|
* Returns NULL if such an attribute is not set. |
162
|
|
|
* |
163
|
|
|
* @see setAttribute() |
164
|
|
|
*/ |
165
|
|
|
protected function getAttribute($name) |
166
|
|
|
{ |
167
|
|
|
$name = self::sanitizeAttributeName($name); |
168
|
|
|
if (array_key_exists($name, $this->attributes)) { |
169
|
|
|
return $this->attributes[$name]; |
170
|
|
|
} |
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Gets all arguments in an array. |
176
|
|
|
* |
177
|
|
|
* @return ArrayObject An ArrayObject with the keys being argument names, |
178
|
|
|
* and the array values being argument values. |
179
|
|
|
* |
180
|
|
|
* @see getArgument() |
181
|
|
|
* @see setArgument() |
182
|
|
|
*/ |
183
|
|
|
public function getIterator() |
184
|
|
|
{ |
185
|
|
|
return new ArrayObject($this->attributes); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Counts the number of attributes. |
190
|
|
|
* |
191
|
|
|
* @return int The number of attributes. |
192
|
|
|
*/ |
193
|
|
|
public function count() |
194
|
|
|
{ |
195
|
|
|
return count($this->attributes); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Sets an attribute for the message. |
200
|
|
|
* |
201
|
|
|
* @param string $name Name of the attribute. |
202
|
|
|
* @param string|resource|null $value Value of the attribute as a string or |
203
|
|
|
* seekable stream. |
204
|
|
|
* Setting the value to NULL removes an argument of this name. |
205
|
|
|
* If a seekable stream is provided, it is sent from its current |
206
|
|
|
* position to its end, and the pointer is seeked back to its current |
207
|
|
|
* position after sending. |
208
|
|
|
* Non seekable streams, as well as all other types, are casted to a |
209
|
|
|
* string. |
210
|
|
|
* |
211
|
|
|
* @return $this The message object. |
212
|
|
|
* |
213
|
|
|
* @see getArgument() |
214
|
|
|
*/ |
215
|
|
|
protected function setAttribute($name, $value = '') |
216
|
|
|
{ |
217
|
|
|
if (null === $value) { |
218
|
|
|
unset($this->attributes[self::sanitizeAttributeName($name)]); |
219
|
|
|
} else { |
220
|
|
|
$this->attributes[self::sanitizeAttributeName($name)] |
221
|
|
|
= self::sanitizeAttributeValue($value); |
222
|
|
|
} |
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Removes all attributes from the message. |
228
|
|
|
* |
229
|
|
|
* @return $this The message object. |
230
|
|
|
*/ |
231
|
|
|
protected function removeAllAttributes() |
232
|
|
|
{ |
233
|
|
|
$this->attributes = array(); |
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get actionable debug info. |
239
|
|
|
* |
240
|
|
|
* This is a magic method available to PHP 5.6 and above, due to which |
241
|
|
|
* output of var_dump() will be more actionable. |
242
|
|
|
* |
243
|
|
|
* You can still call it in earlier versions to get the object as a plain array. |
244
|
|
|
* |
245
|
|
|
* @return array The info, as an associative array. |
246
|
|
|
*/ |
247
|
|
|
public function __debugInfo() |
248
|
|
|
{ |
249
|
|
|
return array( |
250
|
|
|
'attributes' => $this->attributes, |
251
|
|
|
'tag' => $this->_tag |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|