Skip to content

Commit 5ed1fa6

Browse files
committed
Added ParserException, used in Script for parser errors on complex values;
Doc fix at Util::getAll().
1 parent 12ac2db commit 5ed1fa6

File tree

3 files changed

+84
-40
lines changed

3 files changed

+84
-40
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* Base of this class.
25+
*/
26+
use DomainException;
27+
28+
/**
29+
* Exception thrown when a value can't be parsed properly.
30+
*
31+
* @category Net
32+
* @package PEAR2_Net_RouterOS
33+
* @author Vasil Rangelov <[email protected]>
34+
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
35+
* @link http://pear2.php.net/PEAR2_Net_RouterOS
36+
*/
37+
class ParserException extends DomainException implements Exception
38+
{
39+
const CODE_DATETIME = 1;
40+
const CODE_DATEINTERVAL = 2;
41+
const CODE_ARRAY = 3;
42+
}

src/PEAR2/Net/RouterOS/Script.php

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ public function count($mode = COUNT_NORMAL, Query $query = null)
769769
* allowing the results to be observed.
770770
* If you need to use those arguments, use {@link static::newRequest()},
771771
* and pass the resulting {@link Request} to {@link Client::sendAsync()}.
772+
* The "count-only" argument is also prohibited, as results from it
773+
* would not be consumable. Use {@link static::count()} for that.
772774
* @param Query|null $query A query to
773775
* filter items by.
774776
* NULL to get all items.
@@ -778,7 +780,7 @@ public function count($mode = COUNT_NORMAL, Query $query = null)
778780
* when there are no matching items. FALSE on failure.
779781
*
780782
* @throws NotSupportedException If $args contains prohibited arguments
781-
* ("follow" or "follow-only").
783+
* ("follow", "follow-only" or "count-only").
782784
*/
783785
public function getAll(array $args = array(), Query $query = null)
784786
{
@@ -890,7 +892,7 @@ public function filePutContents($filename, $data, $overwrite = false)
890892
return Communicator::seekableStreamLength($data) == $fileSize;
891893
} else {
892894
return sprintf('%u', strlen((string)$data)) === $fileSize;
893-
};
895+
}
894896
}
895897

896898
/**

0 commit comments

Comments
 (0)