Skip to content

Commit c80f632

Browse files
committed
Added Util::getCurrentTime();
Added pseudo "date" support to Util::parseValue(); Enabled Util::setMenu() to go back to root.
1 parent 193d995 commit c80f632

File tree

5 files changed

+126
-10
lines changed

5 files changed

+126
-10
lines changed

RELEASE-1.0.0b6

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Bug fixes, mostly.
22

33
* New Util methods:
4-
- newRequest()
54
- comment()
5+
- getCurrentTime()
6+
- newRequest()
67
* Util::parseValue() now supports letter notation for time (1h2m3s), not just double colon notation (01:02:03), modeled after RouterOS. Related to that is also that leading zeroes, and zero minutes and seconds are now optional (e.g. "1:" is a valid way of saying 1 hour). Sub-second information is rounded up to the nearest second on current PHP versions (future versions are expected to support sub-second information in DateInterval by allowing seconds to be a double; The code currently attempts to give DateInterval a double, falling back to rounding to a second).
7-
* Client::login() consumes the !done or !fatal response even when called on an already logged in connection.
8+
* Client::login() consumes the !done or !fatal response even when called on an already logged in connection.
9+
* Util::setMenu() can now go back to the root menu.

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
*/
3131
use DateInterval;
3232

33+
/**
34+
* Used at {@link Util::getCurrentTime()} to get the proper time.
35+
*/
36+
use DateTimeZone;
37+
3338
/**
3439
* Implemented by this class.
3540
*/
@@ -86,6 +91,10 @@ class Util implements Countable
8691
* This method is intended to be the very opposite of
8792
* {@link static::escapeValue()}. That is, results from that method, if
8893
* given to this method, should produce equivalent results.
94+
*
95+
* For better usefulness, in addition to "actual" RouterOS types, a pseudo
96+
* "date" type is also recognized, whenever the string is in the form
97+
* "M/j/Y".
8998
*
9099
* @param string $value The value to be parsed. Must be a literal of a
91100
* value, e.g. what {@link static::escapeValue()} will give you.
@@ -97,6 +106,8 @@ class Util implements Countable
97106
* - "time" - a {@link DateInterval} object.
98107
* - "array" - an array, with the values processed recursively.
99108
* - "str" - a string.
109+
* - "date" (pseudo type) - a DateTime object with the specified date,
110+
* at midnight UTC time.
100111
* - Unrecognized type - treated as an unquoted string.
101112
*/
102113
public static function parseValue($value)
@@ -169,20 +180,40 @@ public static function parseValue($value)
169180
return new DateInterval(
170181
"P{$days}DT{$time[3]}H{$time[4]}M{$secondsSpec}S"
171182
);
172-
//@codeCoverageIgnoreStart
173-
// See previous ignored section's note.
174-
//
175-
// This section is added for backwards compatibility with current
176-
// PHP versions, when in the future sub-second support is added.
177-
// In that event, the test inputs for older versions will be
178-
// expected to get a rounded up result of the sub-second data.
183+
//@codeCoverageIgnoreStart
184+
// See previous ignored section's note.
185+
//
186+
// This section is added for backwards compatibility with current
187+
// PHP versions, when in the future sub-second support is added.
188+
// In that event, the test inputs for older versions will be
189+
// expected to get a rounded up result of the sub-second data.
179190
} catch (E $e) {
180191
$secondsSpec = (int)round($secondsSpec);
181192
return new DateInterval(
182193
"P{$days}DT{$time[3]}H{$time[4]}M{$secondsSpec}S"
183194
);
184195
}
185196
//@codeCoverageIgnoreEnd
197+
} elseif (preg_match(
198+
'#^
199+
(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
200+
/
201+
(\d\d?)
202+
/
203+
(\d{4})
204+
$#uix',
205+
$value,
206+
$date
207+
)) {
208+
try {
209+
return DateTime::createFromFormat(
210+
'M/j/Y',
211+
ucfirst($date[1]) . '/' . (int)$date[2] . '/' . $date[3],
212+
new DateTimeZone('UTC')
213+
);
214+
} catch (E $e) {
215+
return $value;
216+
}
186217
} elseif (('"' === $value[0]) && substr(strrev($value), 0, 1) === '"') {
187218
return str_replace(
188219
array('\"', '\\\\', "\\\n", "\\\r\n", "\\\r"),
@@ -500,7 +531,9 @@ public function setMenu($newMenu)
500531
$newMenu = (string)$newMenu;
501532
if ('' !== $newMenu) {
502533
$menuRequest = new Request('/menu');
503-
if ('/' === $newMenu[0]) {
534+
if ('/' === $newMenu) {
535+
$this->menu = '/';
536+
} elseif ('/' === $newMenu[0]) {
504537
$this->menu = $menuRequest->setCommand($newMenu)->getCommand();
505538
} else {
506539
$this->menu = $menuRequest->setCommand(
@@ -639,6 +672,39 @@ public function clearIdCache()
639672
return $this;
640673
}
641674

675+
/**
676+
* Gets the current time on the router.
677+
*
678+
* Gets the current time on the router, regardless of the current menu.
679+
*
680+
* If your router uses a "manual" timezone, the resulting object will use
681+
* the "gmt-offset" as the timezone identifier.
682+
*
683+
* @return DateTime The current time of the router, as a DateTime object.
684+
*/
685+
public function getCurrentTime()
686+
{
687+
$clock = $this->client->sendSync(
688+
new Request('/system/clock/print')
689+
)->current();
690+
$datetime = ucfirst($clock->getProperty('date')) . ' ' .
691+
$clock->getProperty('time');
692+
if ('manual' === $clock->getProperty('time-zone-name')) {
693+
$result = DateTime::createFromFormat(
694+
'M/j/Y H:i:s P',
695+
$datetime . ' ' . $clock->getProperty('gmt-offset'),
696+
new DateTimeZone('UTC')
697+
);
698+
} else {
699+
$result = DateTime::createFromFormat(
700+
'M/j/Y H:i:s',
701+
$datetime,
702+
new DateTimeZone($clock->getProperty('time-zone-name'))
703+
);
704+
}
705+
return $result;
706+
}
707+
642708
/**
643709
* Finds the IDs of items at the current menu.
644710
*

tests/Util/Safe.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abstract class Safe extends PHPUnit_Framework_TestCase
2929
public function testSetGetMenu()
3030
{
3131
$this->assertSame('/', $this->util->getMenu());
32+
$this->assertSame('/', $this->util->setMenu('/')->getMenu());
3233
$this->assertSame(
3334
'/queue',
3435
$this->util->setMenu('queue')->getMenu()

tests/Util/Unsafe.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,47 @@ public function testSetAndEdit()
261261

262262
$this->util->remove($id);
263263
}
264+
265+
/**
266+
* @depends testSetAndEdit
267+
*/
268+
public function testGetCurrentTime()
269+
{
270+
$originalTimezone = $this->util->setMenu('/system clock')
271+
->get(null, 'time-zone-name');
272+
$originalTimezoneAutodetect = $this->util
273+
->get(null, 'time-zone-autodetect');
274+
275+
$this->util->set(
276+
null,
277+
array(
278+
'time-zone-autodetect' => 'false',
279+
'time-zone-name' => TEST_TIMEZONE
280+
)
281+
);
282+
$curTime = $this->util->setMenu('/')->getCurrentTime();
283+
$this->assertInstanceOf(
284+
'DateTime',
285+
$curTime
286+
);
287+
$this->assertSame(TEST_TIMEZONE, $curTime->getTimezone()->getName());
288+
289+
$this->util->setMenu('/system clock')
290+
->set(null, array('time-zone-name' => 'manual'));
291+
$curTimeInManual = $this->util->setMenu('/')->getCurrentTime();
292+
$this->assertNotSame(
293+
TEST_TIMEZONE,
294+
$curTimeInManual->getTimezone()->getName()
295+
);
296+
297+
$this->util->setMenu('/system clock')->set(
298+
null,
299+
array(
300+
'time-zone-name' => $originalTimezone,
301+
'time-zone-autodetect' => $originalTimezoneAutodetect
302+
)
303+
);
304+
}
264305

265306
/**
266307
* @depends testRemove

tests/phpunit.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@
117117
-->
118118
<const name="TEST_FILE_NAME" value="API_TEST.txt" />
119119

120+
<!--
121+
A name for a test timezone. It should be a name not in use by the
122+
router or server, but not necesarily, and must NOT be UTC.
123+
-->
124+
<const name="TEST_TIMEZONE" value="Europe/Sofia" />
125+
120126
</php>
121127
<testsuites>
122128
<testsuite name="All Tests">

0 commit comments

Comments
 (0)