Skip to content

Commit 55979f3

Browse files
committed
Removed Message::getAllArguments() in favor of Message::getIterator()@IteratorAggregate;
Changed Message::__invoke() with no arguments to be a shortcut of getTag(); Added Message::count()@countable; Added .scrutinizer.yml; Minor clean ups and polishes.
1 parent 03b0be8 commit 55979f3

File tree

13 files changed

+109
-38
lines changed

13 files changed

+109
-38
lines changed

.scrutinizer.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
imports:
2+
- php
3+
inherit: true
4+
tools:
5+
php_sim: false
6+
php_cpd: true
7+
php_code_sniffer:
8+
config:
9+
standard: PEAR

RELEASE-1.0.0b5

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
Console and some overall improvements.
22

33
* The PHAR file is now also a console. When installed, the console is available from the executable "roscon".
4+
* __BREAKING CHANGES:__
5+
- Removed Message::getAllArguments() in favor of IteratorAggregate implementation that returns an ArrayObject. For most intents and purposes, you should be able to just search&replace "->getAllArguments()" with an empty string (that will implicitly call getIterator()). If you require the arguments as a "real" array, you can replace "->getAllArguments()" with "->getIterator()->getArrayCopy()".
6+
- Message::\_\_invoke() without arguments is now a shortcut for getTag() instead of getting all arguments.
7+
- Util::changeMenu() now returns the Util object itself, except when you supply an empty string, when the current menu is returned (as before).
48
* New Util methods:
59
- getall()
610
- count()
711
- prepareScript()
12+
* Message now implements Countable, reporting the number of arguments.
813
* Util::get() now uses RouterOS' "get" command, unless it returns an empty !done response (as it does for RouterOS versions prior to 6.0), in which case it automatically fallbacks to a print with a query.
914
* Util::escapeValue() and Util::parseValue() now support associative arrays (introduced in RouterOS 6.2).
15+
* Util::escapeValue() now correctly converts DateTime objects to DateInterval objects relative to UNIX epoch time in UTC. Previously, the current time zone was used instead of UTC.
1016
* Util::add() and Util::set()/Util::edit() now support flags as values with a numeric key.
11-
* Util::changeMenu() now returns the Util object itself, except when you supply an empty string, when the current menu is returned (as before).
1217
* ResponseCollection can now be searched by argument values, if you first designate an argument name with the new ResponseCollection::setIndex() method.
1318
* ResponseCollection can now produce a sorted response collection based on user defined criteria using the new ResponseCollection::orderBy() method.
1419
* Doc fixes (Notably: Clarified the acceptability of seekable streams as argument values, which has been present for a long time, but never documented).

packagexmlsetup.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@
9696
) {
9797

9898
$tasksNs = $package->getTasksNs();
99-
if ($compatible) {
100-
$cTasksNs = $compatible->getTasksNs();
101-
}
99+
$cTasksNs = $compatible ? $compatible->getTasksNs() : '';
102100

103101
$oldCwd = getcwd();
104102
chdir(__DIR__);

src/PEAR2/Net/RouterOS/Client.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
*/
3131
use PEAR2\Net\Transmitter\NetworkStream as N;
3232

33+
/**
34+
* Catches arbitrary exceptions at some points.
35+
*/
36+
use Exception as E;
37+
3338
/**
3439
* A RouterOS client.
3540
*
@@ -228,7 +233,7 @@ public static function login(
228233
return $result;
229234
}
230235
return self::_login($com, $username, $password, $timeout);
231-
} catch (\Exception $e) {
236+
} catch (E $e) {
232237
if ($com->getTransmitter()->isPersistent() && null !== $old) {
233238
$com->getTransmitter()->lock($old, true);
234239
}
@@ -596,7 +601,7 @@ public function getPendingRequestsCount()
596601
* @param string $tag Tag of the request to cancel. Setting NULL will cancel
597602
* all requests.
598603
*
599-
* @return self|Client The client object.
604+
* @return $this The client object.
600605
* @see sendAsync()
601606
* @see close()
602607
*/
@@ -710,7 +715,7 @@ public function close()
710715
if (null !== $this->registry) {
711716
$this->registry->setTaglessMode(false);
712717
}
713-
$result = $response->getType() === Response::TYPE_FATAL;
718+
$result = $response[0]->getType() === Response::TYPE_FATAL;
714719
}
715720
$result = $result && $this->com->close();
716721
} catch (SocketException $e) {
@@ -744,7 +749,7 @@ public function __destruct()
744749
*
745750
* @param Request $request The request to send.
746751
*
747-
* @return self|Client The client object.
752+
* @return $this The client object.
748753
* @see sendSync()
749754
* @see sendAsync()
750755
*/

src/PEAR2/Net/RouterOS/Message.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
*/
2121
namespace PEAR2\Net\RouterOS;
2222

23+
/**
24+
* Implements this interface.
25+
*/
26+
use Countable;
27+
28+
/**
29+
* Implements this interface.
30+
*/
31+
use IteratorAggregate;
32+
33+
/**
34+
* Requred for IteratorAggregate::getIterator() to work properly with foreach.
35+
*/
36+
use ArrayObject;
37+
2338
/**
2439
* Represents a RouterOS message.
2540
*
@@ -29,7 +44,7 @@
2944
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
3045
* @link http://pear2.php.net/PEAR2_Net_RouterOS
3146
*/
32-
abstract class Message
47+
abstract class Message implements IteratorAggregate, Countable
3348
{
3449

3550
/**
@@ -59,7 +74,7 @@ abstract class Message
5974
public function __invoke($name = null)
6075
{
6176
if (null === $name) {
62-
return $this->getAllArguments();
77+
return $this->getTag();
6378
}
6479
return $this->getArgument($name);
6580
}
@@ -120,7 +135,7 @@ public function getTag()
120135
*
121136
* @param string $tag The tag to set.
122137
*
123-
* @return self|Message The message object.
138+
* @return $this The message object.
124139
* @see getTag()
125140
*/
126141
protected function setTag($tag)
@@ -150,14 +165,24 @@ public function getArgument($name)
150165
/**
151166
* Gets all arguments in an array.
152167
*
153-
* @return array An array with the keys as argument names, and the array
154-
* values as argument values.
168+
* @return ArrayObject An ArrayObject with the keys being argument names,
169+
* and the array values being argument values.
155170
* @see getArgument()
156171
* @see setArgument()
157172
*/
158-
public function getAllArguments()
173+
public function getIterator()
174+
{
175+
return new ArrayObject($this->arguments);
176+
}
177+
178+
/**
179+
* Counts the number of arguments.
180+
*
181+
* @return int The number of arguments.
182+
*/
183+
public function count()
159184
{
160-
return $this->arguments;
185+
return count($this->arguments);
161186
}
162187

163188
/**
@@ -173,7 +198,7 @@ public function getAllArguments()
173198
* Non seekable streams, as well as all other types, are casted to a
174199
* string.
175200
*
176-
* @return self|Message The message object.
201+
* @return $this The message object.
177202
* @see getArgument()
178203
*/
179204
protected function setArgument($name, $value = '')
@@ -190,7 +215,7 @@ protected function setArgument($name, $value = '')
190215
/**
191216
* Removes all arguments from the message.
192217
*
193-
* @return self|Message The message object.
218+
* @return $this The message object.
194219
*/
195220
protected function removeAllArguments()
196221
{

src/PEAR2/Net/RouterOS/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected static function sanitizeAction($action)
114114
* @param string $action One of the ACTION_* constants. Describes the
115115
* operation to perform.
116116
*
117-
* @return static The query object.
117+
* @return static A new query object.
118118
*/
119119
public static function where(
120120
$name,

src/PEAR2/Net/RouterOS/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private function _send(Communicator $com)
299299
if (null !== ($tag = $this->getTag())) {
300300
$bytes += $com->sendWord('.tag=' . $tag);
301301
}
302-
foreach ($this->getAllArguments() as $name => $value) {
302+
foreach ($this->arguments as $name => $value) {
303303
$prefix = '=' . $name . '=';
304304
if (is_string($value)) {
305305
$bytes += $com->sendWord($prefix . $value);

src/PEAR2/Net/RouterOS/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private function _receive(
228228
*
229229
* @param string $type The new response type.
230230
*
231-
* @return self|Response The response object.
231+
* @return $this The response object.
232232
* @see getType()
233233
*/
234234
protected function setType($type)

src/PEAR2/Net/RouterOS/ResponseCollection.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@
4343
* @author Vasil Rangelov <boen.robot@gmail.com>
4444
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
4545
* @link http://pear2.php.net/PEAR2_Net_RouterOS
46+
*
47+
* @method string getType()
48+
* Calls {@link Response::getType()}
49+
* on the response pointed by the pointer.
50+
* @method string getUnrecognizedWords()
51+
* Calls {@link Response::getUnrecognizedWords()}
52+
* on the response pointed by the pointer.
53+
* @method string getArgument(string $name)
54+
* Calls {@link Response::getArgument()}
55+
* on the response pointed by the pointer.
56+
* @method string getTag()
57+
* Calls {@link Response::getTag()}
58+
* on the response pointed by the pointer.
4659
*/
4760
class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
4861
{
@@ -380,7 +393,8 @@ public function getArgumentMap()
380393
if (null === $this->argumentMap) {
381394
$arguments = array();
382395
foreach ($this->responses as $index => $response) {
383-
foreach (array_keys($response->getAllArguments()) as $name) {
396+
$names = array_keys($response->getIterator()->getArrayCopy());
397+
foreach ($names as $name) {
384398
if (!isset($arguments[$name])) {
385399
$arguments[$name] = array();
386400
}

tests/Client/Safe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ public function testStreamEquality()
774774
);
775775

776776
foreach ($list as $index => $response) {
777-
$streamListArgs = $streamList[$index]->getAllArguments();
778-
foreach ($response->getAllArguments() as $argName => $value) {
777+
$streamListArgs = $streamList[$index]->getIterator();
778+
foreach ($response as $argName => $value) {
779779
$this->assertArrayHasKey(
780780
$argName,
781781
$streamListArgs,

0 commit comments

Comments
 (0)