|
|
|
@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
namespace RouterOS; |
|
|
|
|
|
|
|
use RouterOS\Exceptions\ClientException; |
|
|
|
use RouterOS\Exceptions\QueryException; |
|
|
|
use RouterOS\Interfaces\QueryInterface; |
|
|
|
|
|
|
|
@ -21,6 +22,20 @@ class Query implements QueryInterface |
|
|
|
private $_attributes = []; |
|
|
|
|
|
|
|
/** |
|
|
|
* Some additional operations |
|
|
|
* |
|
|
|
* @var string |
|
|
|
*/ |
|
|
|
private $_operations; |
|
|
|
|
|
|
|
/** |
|
|
|
* Tag of query |
|
|
|
* |
|
|
|
* @var string |
|
|
|
*/ |
|
|
|
private $_tag; |
|
|
|
|
|
|
|
/** |
|
|
|
* Endpoint of query |
|
|
|
* |
|
|
|
* @var string |
|
|
|
@ -48,6 +63,65 @@ class Query implements QueryInterface |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const AVAILABLE_OPERATORS = [ |
|
|
|
'-', // Does not have
|
|
|
|
'=', // Equal
|
|
|
|
'>', // More than
|
|
|
|
'<' // Less than
|
|
|
|
]; |
|
|
|
|
|
|
|
/** |
|
|
|
* Where logic of query |
|
|
|
* |
|
|
|
* @param string $key |
|
|
|
* @param bool $value |
|
|
|
* @param string|null $operator |
|
|
|
* @return \RouterOS\Query |
|
|
|
* @throws \RouterOS\Exceptions\ClientException |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
public function where(string $key, $value = true, string $operator = ''): self |
|
|
|
{ |
|
|
|
if (!empty($operator)) { |
|
|
|
// If operator is available in list
|
|
|
|
if (\in_array($operator, self::AVAILABLE_OPERATORS, true)) { |
|
|
|
// Overwrite key
|
|
|
|
$key = $operator . $key; |
|
|
|
} else { |
|
|
|
throw new ClientException('Operator "' . $operator . '" in not in allowed list [' . implode(',', self::AVAILABLE_OPERATORS) . ']'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$this->add('?' . $key . '=' . $value); |
|
|
|
return $this; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Append additional operations |
|
|
|
* |
|
|
|
* @param string $operations |
|
|
|
* @return \RouterOS\Query |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
public function operations(string $operations): self |
|
|
|
{ |
|
|
|
$this->_operations = '?#' . $operations; |
|
|
|
return $this; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Append tag to query (it should be at end) |
|
|
|
* |
|
|
|
* @param string $name |
|
|
|
* @return \RouterOS\Query |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
public function tag(string $name): self |
|
|
|
{ |
|
|
|
$this->_tag = '.tag=' . $name; |
|
|
|
return $this; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Append to array yet another attribute of query |
|
|
|
* |
|
|
|
@ -74,8 +148,8 @@ class Query implements QueryInterface |
|
|
|
* Set array of attributes |
|
|
|
* |
|
|
|
* @param array $attributes |
|
|
|
* @since 0.7 |
|
|
|
* @return \RouterOS\Query |
|
|
|
* @since 0.7 |
|
|
|
*/ |
|
|
|
public function setAttributes(array $attributes): Query |
|
|
|
{ |
|
|
|
@ -97,8 +171,8 @@ class Query implements QueryInterface |
|
|
|
* Set endpoint of query |
|
|
|
* |
|
|
|
* @param string|null $endpoint |
|
|
|
* @since 0.7 |
|
|
|
* @return \RouterOS\Query |
|
|
|
* @since 0.7 |
|
|
|
*/ |
|
|
|
public function setEndpoint(string $endpoint = null): Query |
|
|
|
{ |
|
|
|
@ -114,13 +188,23 @@ class Query implements QueryInterface |
|
|
|
*/ |
|
|
|
public function getQuery(): array |
|
|
|
{ |
|
|
|
if ($this->getEndpoint() === null) { |
|
|
|
if ($this->_endpoint === null) { |
|
|
|
throw new QueryException('Endpoint of query is not set'); |
|
|
|
} |
|
|
|
|
|
|
|
$endpoint = $this->getEndpoint(); |
|
|
|
// Get all attributes and prepend endpoint to this list
|
|
|
|
$attributes = $this->getAttributes(); |
|
|
|
array_unshift($attributes, $endpoint); |
|
|
|
array_unshift($attributes, $this->_endpoint); |
|
|
|
|
|
|
|
// If operations is set then add to query
|
|
|
|
if (is_string($this->_operations) && !empty($this->_operations)) { |
|
|
|
$attributes[] = $this->_operations; |
|
|
|
} |
|
|
|
|
|
|
|
// If tag is set then added to query
|
|
|
|
if (is_string($this->_tag) && !empty($this->_tag)) { |
|
|
|
$attributes[] = $this->_tag; |
|
|
|
} |
|
|
|
|
|
|
|
return $attributes; |
|
|
|
} |
|
|
|
|