Add Formatters

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-08-11 12:42:43 +00:00
commit 328094d803
8 changed files with 61 additions and 31 deletions

View file

@ -2,6 +2,7 @@
namespace Arendsen\FluxQueryBuilder\Expression; namespace Arendsen\FluxQueryBuilder\Expression;
use Arendsen\FluxQueryBuilder\Formatters;
use Exception; use Exception;
class KeyValue extends Base { class KeyValue extends Base {
@ -34,8 +35,7 @@ class KeyValue extends Base {
private function __construct(string $key, string $operator, $value) private function __construct(string $key, string $operator, $value)
{ {
$this->checkOperator($operator); $this->checkOperator($operator);
$value = is_string($value) ? '"' . $value . '"' : $value; $this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . Formatters::valueToString($value);
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . $value;
} }
public static function set(string $key, string $operator, $value): KeyValue public static function set(string $key, string $operator, $value): KeyValue
@ -86,8 +86,7 @@ class KeyValue extends Base {
public function and(string $key, string $operator, $value): KeyValue public function and(string $key, string $operator, $value): KeyValue
{ {
$this->checkOperator($operator); $this->checkOperator($operator);
$value = is_string($value) ? '"' . $value . '"' : $value; $this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . Formatters::valueToString($value);
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . $value;
return $this; return $this;
} }
@ -142,8 +141,7 @@ class KeyValue extends Base {
public function or(string $key, string $operator, $value): KeyValue public function or(string $key, string $operator, $value): KeyValue
{ {
$this->checkOperator($operator); $this->checkOperator($operator);
$value = is_string($value) ? '"' . $value . '"' : $value; $this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . Formatters::valueToString($value);
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . $value;
return $this; return $this;
} }

39
src/Formatters.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace Arendsen\FluxQueryBuilder;
class Formatters {
public static function valueToString($value): string
{
if(is_string($value))
{
return '"' . $value . '"';
}
elseif(is_bool($value))
{
return $value ? 'true' : 'false';
}
return $value;
}
public static function toFluxArrayString(array $array): string
{
$array = array_map(function($column) {
return self::valueToString($column);
}, $array);
return implode(', ', $array);
}
public static function toFluxAssociativeArrayString(array $array): string
{
array_walk($array, function(&$value, $key) {
$value = $key . ': ' . self::valueToString($value);
});
return implode(', ', $array);
}
}

View file

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function; namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
abstract class Base { abstract class Base {
/** /**
@ -12,13 +14,4 @@ abstract class Base {
throw new FunctionNotImplementedException('__toString', get_class($this)); 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

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function; namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
class From extends Base { class From extends Base {
/** /**
@ -16,7 +18,7 @@ class From extends Base {
public function __toString() public function __toString()
{ {
return 'from(' . implode(', ', $this->format($this->settings)) . ') '; return 'from(' . Formatters::toFluxAssociativeArrayString($this->settings) . ') ';
} }
} }

View file

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function; namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
class Group extends Base { class Group extends Base {
/** /**
@ -22,11 +24,7 @@ class Group extends Base {
public function __toString() public function __toString()
{ {
$columns = array_map(function($column) { return '|> group(columns: [' . Formatters::toFluxArrayString($this->columns) . '], mode: "' . $this->mode . '") ';
return '"' . $column . '"';
}, $this->columns);
return '|> group(columns: [' . implode(', ', $columns) . '], mode: "' . $this->mode . '") ';
} }
} }

View file

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function; namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
class Range extends Base { class Range extends Base {
/** /**
@ -16,7 +18,7 @@ class Range extends Base {
public function __toString() public function __toString()
{ {
return '|> range(' . implode(', ', $this->format($this->settings)) . ') '; return '|> range(' . Formatters::toFluxAssociativeArrayString($this->settings) . ') ';
} }
} }

View file

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function; namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
class Reduce extends Base { class Reduce extends Base {
/** /**
@ -22,10 +24,8 @@ class Reduce extends Base {
public function __toString() public function __toString()
{ {
//reduce(fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0})
return '|> reduce(fn: (r, accumulator) => ({' . implode(', ', $this->formatSettings($this->settings)) . '}), ' . return '|> reduce(fn: (r, accumulator) => ({' . implode(', ', $this->formatSettings($this->settings)) . '}), ' .
'identity: {' . implode(', ', $this->format($this->identity)) . '}) '; 'identity: {' . Formatters::toFluxAssociativeArrayString($this->identity) . '}) ';
} }
protected function formatSettings(array $settings) protected function formatSettings(array $settings)

View file

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function; namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
class Sort extends Base { class Sort extends Base {
/** /**
@ -22,12 +24,8 @@ class Sort extends Base {
public function __toString() public function __toString()
{ {
$columns = array_map(function($column) { return '|> sort(columns: [' . Formatters::toFluxArrayString($this->columns) .
return '"' . $column . '"'; '], desc: ' . Formatters::valueToString($this->desc) . ') ';
}, $this->columns);
$desc = $this->desc ? 'true' : 'false';
return '|> sort(columns: [' . implode(', ', $columns) . '], desc: ' . $desc . ') ';
} }
} }