From 3d9b168dc67b2bb845acf1bd2318240fb2d4ac6b Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Tue, 23 Jul 2019 23:51:08 +0300 Subject: [PATCH] methods where(), tag(), operations() added to Query class --- src/Interfaces/QueryInterface.php | 52 ++++++++++++++----- src/Query.php | 106 ++++++++++++++++++++++++++++++++++---- 2 files changed, 135 insertions(+), 23 deletions(-) diff --git a/src/Interfaces/QueryInterface.php b/src/Interfaces/QueryInterface.php index 7da279f..e0c5c03 100644 --- a/src/Interfaces/QueryInterface.php +++ b/src/Interfaces/QueryInterface.php @@ -13,50 +13,78 @@ use RouterOS\Query; interface QueryInterface { /** + * Where logic of query + * + * @param string $key Key which need to find + * @param bool $value Value which need to check (by default true) + * @param string|null $operator It may be one from list [-,=,>,<] + * @return \RouterOS\Query + * @throws \RouterOS\Exceptions\ClientException + * @since 1.0.0 + */ + public function where(string $key, $value = true, string $operator = ''); + + /** + * Append additional operations + * + * @param string $operations + * @since 1.0.0 + */ + public function operations(string $operations); + + /** + * Append tag to query (it should be at end) + * + * @param string $name + * @since 1.0.0 + */ + public function tag(string $name); + + /** * Append to array yet another attribute of query * - * @param string $word - * @return Query + * @param string $word + * @return \RouterOS\Query */ public function add(string $word): Query; /** * Get attributes array of current query * - * @return array + * @return array */ public function getAttributes(): array; /** * Set array of attributes * - * @param array $attributes - * @since 0.7 - * @return \RouterOS\Query + * @param array $attributes + * @return \RouterOS\Query + * @since 0.7 */ public function setAttributes(array $attributes): Query; /** * Get endpoint of current query * - * @return string|null + * @return string|null */ public function getEndpoint(); /** * Set endpoint of query * - * @param string $endpoint - * @since 0.7 - * @return \RouterOS\Query + * @param string $endpoint + * @return \RouterOS\Query + * @since 0.7 */ public function setEndpoint(string $endpoint): Query; /** * Build body of query * - * @return array - * @throws \RouterOS\Exceptions\QueryException + * @return array + * @throws \RouterOS\Exceptions\QueryException */ public function getQuery(): array; } diff --git a/src/Query.php b/src/Query.php index f48fcd8..0f1aa05 100644 --- a/src/Query.php +++ b/src/Query.php @@ -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 @@ -30,9 +45,9 @@ class Query implements QueryInterface /** * Query constructor. * - * @param array|string $endpoint Path of endpoint - * @param array $attributes List of attributes which should be set - * @throws QueryException + * @param array|string $endpoint Path of endpoint + * @param array $attributes List of attributes which should be set + * @throws QueryException */ public function __construct($endpoint, array $attributes = []) { @@ -48,10 +63,69 @@ 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 * - * @param string $word + * @param string $word * @return \RouterOS\Query */ public function add(string $word): Query @@ -73,9 +147,9 @@ class Query implements QueryInterface /** * Set array of attributes * - * @param array $attributes - * @since 0.7 + * @param array $attributes * @return \RouterOS\Query + * @since 0.7 */ public function setAttributes(array $attributes): Query { @@ -96,9 +170,9 @@ class Query implements QueryInterface /** * Set endpoint of query * - * @param string|null $endpoint - * @since 0.7 + * @param string|null $endpoint * @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; }