2022-08-05 18:51:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Arendsen\FluxQueryBuilder\Expression;
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
use Exception;
|
|
|
|
|
|
2022-08-05 18:51:35 +00:00
|
|
|
class KeyValue extends Base {
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
const EQUAL_TO = '==';
|
|
|
|
|
const NOT_EQUAL_TO = '!=';
|
|
|
|
|
const GREATER_THAN = '>';
|
|
|
|
|
const GREATER_EQUAL_TO = '>=';
|
|
|
|
|
const LESS_THAN = '<';
|
|
|
|
|
const LESS_EQUAL_TO = '<=';
|
|
|
|
|
const EQUAL_TO_REGEX = '=~';
|
|
|
|
|
const NOT_EQUAL_TO_REGEX = '!~';
|
|
|
|
|
|
|
|
|
|
const COMPARISON_OPERATORS = [
|
|
|
|
|
self::EQUAL_TO,
|
|
|
|
|
self::NOT_EQUAL_TO,
|
|
|
|
|
self::GREATER_THAN,
|
|
|
|
|
self::GREATER_EQUAL_TO,
|
|
|
|
|
self::LESS_THAN,
|
|
|
|
|
self::LESS_EQUAL_TO,
|
|
|
|
|
self::EQUAL_TO_REGEX,
|
|
|
|
|
self::NOT_EQUAL_TO_REGEX,
|
|
|
|
|
];
|
|
|
|
|
|
2022-08-05 18:51:35 +00:00
|
|
|
/**
|
|
|
|
|
* @var array $expressions
|
|
|
|
|
*/
|
|
|
|
|
private $expressions;
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
private function __construct(string $key, string $operator, string $value)
|
2022-08-05 18:51:35 +00:00
|
|
|
{
|
2022-08-09 15:41:38 +00:00
|
|
|
$this->checkOperator($operator);
|
|
|
|
|
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
2022-08-05 18:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
public static function set(string $key, string $operator, string $value): KeyValue
|
2022-08-05 18:51:35 +00:00
|
|
|
{
|
2022-08-09 15:41:38 +00:00
|
|
|
return new self($key, $operator, $value);
|
2022-08-05 18:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
public static function setEquals(string $key, string $value): KeyValue
|
2022-08-05 18:51:35 +00:00
|
|
|
{
|
2022-08-09 15:41:38 +00:00
|
|
|
return self::set($key, self::EQUAL_TO, $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function and(string $key, string $operator, string $value): KeyValue
|
|
|
|
|
{
|
|
|
|
|
$this->checkOperator($operator);
|
|
|
|
|
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
2022-08-05 18:51:35 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
public function andEquals(string $key, string $value): KeyValue
|
2022-08-05 18:51:35 +00:00
|
|
|
{
|
2022-08-09 15:41:38 +00:00
|
|
|
$this->and($key, '==', $value);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function or(string $key, string $operator, string $value): KeyValue
|
|
|
|
|
{
|
|
|
|
|
$this->checkOperator($operator);
|
|
|
|
|
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' "' . $value . '"';
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function orEquals(string $key, string $value): KeyValue
|
|
|
|
|
{
|
|
|
|
|
$this->or($key, '==', $value);
|
2022-08-05 18:51:35 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
|
{
|
|
|
|
|
return implode(' ', $this->expressions);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 15:41:38 +00:00
|
|
|
protected function checkOperator(string $operator)
|
|
|
|
|
{
|
|
|
|
|
if(!in_array($operator, self::COMPARISON_OPERATORS))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception('Operator "' . $operator . '" is not supported!');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 18:51:35 +00:00
|
|
|
}
|