Movee Formatters to Type factory

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-08-16 15:06:32 +00:00
commit 4abd95f303
12 changed files with 40 additions and 68 deletions

View file

@ -2,7 +2,7 @@
namespace Arendsen\FluxQueryBuilder\Expression; namespace Arendsen\FluxQueryBuilder\Expression;
use Arendsen\FluxQueryBuilder\Formatters; use Arendsen\FluxQueryBuilder\Type;
use Exception; use Exception;
class KeyValue extends Base class KeyValue extends Base
@ -35,7 +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);
$this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . Formatters::valueToString($value); $this->expressions[] = 'r.' . $key . ' ' . $operator . ' ' . new Type($value);
} }
public static function set(string $key, string $operator, $value): KeyValue public static function set(string $key, string $operator, $value): KeyValue
@ -86,7 +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);
$this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . Formatters::valueToString($value); $this->expressions[] = 'and r.' . $key . ' ' . $operator . ' ' . new Type($value);
return $this; return $this;
} }
@ -141,7 +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);
$this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . Formatters::valueToString($value); $this->expressions[] = 'or r.' . $key . ' ' . $operator . ' ' . new Type($value);
return $this; return $this;
} }

View file

@ -10,18 +10,12 @@ class Formatters
{ {
public static function valueToString($value): string public static function valueToString($value): string
{ {
if (is_array($value)) {
return '[' . new ArrayType($value) . ']';
} else {
return new Type($value); return new Type($value);
} }
return $value;
}
public static function toFluxArrayString(array $array): string public static function toFluxArrayString(array $array): string
{ {
return new ArrayType($array); return new Type($array);
} }
public static function dateTimeToString(DateTime $dateTime): string public static function dateTimeToString(DateTime $dateTime): string

View file

@ -2,7 +2,6 @@
namespace Arendsen\FluxQueryBuilder\Functions; namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Formatters;
use Arendsen\FluxQueryBuilder\Exception\FunctionNotImplementedException; use Arendsen\FluxQueryBuilder\Exception\FunctionNotImplementedException;
abstract class Base abstract class Base

View file

@ -2,7 +2,7 @@
namespace Arendsen\FluxQueryBuilder\Functions; namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Formatters; use Arendsen\FluxQueryBuilder\Type;
class From extends Base class From extends Base
{ {
@ -18,6 +18,6 @@ class From extends Base
public function __toString() public function __toString()
{ {
return 'from(' . Formatters::toFluxArrayString($this->settings) . ') '; return 'from(' . new Type($this->settings) . ') ';
} }
} }

View file

@ -2,7 +2,7 @@
namespace Arendsen\FluxQueryBuilder\Functions; namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Formatters; use Arendsen\FluxQueryBuilder\Type;
class Group extends Base class Group extends Base
{ {
@ -24,7 +24,7 @@ class Group extends Base
public function __toString() public function __toString()
{ {
$array = Formatters::toFluxArrayString([ $array = new Type([
'columns' => $this->columns, 'columns' => $this->columns,
'mode' => $this->mode, 'mode' => $this->mode,
]); ]);

View file

@ -3,7 +3,7 @@
namespace Arendsen\FluxQueryBuilder\Functions; namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Exception\FunctionRequiredSettingMissingException; use Arendsen\FluxQueryBuilder\Exception\FunctionRequiredSettingMissingException;
use Arendsen\FluxQueryBuilder\Formatters; use Arendsen\FluxQueryBuilder\Type;
use DateTime; use DateTime;
class Range extends Base class Range extends Base
@ -20,8 +20,8 @@ class Range extends Base
public function __construct(DateTime $start, ?DateTime $stop = null) public function __construct(DateTime $start, ?DateTime $stop = null)
{ {
$this->start = Formatters::dateTimeToString($start); $this->start = new Type($start);
$this->stop = $stop ? Formatters::dateTimeToString($stop) : null; $this->stop = $stop ? new Type($stop) : null;
} }
public function __toString() public function __toString()

View file

@ -2,7 +2,6 @@
namespace Arendsen\FluxQueryBuilder\Functions; namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Formatters;
use Arendsen\FluxQueryBuilder\Type\Record; use Arendsen\FluxQueryBuilder\Type\Record;
class Reduce extends Base class Reduce extends Base
@ -28,5 +27,4 @@ class Reduce extends Base
return '|> reduce(fn: (r, accumulator) => (' . new Record($this->settings) . '), ' . return '|> reduce(fn: (r, accumulator) => (' . new Record($this->settings) . '), ' .
'identity: ' . new Record($this->identity) . ') '; 'identity: ' . new Record($this->identity) . ') ';
} }
} }

View file

@ -2,7 +2,7 @@
namespace Arendsen\FluxQueryBuilder\Functions; namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Formatters; use Arendsen\FluxQueryBuilder\Type;
class Sort extends Base class Sort extends Base
{ {
@ -24,7 +24,7 @@ class Sort extends Base
public function __toString() public function __toString()
{ {
return '|> sort(columns: [' . Formatters::toFluxArrayString($this->columns) . return '|> sort(columns: [' . new Type($this->columns) .
'], desc: ' . Formatters::valueToString($this->desc) . ') '; '], desc: ' . new Type($this->desc) . ') ';
} }
} }

View file

@ -9,9 +9,10 @@ use DateTime;
class Type class Type
{ {
public function __construct($value) public function __construct($value, $settings = [])
{ {
$this->value = $value; $this->value = $value;
$this->settings = $settings;
} }
public function __toString(): string public function __toString(): string
@ -27,7 +28,7 @@ class Type
case 'boolean': case 'boolean':
return new BooleanType($this->value); return new BooleanType($this->value);
case 'array': case 'array':
return new ArrayType($this->value); return new ArrayType($this->value, $this->settings);
default: default:
return (string)$this->value; return (string)$this->value;
} }

View file

@ -2,26 +2,42 @@
namespace Arendsen\FluxQueryBuilder\Type; namespace Arendsen\FluxQueryBuilder\Type;
use Arendsen\FluxQueryBuilder\Formatters;
use Arendsen\FluxQueryBuilder\Type; use Arendsen\FluxQueryBuilder\Type;
class ArrayType implements TypeInterface class ArrayType implements TypeInterface
{ {
public function __construct(array $value) /**
* @var array $value
*/
protected $value;
/**
* @var array $settings
*/
protected $settings;
public function __construct(array $value, $settings = [])
{ {
$this->value = $value; $this->value = $value;
$this->settings = $settings;
} }
public function __toString(): string public function __toString(): string
{ {
$subArray = isset($this->settings['subArray']) && $this->settings['subArray'];
array_walk($this->value, function (&$value, $key) { array_walk($this->value, function (&$value, $key) {
if (is_string($key)) { if (is_string($key)) {
$value = $key . ': ' . Formatters::valueToString($value); $value = $key . ': ' . new Type($value, [
'subArray' => is_array($value)
]);
} else { } else {
$value = Formatters::valueToString($value); $value = new Type($value, [
'subArray' => is_array($value)
]);
} }
}); });
return implode(', ', $this->value); return ($subArray ? '[' : '') . implode(', ', $this->value) . ($subArray ? ']' : '');
} }
} }

View file

@ -2,9 +2,6 @@
namespace Arendsen\FluxQueryBuilder\Type; namespace Arendsen\FluxQueryBuilder\Type;
use Arendsen\FluxQueryBuilder\Formatters;
use Arendsen\FluxQueryBuilder\Type;
class Math implements TypeInterface class Math implements TypeInterface
{ {
public function __construct(string $value) public function __construct(string $value)

View file

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Generic;
use DateTime;
use Arendsen\FluxQueryBuilder\Formatters;
use PHPUnit\Framework\TestCase;
final class FormattersTest extends TestCase
{
public function testAssociativeArrayNested()
{
$array = [
'columns' => ['foo', 'bar'],
'mode' => 'by'
];
$expected = 'columns: ["foo", "bar"], mode: "by"';
$this->assertEquals($expected, Formatters::toFluxArrayString($array));
}
public function testDateTimeToString()
{
$dateTime = new DateTime('2022-08-12 17:31:00');
$expected = 'time(v: 2022-08-12T17:31:00Z)';
$this->assertEquals($expected, Formatters::dateTimeToString($dateTime));
}
}