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,8 @@
namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
abstract class Base {
/**
@ -12,13 +14,4 @@ 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

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

View file

@ -2,6 +2,8 @@
namespace Arendsen\FluxQueryBuilder\Function;
use Arendsen\FluxQueryBuilder\Formatters;
class Range extends Base {
/**
@ -16,7 +18,7 @@ class Range extends Base {
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;
use Arendsen\FluxQueryBuilder\Formatters;
class Reduce extends Base {
/**
@ -22,10 +24,8 @@ class Reduce extends Base {
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)) . '}), ' .
'identity: {' . implode(', ', $this->format($this->identity)) . '}) ';
'identity: {' . Formatters::toFluxAssociativeArrayString($this->identity) . '}) ';
}
protected function formatSettings(array $settings)

View file

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