@@ -98,22 +98,19 @@ public static function parseValue($value, DateTimeZone $timezone = null)
9898 return $ value ;
9999 }
100100
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+ }
114113 }
115-
116- return static ::parseValueToString ($ value );
117114 }
118115
119116 /**
@@ -181,23 +178,22 @@ public static function parseValueToSimple($value)
181178 * @param DateTimeZone|null $timezone The timezone which the resulting
182179 * DateTime object will use. Defaults to UTC.
183180 *
184- * @return string| DateTime Depending on RouterOS type detected:
181+ * @return DateTime Depending on RouterOS type detected:
185182 * - "date" (pseudo type; string in the form "M/j/Y") - a DateTime
186183 * object with the specified date, at midnight UTC time (regardless
187184 * of timezone provided).
188185 * - "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.
191190 */
192191 public static function parseValueToDateTime (
193192 $ value ,
194193 DateTimeZone $ timezone = null
195194 ) {
196195 $ value = (string )$ value ;
197- if ('' === $ value ) {
198- return $ value ;
199- }
200- if (preg_match (
196+ if ('' !== $ value && preg_match (
201197 '#^
202198 (?<mon>jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
203199 /
@@ -228,7 +224,10 @@ public static function parseValueToDateTime(
228224 return $ value ;
229225 }
230226 }
231- return $ value ;
227+ throw new ParserException (
228+ 'The supplied value can not be converted to a DateTime ' ,
229+ ParserException::CODE_DATETIME
230+ );
232231 }
233232
234233 /**
@@ -239,18 +238,14 @@ public static function parseValueToDateTime(
239238 * @param string $value The value to be parsed. Must be a literal of a
240239 * value, e.g. what {@link static::escapeValue()} will give you.
241240 *
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 .
245244 */
246245 public static function parseValueToDateInterval ($ value )
247246 {
248247 $ value = (string )$ value ;
249- if ('' === $ value ) {
250- return $ value ;
251- }
252-
253- if (preg_match (
248+ if ('' !== $ value && preg_match (
254249 '/^
255250 (?:(\d+)w)?
256251 (?:(\d+)d)?
@@ -323,8 +318,10 @@ public static function parseValueToDateInterval($value)
323318 }
324319 //@codeCoverageIgnoreEnd
325320 }
326-
327- return $ value ;
321+ throw new ParserException (
322+ 'The supplied value can not be converted to DateInterval ' ,
323+ ParserException::CODE_DATEINTERVAL
324+ );
328325 }
329326
330327 /**
@@ -338,11 +335,11 @@ public static function parseValueToDateInterval($value)
338335 * @param DateTimeZone|null $timezone The timezone which any resulting
339336 * DateTime object within the array will use. Defaults to UTC.
340337 *
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,
343339 * 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.
346343 */
347344 public static function parseValueToArray (
348345 $ value ,
@@ -396,7 +393,10 @@ public static function parseValueToArray(
396393 }
397394 return $ result ;
398395 }
399- return $ value ;
396+ throw new ParserException (
397+ 'The supplied value can not be converted to an array ' ,
398+ ParserException::CODE_DATETIME
399+ );
400400 }
401401
402402 /**
0 commit comments