Improve FnType to add a body or block

Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
David Arendsen 2023-03-08 21:20:20 +01:00
commit ec73c70706
4 changed files with 56 additions and 26 deletions

View file

@ -4,6 +4,7 @@ namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\ArrayType;
use Arendsen\FluxQueryBuilder\Type\CustomType;
use Arendsen\FluxQueryBuilder\Type\DurationType;
use Arendsen\FluxQueryBuilder\Type\FnType;
@ -37,7 +38,7 @@ class AggregateWindow extends Base
'every' => new DurationType($this->every),
'period' => isset($this->options['period']) ? new DurationType($this->options['period']) : null,
'offset' => isset($this->options['offset']) ? new DurationType($this->options['offset']) : null,
'fn' => new FnType($this->fn),
'fn' => new CustomType($this->fn),
'location' => isset($this->options['location']) ? new Type($this->options['location']) : null,
'column' => isset($this->options['column']) ? new Type($this->options['column']) : null,
'timeSrc' => isset($this->options['timeSrc']) ? new Type($this->options['timeSrc']) : null,

View file

@ -2,30 +2,50 @@
namespace Arendsen\FluxQueryBuilder\Type;
use Arendsen\FluxQueryBuilder\Type;
class FnType implements TypeInterface
{
/**
* @var mixed $value
* @var array $params
*/
protected $value;
protected $params;
/**
* @var string $content
*/
protected $content;
public function __construct($value, string $content = '')
private function __construct(array $params)
{
$this->params = $params;
}
public static function params(array $params)
{
return new self($params);
}
public function withBody(string $content)
{
$this->value = $value;
$this->content = $content;
return $this;
}
public function withBlock(string $content)
{
$this->content = '{ ' . $content . ' }';
return $this;
}
public function __toString(): string
{
if (is_string($this->value)) {
return $this->value;
}
array_walk($this->params, function (&$value, $key) {
if (is_string($key)) {
$value = $key . ' = ' . new Type($value);
}
});
return '(' . implode(', ', $this->value) . ') => ' . $this->content;
return '(' . implode(', ', $this->params) . ') => ' . $this->content;
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Type\FnType;
use PHPUnit\Framework\TestCase;
final class AggregateWindowFunctionTest extends TestCase
@ -20,18 +21,22 @@ final class AggregateWindowFunctionTest extends TestCase
public function testAllParameters()
{
$expression = new AggregateWindow('20s', 'mean', [
'period' => 'every',
'offset' => '0s',
'location' => 'location',
'column' => '_value',
'timeSrc' => '_stop',
'timeDst' => '_time',
'createEmpty' => false
]);
$expression = new AggregateWindow(
'20s',
FnType::params(['r'])->withBody('r._field == "test"'),
[
'period' => 'every',
'offset' => '0s',
'location' => 'location',
'column' => '_value',
'timeSrc' => '_stop',
'timeDst' => '_time',
'createEmpty' => false
]
);
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: mean, location: "location", ' .
'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: (r) => r._field == "test",
location: "location", ' . 'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
$this->assertEquals($query, $expression->__toString());
}

View file

@ -81,12 +81,16 @@ final class FactoryTypeTest extends TestCase
'this can be anything'
],
'FnType with only content' => [
new FnType('(r) => ({ r with _value: r._value * r._value })'),
'(r) => ({ r with _value: r._value * r._value })'
FnType::params([])->withBlock('r with _value: r._value * r._value'),
'() => { r with _value: r._value * r._value }'
],
'FnType with params and content' => [
new FnType(['r', 'a'], '({ r with _value: r._value * r._value })'),
'(r, a) => ({ r with _value: r._value * r._value })'
'FnType with params and a body' => [
FnType::params(['r'])->withBody('r with _value: r._value * r._value'),
'(r) => r with _value: r._value * r._value'
],
'FnType with params and a block' => [
FnType::params(['r', 'a'])->withBlock('return r with _value: r._value * r._value'),
'(r, a) => { return r with _value: r._value * r._value }'
],
];
}