Browse Source

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

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

BIN
src/.DS_Store

38
src/Client.php

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

25
src/Iterators/ResponseIterator.php

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