diff --git a/src/Client.php b/src/Client.php index ce89b02..796fb32 100644 --- a/src/Client.php +++ b/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()) { diff --git a/src/Interfaces/ClientInterface.php b/src/Interfaces/ClientInterface.php index 2b81e1e..aecc698 100644 --- a/src/Interfaces/ClientInterface.php +++ b/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) diff --git a/src/ShortsTrait.php b/src/ShortsTrait.php index 2287a41..9495cf1 100644 --- a/src/ShortsTrait.php +++ b/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); } /** diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 52adb62..77682c3 100644 --- a/tests/ClientTest.php +++ b/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