Browse Source

Merge pull request #48 from PronerInformatica/master

Created options in read method
tags/1.3.2
Paul Zloi 5 years ago
committed by GitHub
parent
commit
07cbe0e2f6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      src/Client.php
  2. 3
      src/Interfaces/ClientInterface.php
  3. 5
      src/ShortsTrait.php
  4. 6
      tests/ClientTest.php

21
src/Client.php

@ -226,15 +226,19 @@ class Client implements Interfaces\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()) {
@ -246,6 +250,11 @@ class Client implements Interfaces\ClientInterface
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
@ -266,6 +275,11 @@ class Client implements Interfaces\ClientInterface
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
@ -282,13 +296,14 @@ class Client implements Interfaces\ClientInterface
* 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()) {

3
src/Interfaces/ClientInterface.php

@ -27,10 +27,11 @@ interface ClientInterface
* 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)

5
src/ShortsTrait.php

@ -33,13 +33,14 @@ trait ShortsTrait
* 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);
}
/**

6
tests/ClientTest.php

@ -249,6 +249,12 @@ class ClientTest extends TestCase
// $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

Loading…
Cancel
Save