Browse Source

Merge branch 'master' of https://github.com/arily/routeros-api-php

pull/14/head
arily 6 years ago
parent
commit
969c05faee
  1. BIN
      src/.DS_Store
  2. 38
      src/Client.php
  3. 11
      src/Iterators/ResponseIterator.php

BIN
src/.DS_Store

38
src/Client.php

@ -14,8 +14,7 @@ use RouterOS\Interfaces\ClientInterface;
* @package RouterOS
* @since 0.1
*/
class Client implements Interfaces\ClientInterface
{
class Client implements Interfaces\ClientInterface {
use SocketTrait, ShortsTrait;
/**
@ -41,8 +40,7 @@ class Client implements Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException
*/
public function __construct($config)
{
public function __construct($config) {
// If array then need create object
if (\is_array($config)) {
$config = new Config($config);
@ -69,8 +67,7 @@ class Client implements Interfaces\ClientInterface
* @return mixed
* @throws \RouterOS\Exceptions\ConfigException
*/
private function config(string $parameter)
{
private function config(string $parameter) {
return $this->_config->get($parameter);
}
@ -80,8 +77,7 @@ class Client implements Interfaces\ClientInterface
* @return \RouterOS\Config
* @since 0.6
*/
public function getConfig(): Config
{
public function getConfig(): Config {
return $this->_config;
}
@ -91,8 +87,7 @@ class Client implements Interfaces\ClientInterface
* @param \RouterOS\Config $config
* @since 0.7
*/
public function setConfig(Config $config)
{
public function setConfig(Config $config) {
$this->_config = $config;
}
@ -103,8 +98,7 @@ class Client implements Interfaces\ClientInterface
* @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\QueryException
*/
public function write($query): ClientInterface
{
public function write($query): ClientInterface {
if (\is_string($query)) {
$query = new Query($query);
} elseif (\is_array($query)) {
@ -139,8 +133,7 @@ class Client implements Interfaces\ClientInterface
* @param bool $parse
* @return mixed
*/
public function read(bool $parse = true)
{
public function read(bool $parse = true) {
// By default response is empty
$response = [];
// We have to wait a !done or !fatal
@ -176,14 +169,12 @@ class Client implements Interfaces\ClientInterface
return $parse ? $this->rosario($response) : $response;
}
/**
* Read using Iterators to improve performance on large dataset
*
* @return Iterators\ResponseIterator
*/
public function readAsIterator()
{
public function readAsIterator() {
return new Iterators\ResponseIterator($this->read(false));
}
@ -231,7 +222,6 @@ class Client implements Interfaces\ClientInterface
return $result;
}
/**
* Parse response from Router OS
*
@ -281,8 +271,7 @@ class Client implements Interfaces\ClientInterface
* @param string $value
* @param array $matches
*/
private function pregResponse(string $value, &$matches)
{
private function pregResponse(string $value, &$matches) {
preg_match_all('/^[=|\.](.*)=(.*)/', $value, $matches);
}
@ -295,8 +284,7 @@ class Client implements Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException
*/
private function login(bool $legacyRetry = false): bool
{
private function login(bool $legacyRetry = false): bool {
// If legacy login scheme is enabled
if ($this->config('legacy')) {
// For the first we need get hash with salt
@ -342,8 +330,7 @@ class Client implements Interfaces\ClientInterface
* @return bool
* @throws ConfigException
*/
private function isLegacy(array &$response): bool
{
private function isLegacy(array &$response): bool {
return \count($response) > 1 && $response[0] === '!done' && !$this->config('legacy');
}
@ -355,8 +342,7 @@ class Client implements Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException
*/
private function connect(): bool
{
private function connect(): bool{
// By default we not connected
$connected = false;

11
src/Iterators/ResponseIterator.php

@ -8,16 +8,18 @@
namespace RouterOS\Iterators;
/**
* Class ResponseIterator
* @package RouterOS\Iterators
*/
class ResponseIterator implements \Iterator, \ArrayAccess, \Countable {
public $parsed = [];
public $raw = [];
public $current;
public $length;
public function __construct($raw) {
$this->current = 0;
// This RAW should't be an error
@ -35,10 +37,8 @@ class ResponseIterator implements \Iterator, \ArrayAccess, \Countable {
: $count - $position;
// Convert array to simple items
$item = array_slice($raw,$position,$length);
// Save as result
$result[] = $item;
$result[] = array_slice($raw, $position, $length);
}
} else {
@ -139,8 +139,7 @@ class ResponseIterator implements \Iterator, \ArrayAccess, \Countable {
}
return $result;
}
private function pregResponse(string $value, &$matches)
{
private function pregResponse(string $value, &$matches) {
preg_match_all('/^[=|\.](.*)=(.*)/', $value, $matches);
}
}
Loading…
Cancel
Save