@@ -70,38 +70,104 @@ class Script
70
70
* This method is intended to be the very opposite of
71
71
* {@link static::escapeValue()}. That is, results from that method, if
72
72
* 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".
77
73
*
78
74
* @param string $value The value to be parsed. Must be a literal of a
79
75
* value, e.g. what {@link static::escapeValue()} will give you.
80
76
*
81
77
* @return mixed Depending on RouterOS type detected:
82
- * - "nil" or "nothing" - NULL.
78
+ * - "nil" (the string "[]") or "nothing" (empty string) - NULL.
83
79
* - "number" - int or double for large values.
84
80
* - "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.
87
82
* - "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.
90
88
* - Unrecognized type - treated as an unquoted string.
91
89
*/
92
90
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 )
93
132
{
94
133
$ value = (string )$ value ;
95
134
96
- if (in_array ($ value , array ('' , 'nil ' ), true )) {
135
+ if (in_array ($ value , array ('' , '[] ' ), true )) {
97
136
return null ;
98
137
} elseif (in_array ($ value , array ('true ' , 'false ' , 'yes ' , 'no ' ), true )) {
99
138
return $ value === 'true ' || $ value === 'yes ' ;
100
139
} elseif ($ value === (string )($ num = (int )$ value )
101
140
|| $ value === (string )($ num = (double )$ value )
102
141
) {
103
142
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 (
105
171
'/^
106
172
(?:(\d+)w)?
107
173
(?:(\d+)d)?
@@ -200,61 +266,71 @@ public static function parseValue($value)
200
266
} catch (E $ e ) {
201
267
return $ value ;
202
268
}
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
208
304
);
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 ;
249
317
}
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 ]);
250
326
}
251
- if (null === $ newKey ) {
252
- $ result [] = $ newVal ;
253
- } else {
254
- $ result [$ newKey ] = $ newVal ;
255
- }
256
- return $ result ;
257
327
}
328
+ if (null === $ newKey ) {
329
+ $ result [] = $ newVal ;
330
+ } else {
331
+ $ result [$ newKey ] = $ newVal ;
332
+ }
333
+ return $ result ;
258
334
}
259
335
return $ value ;
260
336
}
0 commit comments