Skip to content

Commit a562e1f

Browse files
committed
Moved most of Script::parseValue() into the new Script::parseValueSimple(), parseValueArray() and Script::parseValueObject() methods.
1 parent 49c3fad commit a562e1f

File tree

2 files changed

+139
-63
lines changed

2 files changed

+139
-63
lines changed

src/PEAR2/Net/RouterOS/Script.php

Lines changed: 138 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,104 @@ class Script
7070
* This method is intended to be the very opposite of
7171
* {@link static::escapeValue()}. That is, results from that method, if
7272
* given to this method, should produce equivalent results.
73-
*
74-
* For better usefulness, in addition to "actual" RouterOS types, a pseudo
75-
* "date" type is also recognized, whenever the string is in the form
76-
* "M/j/Y".
7773
*
7874
* @param string $value The value to be parsed. Must be a literal of a
7975
* value, e.g. what {@link static::escapeValue()} will give you.
8076
*
8177
* @return mixed Depending on RouterOS type detected:
82-
* - "nil" or "nothing" - NULL.
78+
* - "nil" (the string "[]") or "nothing" (empty string) - NULL.
8379
* - "number" - int or double for large values.
8480
* - "bool" - a boolean.
85-
* - "time" - a {@link DateInterval} object.
86-
* - "array" - an array, with the values processed recursively.
81+
* - "array" - an array, with the keys and values processed recursively.
8782
* - "str" - a string.
88-
* - "date" (pseudo type) - a DateTime object with the specified date,
89-
* at midnight UTC time.
83+
* - "time" - a {@link DateInterval} object.
84+
* - "date" (pseudo type; string in the form "M/j/Y") - a DateTime
85+
* object with the specified date, at midnight UTC time.
86+
* - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a
87+
* DateTime object with the specified date and UTC time.
9088
* - Unrecognized type - treated as an unquoted string.
9189
*/
9290
public static function parseValue($value)
91+
{
92+
$value = static::parseValueToSimple($value);
93+
if (!is_string($value)) {
94+
return $value;
95+
} elseif ('{' === $value[0] && '}' === $value[strlen($value) - 1]) {
96+
$value = static::parseValueToArray($value);
97+
if (!is_string($value)) {
98+
return $value;
99+
}
100+
} elseif ('"' === $value[0] && '"' === $value[strlen($value) - 1]) {
101+
return str_replace(
102+
array('\"', '\\\\', "\\\n", "\\\r\n", "\\\r"),
103+
array('"', '\\'),
104+
substr($value, 1, -1)
105+
);
106+
}
107+
108+
$value = static::parseValueToObject($value);
109+
if (!is_string($value)) {
110+
return $value;
111+
}
112+
113+
return $value;
114+
}
115+
116+
/**
117+
* Parses a RouterOS value into a PHP simple type.
118+
*
119+
* Parses a RouterOS value into a PHP simple type. "Simple" types being
120+
* scalar types, plus NULL.
121+
*
122+
* @param string $value The value to be parsed. Must be a literal of a
123+
* value, e.g. what {@link static::escapeValue()} will give you.
124+
*
125+
* @return string|bool|int|double|null Depending on RouterOS type detected:
126+
* - "nil" (the string "[]") or "nothing" (empty string) - NULL.
127+
* - "number" - int or double for large values.
128+
* - "bool" - a boolean.
129+
* - Unrecognized type - treated as an unquoted string.
130+
*/
131+
public static function parseValueToSimple($value)
93132
{
94133
$value = (string)$value;
95134

96-
if (in_array($value, array('', 'nil'), true)) {
135+
if (in_array($value, array('', '[]'), true)) {
97136
return null;
98137
} elseif (in_array($value, array('true', 'false', 'yes', 'no'), true)) {
99138
return $value === 'true' || $value === 'yes';
100139
} elseif ($value === (string)($num = (int)$value)
101140
|| $value === (string)($num = (double)$value)
102141
) {
103142
return $num;
104-
} elseif (preg_match(
143+
}
144+
return $value;
145+
}
146+
147+
/**
148+
* Parses a RouterOS value into a PHP object.
149+
*
150+
* Parses a RouterOS value into a PHP object.
151+
*
152+
* @param string $value The value to be parsed. Must be a literal of a
153+
* value, e.g. what {@link static::escapeValue()} will give you.
154+
*
155+
* @return string|DateInterval|DateTime Depending on RouterOS type detected:
156+
* - "time" - a {@link DateInterval} object.
157+
* - "date" (pseudo type; string in the form "M/j/Y") - a DateTime
158+
* object with the specified date, at midnight UTC time.
159+
* - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a
160+
* DateTime object with the specified date and UTC time.
161+
* - Unrecognized type - treated as an unquoted string.
162+
*/
163+
public static function parseValueToObject($value)
164+
{
165+
$value = (string)$value;
166+
if ('' === $value) {
167+
return $value;
168+
}
169+
170+
if (preg_match(
105171
'/^
106172
(?:(\d+)w)?
107173
(?:(\d+)d)?
@@ -200,61 +266,71 @@ public static function parseValue($value)
200266
} catch (E $e) {
201267
return $value;
202268
}
203-
} elseif (('"' === $value[0]) && substr(strrev($value), 0, 1) === '"') {
204-
return str_replace(
205-
array('\"', '\\\\', "\\\n", "\\\r\n", "\\\r"),
206-
array('"', '\\'),
207-
substr($value, 1, -1)
269+
}
270+
return $value;
271+
}
272+
273+
/**
274+
* Parses a RouterOS value into a PHP array.
275+
*
276+
* Parses a RouterOS value into a PHP array.
277+
*
278+
* @param string $value The value to be parsed. Must be a literal of a
279+
* value, e.g. what {@link static::escapeValue()} will give you.
280+
*
281+
* @return string|array Depending on RouterOS type detected:
282+
* - "array" - an array, with the keys and values processed recursively.
283+
* - Unrecognized type - treated as an unquoted string.
284+
*/
285+
public static function parseValueToArray($value)
286+
{
287+
$value = (string)$value;
288+
if ('{' === $value[0] && '}' === $value[strlen($value) - 1]) {
289+
$value = substr($value, 1, -1);
290+
if ('' === $value) {
291+
return array();
292+
}
293+
$parsedValue = preg_split(
294+
'/
295+
(\"(?:\\\\\\\\|\\\\"|[^"])*\")
296+
|
297+
(\{[^{}]*(?2)?\})
298+
|
299+
([^;=]+)
300+
/sx',
301+
$value,
302+
null,
303+
PREG_SPLIT_DELIM_CAPTURE
208304
);
209-
} elseif ('{' === $value[0]) {
210-
$len = strlen($value);
211-
if ($value[$len - 1] === '}') {
212-
$value = substr($value, 1, -1);
213-
if ('' === $value) {
214-
return array();
215-
}
216-
$parsedValue = preg_split(
217-
'/
218-
(\"(?:\\\\\\\\|\\\\"|[^"])*\")
219-
|
220-
(\{[^{}]*(?2)?\})
221-
|
222-
([^;=]+)
223-
/sx',
224-
$value,
225-
null,
226-
PREG_SPLIT_DELIM_CAPTURE
227-
);
228-
$result = array();
229-
$newVal = null;
230-
$newKey = null;
231-
for ($i = 0, $l = count($parsedValue); $i < $l; ++$i) {
232-
switch ($parsedValue[$i]) {
233-
case '':
234-
break;
235-
case ';':
236-
if (null === $newKey) {
237-
$result[] = $newVal;
238-
} else {
239-
$result[$newKey] = $newVal;
240-
}
241-
$newKey = $newVal = null;
242-
break;
243-
case '=':
244-
$newKey = static::parseValue($parsedValue[$i - 1]);
245-
$newVal = static::parseValue($parsedValue[++$i]);
246-
break;
247-
default:
248-
$newVal = static::parseValue($parsedValue[$i]);
305+
$result = array();
306+
$newVal = null;
307+
$newKey = null;
308+
for ($i = 0, $l = count($parsedValue); $i < $l; ++$i) {
309+
switch ($parsedValue[$i]) {
310+
case '':
311+
break;
312+
case ';':
313+
if (null === $newKey) {
314+
$result[] = $newVal;
315+
} else {
316+
$result[$newKey] = $newVal;
249317
}
318+
$newKey = $newVal = null;
319+
break;
320+
case '=':
321+
$newKey = static::parseValueToSimple($parsedValue[$i - 1]);
322+
$newVal = static::parseValue($parsedValue[++$i]);
323+
break;
324+
default:
325+
$newVal = static::parseValue($parsedValue[$i]);
250326
}
251-
if (null === $newKey) {
252-
$result[] = $newVal;
253-
} else {
254-
$result[$newKey] = $newVal;
255-
}
256-
return $result;
257327
}
328+
if (null === $newKey) {
329+
$result[] = $newVal;
330+
} else {
331+
$result[$newKey] = $newVal;
332+
}
333+
return $result;
258334
}
259335
return $value;
260336
}

tests/Misc/ConnectionlessTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ public function providerUtilParseValue()
10031003
// new DateInterval('PT0.001002003S')
10041004
//),
10051005
'' => array('', null),
1006-
'nil' => array('nil', null),
1006+
'[]' => array('[]', null),
10071007
'1' => array('1', 1),
10081008
'true' => array('true', true),
10091009
'yes' => array('yes', true),

0 commit comments

Comments
 (0)