Browse Source

methods where(), tag(), operations() added to Query class

tags/1.0
Paul Rock 6 years ago
parent
commit
3d9b168dc6
  1. 52
      src/Interfaces/QueryInterface.php
  2. 106
      src/Query.php

52
src/Interfaces/QueryInterface.php

@ -13,50 +13,78 @@ use RouterOS\Query;
interface QueryInterface 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 * 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; public function add(string $word): Query;
/** /**
* Get attributes array of current query * Get attributes array of current query
* *
* @return array
* @return array
*/ */
public function getAttributes(): array; public function getAttributes(): array;
/** /**
* Set array of attributes * 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; public function setAttributes(array $attributes): Query;
/** /**
* Get endpoint of current query * Get endpoint of current query
* *
* @return string|null
* @return string|null
*/ */
public function getEndpoint(); public function getEndpoint();
/** /**
* Set endpoint of query * 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; public function setEndpoint(string $endpoint): Query;
/** /**
* Build body of query * Build body of query
* *
* @return array
* @throws \RouterOS\Exceptions\QueryException
* @return array
* @throws \RouterOS\Exceptions\QueryException
*/ */
public function getQuery(): array; public function getQuery(): array;
} }

106
src/Query.php

@ -2,6 +2,7 @@
namespace RouterOS; namespace RouterOS;
use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\QueryException; use RouterOS\Exceptions\QueryException;
use RouterOS\Interfaces\QueryInterface; use RouterOS\Interfaces\QueryInterface;
@ -21,6 +22,20 @@ class Query implements QueryInterface
private $_attributes = []; private $_attributes = [];
/** /**
* Some additional operations
*
* @var string
*/
private $_operations;
/**
* Tag of query
*
* @var string
*/
private $_tag;
/**
* Endpoint of query * Endpoint of query
* *
* @var string * @var string
@ -30,9 +45,9 @@ class Query implements QueryInterface
/** /**
* Query constructor. * 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 = []) 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 * Append to array yet another attribute of query
* *
* @param string $word
* @param string $word
* @return \RouterOS\Query * @return \RouterOS\Query
*/ */
public function add(string $word): Query public function add(string $word): Query
@ -73,9 +147,9 @@ class Query implements QueryInterface
/** /**
* Set array of attributes * Set array of attributes
* *
* @param array $attributes
* @since 0.7
* @param array $attributes
* @return \RouterOS\Query * @return \RouterOS\Query
* @since 0.7
*/ */
public function setAttributes(array $attributes): Query public function setAttributes(array $attributes): Query
{ {
@ -96,9 +170,9 @@ class Query implements QueryInterface
/** /**
* Set endpoint of query * Set endpoint of query
* *
* @param string|null $endpoint
* @since 0.7
* @param string|null $endpoint
* @return \RouterOS\Query * @return \RouterOS\Query
* @since 0.7
*/ */
public function setEndpoint(string $endpoint = null): Query public function setEndpoint(string $endpoint = null): Query
{ {
@ -114,13 +188,23 @@ class Query implements QueryInterface
*/ */
public function getQuery(): array public function getQuery(): array
{ {
if ($this->getEndpoint() === null) {
if ($this->_endpoint === null) {
throw new QueryException('Endpoint of query is not set'); throw new QueryException('Endpoint of query is not set');
} }
$endpoint = $this->getEndpoint();
// Get all attributes and prepend endpoint to this list
$attributes = $this->getAttributes(); $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; return $attributes;
} }

Loading…
Cancel
Save