Browse Source

queries writing simplification

tags/0.8
Paul Rock 7 years ago
parent
commit
f03f2e28d4
  1. 32
      src/Client.php
  2. 12
      src/Interfaces/ClientInterface.php
  3. 14
      src/Query.php

32
src/Client.php

@ -4,6 +4,7 @@ namespace RouterOS;
use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConfigException; use RouterOS\Exceptions\ConfigException;
use RouterOS\Exceptions\QueryException;
use RouterOS\Helpers\ArrayHelper; use RouterOS\Helpers\ArrayHelper;
/** /**
@ -166,12 +167,10 @@ class Client implements Interfaces\ClientInterface
if ($byte & 128) { if ($byte & 128) {
if (($byte & 192) === 128) { if (($byte & 192) === 128) {
$length = (($byte & 63) << 8) + \ord(fread($this->_socket, 1)); $length = (($byte & 63) << 8) + \ord(fread($this->_socket, 1));
} else {
if (($byte & 224) === 192) {
} elseif (($byte & 224) === 192) {
$length = (($byte & 31) << 8) + \ord(fread($this->_socket, 1)); $length = (($byte & 31) << 8) + \ord(fread($this->_socket, 1));
$length = ($length << 8) + \ord(fread($this->_socket, 1)); $length = ($length << 8) + \ord(fread($this->_socket, 1));
} else {
if (($byte & 240) === 224) {
} elseif (($byte & 240) === 224) {
$length = (($byte & 15) << 8) + \ord(fread($this->_socket, 1)); $length = (($byte & 15) << 8) + \ord(fread($this->_socket, 1));
$length = ($length << 8) + \ord(fread($this->_socket, 1)); $length = ($length << 8) + \ord(fread($this->_socket, 1));
$length = ($length << 8) + \ord(fread($this->_socket, 1)); $length = ($length << 8) + \ord(fread($this->_socket, 1));
@ -181,8 +180,6 @@ class Client implements Interfaces\ClientInterface
$length = ($length << 8) + \ord(fread($this->_socket, 1)); $length = ($length << 8) + \ord(fread($this->_socket, 1));
$length = ($length << 8) + \ord(fread($this->_socket, 1)); $length = ($length << 8) + \ord(fread($this->_socket, 1));
} }
}
}
} else { } else {
$length = $byte; $length = $byte;
} }
@ -192,13 +189,24 @@ class Client implements Interfaces\ClientInterface
/** /**
* Send write query to RouterOS (with or without tag) * Send write query to RouterOS (with or without tag)
* *
* @param \RouterOS\Query $query
* @param string|array|\RouterOS\Query $query
* @return \RouterOS\Client * @return \RouterOS\Client
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
public function write(Query $query): Client
public function write($query): Client
{ {
if (\is_string($query)) {
$query = new Query($query);
} elseif (\is_array($query)) {
$endpoint = array_shift($query);
$query = new Query($endpoint, $query);
}
if (!$query instanceof Query) {
throw new QueryException('Parameters cannot be processed');
}
// Send commands via loop to router // Send commands via loop to router
foreach ($query->getQuery() as $command) { foreach ($query->getQuery() as $command) {
$command = trim($command); $command = trim($command);
@ -257,12 +265,12 @@ class Client implements Interfaces\ClientInterface
/** /**
* Alias for ->write() method * Alias for ->write() method
* *
* @param \RouterOS\Query $query
* @param string|array|\RouterOS\Query $query
* @return \RouterOS\Client * @return \RouterOS\Client
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
public function w(Query $query): Client
public function w($query): Client
{ {
return $this->write($query); return $this->write($query);
} }
@ -282,14 +290,14 @@ class Client implements Interfaces\ClientInterface
/** /**
* Alias for ->write()->read() combination of methods * Alias for ->write()->read() combination of methods
* *
* @param \RouterOS\Query $query
* @param string|array|\RouterOS\Query $query
* @param bool $parse * @param bool $parse
* @return array * @return array
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
* @since 0.6 * @since 0.6
*/ */
public function wr(Query $query, bool $parse = true): array
public function wr($query, bool $parse = true): array
{ {
return $this->write($query)->read($parse); return $this->write($query)->read($parse);
} }

12
src/Interfaces/ClientInterface.php

@ -66,10 +66,10 @@ interface ClientInterface
/** /**
* Send write query to RouterOS (with or without tag) * Send write query to RouterOS (with or without tag)
* *
* @param \RouterOS\Query $query
* @param string|array|\RouterOS\Query $query
* @return \RouterOS\Client * @return \RouterOS\Client
*/ */
public function write(Query $query): Client;
public function write($query): Client;
/** /**
* Alias for ->read() method * Alias for ->read() method
@ -83,18 +83,18 @@ interface ClientInterface
/** /**
* Alias for ->write() method * Alias for ->write() method
* *
* @param \RouterOS\Query $query
* @param string|array|\RouterOS\Query $query
* @return \RouterOS\Client * @return \RouterOS\Client
*/ */
public function w(Query $query): Client;
public function w($query): Client;
/** /**
* Alias for ->write()->read() combination of methods * Alias for ->write()->read() combination of methods
* *
* @param \RouterOS\Query $query
* @param string|array|\RouterOS\Query $query
* @param bool $parse * @param bool $parse
* @return array * @return array
* @since 0.6 * @since 0.6
*/ */
public function wr(Query $query, bool $parse = true): array;
public function wr($query, bool $parse = true): array;
} }

14
src/Query.php

@ -30,14 +30,22 @@ class Query implements QueryInterface
/** /**
* Query constructor. * Query constructor.
* *
* @param string $endpoint Path of endpoint
* @param array|string $endpoint Path of endpoint
* @param array $attributes List of attributes which should be set * @param array $attributes List of attributes which should be set
* @throws QueryException
*/ */
public function __construct(string $endpoint = null, array $attributes = [])
public function __construct($endpoint, array $attributes = [])
{ {
// TODO: Endpoint may be array, first line will be endpoint, any other attributes
if (\is_string($endpoint)) {
$this->setEndpoint($endpoint); $this->setEndpoint($endpoint);
$this->setAttributes($attributes); $this->setAttributes($attributes);
} elseif (\is_array($endpoint)) {
$query = array_shift($endpoint);
$this->setEndpoint($query);
$this->setAttributes($endpoint);
} else {
throw new QueryException('Specified endpoint is not correct');
}
} }
/** /**

Loading…
Cancel
Save