@@ -98,22 +98,19 @@ public static function parseValue($value, DateTimeZone $timezone = null)
98
98
return $ value ;
99
99
}
100
100
101
- $ value = static ::parseValueToArray ($ value , $ timezone );
102
- if (!is_string ($ value )) {
103
- return $ value ;
104
- }
105
-
106
- $ value = static ::parseValueToDateInterval ($ value );
107
- if (!is_string ($ value )) {
108
- return $ value ;
109
- }
110
-
111
- $ value = static ::parseValueToDateTime ($ value , $ timezone );
112
- if (!is_string ($ value )) {
113
- return $ value ;
101
+ try {
102
+ return static ::parseValueToArray ($ value , $ timezone );
103
+ } catch (ParserException $ e ) {
104
+ try {
105
+ return static ::parseValueToDateInterval ($ value );
106
+ } catch (ParserException $ e ) {
107
+ try {
108
+ return static ::parseValueToDateTime ($ value , $ timezone );
109
+ } catch (ParserException $ e ) {
110
+ return static ::parseValueToString ($ value );
111
+ }
112
+ }
114
113
}
115
-
116
- return static ::parseValueToString ($ value );
117
114
}
118
115
119
116
/**
@@ -181,23 +178,22 @@ public static function parseValueToSimple($value)
181
178
* @param DateTimeZone|null $timezone The timezone which the resulting
182
179
* DateTime object will use. Defaults to UTC.
183
180
*
184
- * @return string| DateTime Depending on RouterOS type detected:
181
+ * @return DateTime Depending on RouterOS type detected:
185
182
* - "date" (pseudo type; string in the form "M/j/Y") - a DateTime
186
183
* object with the specified date, at midnight UTC time (regardless
187
184
* of timezone provided).
188
185
* - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a
189
- * DateTime object with the specified date and time.
190
- * - Unrecognized type - casted to a string, unmodified.
186
+ * DateTime object with the specified date and time,
187
+ * with the specified timezone.
188
+ *
189
+ * @throws ParserException When the value is not of a recognized type.
191
190
*/
192
191
public static function parseValueToDateTime (
193
192
$ value ,
194
193
DateTimeZone $ timezone = null
195
194
) {
196
195
$ value = (string )$ value ;
197
- if ('' === $ value ) {
198
- return $ value ;
199
- }
200
- if (preg_match (
196
+ if ('' !== $ value && preg_match (
201
197
'#^
202
198
(?<mon>jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
203
199
/
@@ -228,7 +224,10 @@ public static function parseValueToDateTime(
228
224
return $ value ;
229
225
}
230
226
}
231
- return $ value ;
227
+ throw new ParserException (
228
+ 'The supplied value can not be converted to a DateTime ' ,
229
+ ParserException::CODE_DATETIME
230
+ );
232
231
}
233
232
234
233
/**
@@ -239,18 +238,14 @@ public static function parseValueToDateTime(
239
238
* @param string $value The value to be parsed. Must be a literal of a
240
239
* value, e.g. what {@link static::escapeValue()} will give you.
241
240
*
242
- * @return string| DateInterval|DateTime Depending on RouterOS type detected:
243
- * - "time" - a {@link DateInterval} object.
244
- * - Unrecognized type - casted to a string, unmodified .
241
+ * @return DateInterval The value as a DateInterval object.
242
+ *
243
+ * @throws ParserException When the value is not of a recognized type .
245
244
*/
246
245
public static function parseValueToDateInterval ($ value )
247
246
{
248
247
$ value = (string )$ value ;
249
- if ('' === $ value ) {
250
- return $ value ;
251
- }
252
-
253
- if (preg_match (
248
+ if ('' !== $ value && preg_match (
254
249
'/^
255
250
(?:(\d+)w)?
256
251
(?:(\d+)d)?
@@ -323,8 +318,10 @@ public static function parseValueToDateInterval($value)
323
318
}
324
319
//@codeCoverageIgnoreEnd
325
320
}
326
-
327
- return $ value ;
321
+ throw new ParserException (
322
+ 'The supplied value can not be converted to DateInterval ' ,
323
+ ParserException::CODE_DATEINTERVAL
324
+ );
328
325
}
329
326
330
327
/**
@@ -338,11 +335,11 @@ public static function parseValueToDateInterval($value)
338
335
* @param DateTimeZone|null $timezone The timezone which any resulting
339
336
* DateTime object within the array will use. Defaults to UTC.
340
337
*
341
- * @return string|array Depending on RouterOS type detected:
342
- * - "array" - an array, with the and values processed recursively,
338
+ * @return array An array, with the keys and values processed recursively,
343
339
* the keys with {@link static::parseValueToSimple()},
344
- * and the values with {@link static::parseValue()}
345
- * - Unrecognized type - casted to a string, unmodified.
340
+ * and the values with {@link static::parseValue()}.
341
+ *
342
+ * @throws ParserException When the value is not of a recognized type.
346
343
*/
347
344
public static function parseValueToArray (
348
345
$ value ,
@@ -396,7 +393,10 @@ public static function parseValueToArray(
396
393
}
397
394
return $ result ;
398
395
}
399
- return $ value ;
396
+ throw new ParserException (
397
+ 'The supplied value can not be converted to an array ' ,
398
+ ParserException::CODE_DATETIME
399
+ );
400
400
}
401
401
402
402
/**
0 commit comments