Skip to content

Commit

Permalink
Added __debugInfo() to Message, Request, Response and Query, and Resp…
Browse files Browse the repository at this point in the history
…onseCollection;

ResponseCollection::toArray() now accepts a bit mask of its new constants, enabling optional serialization of the Response objects inside;

The bootstrap.php's resolve function now resolves unknown hosts to "0.0.0.0", which allows for the unconnected tests to run normally.
  • Loading branch information
boenrobot committed Oct 4, 2018
1 parent ead4342 commit 0a8e77c
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 8 deletions.
1 change: 1 addition & 0 deletions RELEASE-1.0.0b7
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* Util::get() and Util::find() no longer treat numeric arguments as numbers, but instead treat them as literals to pass to the router. This means that items with names that resemble numbers can now be targeted.
* Util::find() no longer processes comma separated lists recursively. Instead, they are included in the result after a normalization. This means that items with names that are also the names of callbacks can be targeted by simply adding a "," at the end of the name.
* Different response properties can now be streamed depending on a threshold, instead of "all or nothing", as was before.
* ResponseCollection::toArray() now accepts a bit mask of ResponseCollection::ARRAY_\* constants, which allow the use of indexes (uses 1 for compatibility), and the new recursive option, which allows getting the individual responses serialized as a plain array.
* Doc fixes.
18 changes: 18 additions & 0 deletions src/PEAR2/Net/RouterOS/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,22 @@ protected function removeAllAttributes()
$this->attributes = array();
return $this;
}

/**
* Get actionable debug info.
*
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
*
* @return array The info, as an associative array.
*/
public function __debugInfo()
{
return array(
'attributes' => $this->attributes,
'tag' => $this->_tag
);
}
}
15 changes: 15 additions & 0 deletions src/PEAR2/Net/RouterOS/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ public function verify(Communicator $com)
return $this;
}

/**
* Get actionable debug info.
*
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
*
* @return array The info, as an associative array.
*/
public function __debugInfo()
{
return $this->words;
}

/**
* Adds a condition.
*
Expand Down
18 changes: 18 additions & 0 deletions src/PEAR2/Net/RouterOS/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,24 @@ public function removeAllArguments()
return parent::removeAllAttributes();
}

/**
* Get actionable debug info.
*
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
*
* @return array The info, as an associative array.
*/
public function __debugInfo()
{
return parent::__debugInfo() + array(
'command' => $this->_command,
'query' => $this->_query
);
}

/**
* Sends a request over a communicator.
*
Expand Down
19 changes: 18 additions & 1 deletion src/PEAR2/Net/RouterOS/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Response extends Message
/**
* An array of unrecognized words in network order.
*
* @var string[]
* @var (string|resource)[]
*/
protected $unrecognizedWords = array();

Expand Down Expand Up @@ -344,4 +344,21 @@ public function getUnrecognizedWords()
{
return $this->unrecognizedWords;
}

/**
* Get actionable debug info.
*
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
*
* @return array The info, as an associative array.
*/
public function __debugInfo()
{
return parent::__debugInfo() + array(
'unrecognized' => $this->unrecognizedWords
);
}
}
59 changes: 52 additions & 7 deletions src/PEAR2/Net/RouterOS/ResponseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@
class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
{

/**
* Used in {@link static::toArray()} as part of a bit mask.
*
* If set, it is effectively ignored. It is used merely as a placeholder.
*/
const ARRAY_DEFAULT = 0;

/**
* Used in {@link static::toArray()} as part of a bit mask.
*
* If set, uses an index when available.
* if not set, ignores it even if one was set with
* {@link static::setIndex()}.
*/
const ARRAY_INDEXED = 1;

/**
* Used in {@link static::toArray()} as part of a bit mask.
*
* If set, also serializes all {@link Response} objects to a plain array
* using the __debugInfo() method.
* If not set, the result will be an array of {@link Response} objects.
*/
const ARRAY_RECURSIVE = 2;

/**
* An array with all {@link Response} objects.
*
Expand Down Expand Up @@ -166,7 +191,7 @@ public function __construct(array $responses)
* If the collection is indexed, you can also supply a value to seek to.
* Setting NULL will get the current response's iterator.
*
* @return Response|ArrayObject The {@link Response} at the specified
* @return Response|\ArrayObject The {@link Response} at the specified
* offset, the current response's iterator (which is an ArrayObject)
* when NULL is given, or FALSE if the offset is invalid
* or the collection is empty.
Expand Down Expand Up @@ -222,23 +247,43 @@ public function getIndex()
/**
* Gets the whole collection as an array.
*
* @param bool $useIndex Whether to use the index values as keys for the
* resulting array.
* @param int $flags A bit mask of this class' ARRAY_* constants.
*
* @return Response[] An array with all responses, in network order.
*/
public function toArray($useIndex = false)
public function toArray($flags = self::ARRAY_DEFAULT)
{
if ($useIndex) {
$result = $this->responses;
if (($flags & self::ARRAY_INDEXED) === self::ARRAY_INDEXED) {
$positions = $this->responsesIndex[$this->index];
asort($positions, SORT_NUMERIC);
$positions = array_flip($positions);
return array_combine(
$result = array_combine(
$positions,
array_intersect_key($this->responses, $positions)
);
}
return $this->responses;
if (($flags & self::ARRAY_RECURSIVE) === self::ARRAY_RECURSIVE) {
foreach ($result as $key => $value) {
$result[$key] = $value->__debugInfo();
}
}
return $result;
}

/**
* Get actionable debug info.
*
* This is a magic method available to PHP 5.6 and above, due to which
* output of var_dump() will be more actionable.
*
* You can still call it in earlier versions to get the object as a plain array.
*
* @return array The info, as an associative array.
*/
public function __debugInfo()
{
return $this->toArray(self::ARRAY_INDEXED | self::ARRAY_RECURSIVE);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
}
}
}
return '0.0.0.0';
};

if ($defineConstants) {
Expand Down

0 comments on commit 0a8e77c

Please sign in to comment.