Message   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 39
c 3
b 1
f 0
dl 0
loc 208
rs 10
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeAttributeValue() 0 6 2
A sanitizeAttributeName() 0 12 4
A __invoke() 0 6 2
A getAttribute() 0 7 2
A getTag() 0 3 1
A setTag() 0 4 2
A count() 0 4 1
A setAttribute() 0 9 2
A removeAllAttributes() 0 4 1
A getIterator() 0 4 1
A __debugInfo() 0 5 1
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
    #[\ReturnTypeWillChange]
184
    public function getIterator()
185
    {
186
        return new ArrayObject($this->attributes);
187
    }
188
189
    /**
190
     * Counts the number of attributes.
191
     *
192
     * @return int The number of attributes.
193
     */
194
    #[\ReturnTypeWillChange]
195
    public function count()
196
    {
197
        return count($this->attributes);
198
    }
199
200
    /**
201
     * Sets an attribute for the message.
202
     *
203
     * @param string               $name  Name of the attribute.
204
     * @param string|resource|null $value Value of the attribute as a string or
205
     *     seekable stream.
206
     *     Setting the value to NULL removes an argument of this name.
207
     *     If a seekable stream is provided, it is sent from its current
208
     *     position to its end, and the pointer is seeked back to its current
209
     *     position after sending.
210
     *     Non seekable streams, as well as all other types, are casted to a
211
     *     string.
212
     *
213
     * @return $this The message object.
214
     *
215
     * @see getArgument()
216
     */
217
    protected function setAttribute($name, $value = '')
218
    {
219
        if (null === $value) {
220
            unset($this->attributes[self::sanitizeAttributeName($name)]);
221
        } else {
222
            $this->attributes[self::sanitizeAttributeName($name)]
223
                = self::sanitizeAttributeValue($value);
224
        }
225
        return $this;
226
    }
227
228
    /**
229
     * Removes all attributes from the message.
230
     *
231
     * @return $this The message object.
232
     */
233
    protected function removeAllAttributes()
234
    {
235
        $this->attributes = array();
236
        return $this;
237
    }
238
239
    /**
240
     * Get actionable debug info.
241
     *
242
     * This is a magic method available to PHP 5.6 and above, due to which
243
     * output of var_dump() will be more actionable.
244
     *
245
     * You can still call it in earlier versions to get the object as a
246
     * plain array.
247
     *
248
     * @return array The info, as an associative array.
249
     */
250
    public function __debugInfo()
251
    {
252
        return array(
253
            'attributes' => $this->attributes,
254
            'tag' => $this->_tag
255
        );
256
    }
257
}
258