Add more helper methods to KeyValue expression

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-08-10 13:30:16 +00:00
commit 1922f33c5c
9 changed files with 146 additions and 45 deletions

View file

@ -42,11 +42,46 @@ class KeyValue extends Base {
return new self($key, $operator, $value);
}
public static function setEquals(string $key, string $value): KeyValue
public static function setEqualTo(string $key, string $value): KeyValue
{
return self::set($key, self::EQUAL_TO, $value);
}
public static function setNotEqualTo(string $key, string $value): KeyValue
{
return self::set($key, self::NOT_EQUAL_TO, $value);
}
public static function setGreaterThan(string $key, string $value): KeyValue
{
return self::set($key, self::GREATER_THAN, $value);
}
public static function setGreaterOrEqualTo(string $key, string $value): KeyValue
{
return self::set($key, self::GREATER_EQUAL_TO, $value);
}
public static function setLessThan(string $key, string $value): KeyValue
{
return self::set($key, self::LESS_THAN, $value);
}
public static function setLessOrEqualTo(string $key, string $value): KeyValue
{
return self::set($key, self::LESS_EQUAL_TO, $value);
}
public static function setEqualToRegex(string $key, string $value): KeyValue
{
return self::set($key, self::EQUAL_TO_REGEX, $value);
}
public static function setNotEqualToRegex(string $key, string $value): KeyValue
{
return self::set($key, self::NOT_EQUAL_TO_REGEX, $value);
}
public function and(string $key, string $operator, string $value): KeyValue
{
$this->checkOperator($operator);
@ -54,9 +89,51 @@ class KeyValue extends Base {
return $this;
}
public function andEquals(string $key, string $value): KeyValue
public function andEqualTo(string $key, string $value): KeyValue
{
$this->and($key, '==', $value);
$this->and($key, self::EQUAL_TO, $value);
return $this;
}
public function andNotEqualTo(string $key, string $value): KeyValue
{
$this->and($key, self::NOT_EQUAL_TO, $value);
return $this;
}
public function andGreaterThan(string $key, string $value): KeyValue
{
$this->and($key, self::GREATER_THAN, $value);
return $this;
}
public function andGreaterOrEqualTo(string $key, string $value): KeyValue
{
$this->and($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function andLessThan(string $key, string $value): KeyValue
{
$this->and($key, self::LESS_THAN, $value);
return $this;
}
public function andLessOrEqualTo(string $key, string $value): KeyValue
{
$this->and($key, self::LESS_EQUAL_TO, $value);
return $this;
}
public function andEqualToRegex(string $key, string $value): KeyValue
{
$this->and($key, self::EQUAL_TO_REGEX, $value);
return $this;
}
public function andNotEqualToRegex(string $key, string $value): KeyValue
{
$this->and($key, self::NOT_EQUAL_TO_REGEX, $value);
return $this;
}
@ -67,9 +144,51 @@ class KeyValue extends Base {
return $this;
}
public function orEquals(string $key, string $value): KeyValue
public function orEqualTo(string $key, string $value): KeyValue
{
$this->or($key, '==', $value);
$this->or($key, self::EQUAL_TO, $value);
return $this;
}
public function orNotEqualTo(string $key, string $value): KeyValue
{
$this->or($key, self::NOT_EQUAL_TO, $value);
return $this;
}
public function orGreaterThan(string $key, string $value): KeyValue
{
$this->or($key, self::GREATER_THAN, $value);
return $this;
}
public function orGreaterOrEqualTo(string $key, string $value): KeyValue
{
$this->or($key, self::GREATER_EQUAL_TO, $value);
return $this;
}
public function orLessThan(string $key, string $value): KeyValue
{
$this->or($key, self::LESS_THAN, $value);
return $this;
}
public function orLessOrEqualTo(string $key, string $value): KeyValue
{
$this->or($key, self::LESS_EQUAL_TO, $value);
return $this;
}
public function orEqualToRegex(string $key, string $value): KeyValue
{
$this->or($key, self::EQUAL_TO_REGEX, $value);
return $this;
}
public function orNotEqualToRegex(string $key, string $value): KeyValue
{
$this->or($key, self::NOT_EQUAL_TO_REGEX, $value);
return $this;
}

View file

@ -12,4 +12,13 @@ abstract class Base {
throw new FunctionNotImplementedException('__toString', get_class($this));
}
protected function format(array $settings)
{
array_walk($settings, function(&$value, $key) {
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
});
return $settings;
}
}

View file

@ -19,13 +19,4 @@ class From extends Base {
return 'from(' . implode(', ', $this->format($this->settings)) . ') ';
}
protected function format(array $settings)
{
array_walk($settings, function(&$value, $key) {
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
});
return $settings;
}
}

View file

@ -19,13 +19,4 @@ class Range extends Base {
return '|> range(' . implode(', ', $this->format($this->settings)) . ') ';
}
protected function format(array $settings)
{
array_walk($settings, function(&$value, $key) {
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
});
return $settings;
}
}

View file

@ -37,13 +37,4 @@ class Reduce extends Base {
return $settings;
}
protected function format(array $settings)
{
array_walk($settings, function(&$value, $key) {
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
});
return $settings;
}
}

View file

@ -64,7 +64,7 @@ class QueryBuilder {
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
$this->addToQueryArray(
self::FLUX_PART_FILTERS,
new Filter(KeyValue::setEquals('_measurement', $measurement))
new Filter(KeyValue::setEqualTo('_measurement', $measurement))
);
return $this;
}