Skip to content

Commit

Permalink
Merge pull request #48 from PronerInformatica/master
Browse files Browse the repository at this point in the history
Created options in read method
  • Loading branch information
EvilFreelancer committed Nov 30, 2020
2 parents 45c2722 + 5ce7628 commit 07cbe0e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
21 changes: 18 additions & 3 deletions src/Client.php
Expand Up @@ -226,15 +226,19 @@ private function writeRAW(QueryInterface $query): ClientInterface
/**
* Read RAW response from RouterOS, it can be /export command results also, not only array from API
*
* @param array $options
*
* @return array|string
* @since 1.0.0
*/
public function readRAW()
public function readRAW(array $options = [])
{
// By default response is empty
$response = [];
// We have to wait a !done or !fatal
$lastReply = false;
// Count !re in response
$countResponse = 0;

// Convert strings to array and return results
if ($this->isCustomOutput()) {
Expand All @@ -246,6 +250,11 @@ public function readRAW()
while (true) {
$word = $this->connector->readWord();

//Limit response number to finish the read
if (isset($options['count']) && $countResponse >= (int)$options['count']) {
$lastReply = true;
}

if ('' === $word) {
if ($lastReply) {
// We received a !done or !fatal message in a precedent loop
Expand All @@ -266,6 +275,11 @@ public function readRAW()
if ('!done' === $word || '!fatal' === $word) {
$lastReply = true;
}

// If we get a !re line in response, we increment the variable
if ('!re' === $word) {
$countResponse++;
}
}

// Parse results and return
Expand All @@ -282,13 +296,14 @@ public function readRAW()
* A !fatal block precedes TCP connexion close
*
* @param bool $parse If need parse output to array
* @param array $options
*
* @return mixed
*/
public function read(bool $parse = true)
public function read(bool $parse = true, array $options = [])
{
// Read RAW response
$response = $this->readRAW();
$response = $this->readRAW($options);

// Return RAW configuration if custom output is set
if ($this->isCustomOutput()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Interfaces/ClientInterface.php
Expand Up @@ -27,10 +27,11 @@ public function getSocket();
* A !fatal block precedes TCP connexion close
*
* @param bool $parse If need parse output to array
* @param array $options If need pass options
*
* @return mixed
*/
public function read(bool $parse = true);
public function read(bool $parse = true, array $options = []);

/**
* Send write query to RouterOS (modern version of write)
Expand Down
5 changes: 3 additions & 2 deletions src/ShortsTrait.php
Expand Up @@ -33,13 +33,14 @@ public function q($endpoint, array $where = null, string $operations = null, str
* Alias for ->read() method
*
* @param bool $parse If need parse output to array
* @param array $options
*
* @return mixed
* @since 0.7
*/
public function r(bool $parse = true)
public function r(bool $parse = true, array $options = [])
{
return $this->read($parse);
return $this->read($parse, $options);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/ClientTest.php
Expand Up @@ -249,6 +249,12 @@ public function testQueryRead(): void

// $this->assertCount(13, $read);
$this->assertEquals('zzzz', $read[0]['tag']);

/*
* Build query with option count
*/
$read = $this->client->query('/interface/monitor-traffic')->read(true, ['count' => 3]);
$this->assertCount(3, $read);
}

public function testReadAsIterator(): void
Expand Down

0 comments on commit 07cbe0e

Please sign in to comment.