Improve FnType to add a body or block
Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
parent
9e2bde4217
commit
ec73c70706
4 changed files with 56 additions and 26 deletions
|
|
@ -4,6 +4,7 @@ namespace Arendsen\FluxQueryBuilder\Functions;
|
||||||
|
|
||||||
use Arendsen\FluxQueryBuilder\Type;
|
use Arendsen\FluxQueryBuilder\Type;
|
||||||
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
||||||
|
use Arendsen\FluxQueryBuilder\Type\CustomType;
|
||||||
use Arendsen\FluxQueryBuilder\Type\DurationType;
|
use Arendsen\FluxQueryBuilder\Type\DurationType;
|
||||||
use Arendsen\FluxQueryBuilder\Type\FnType;
|
use Arendsen\FluxQueryBuilder\Type\FnType;
|
||||||
|
|
||||||
|
|
@ -37,7 +38,7 @@ class AggregateWindow extends Base
|
||||||
'every' => new DurationType($this->every),
|
'every' => new DurationType($this->every),
|
||||||
'period' => isset($this->options['period']) ? new DurationType($this->options['period']) : null,
|
'period' => isset($this->options['period']) ? new DurationType($this->options['period']) : null,
|
||||||
'offset' => isset($this->options['offset']) ? new DurationType($this->options['offset']) : 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,
|
'location' => isset($this->options['location']) ? new Type($this->options['location']) : null,
|
||||||
'column' => isset($this->options['column']) ? new Type($this->options['column']) : null,
|
'column' => isset($this->options['column']) ? new Type($this->options['column']) : null,
|
||||||
'timeSrc' => isset($this->options['timeSrc']) ? new Type($this->options['timeSrc']) : null,
|
'timeSrc' => isset($this->options['timeSrc']) ? new Type($this->options['timeSrc']) : null,
|
||||||
|
|
|
||||||
|
|
@ -2,30 +2,50 @@
|
||||||
|
|
||||||
namespace Arendsen\FluxQueryBuilder\Type;
|
namespace Arendsen\FluxQueryBuilder\Type;
|
||||||
|
|
||||||
|
use Arendsen\FluxQueryBuilder\Type;
|
||||||
|
|
||||||
class FnType implements TypeInterface
|
class FnType implements TypeInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var mixed $value
|
* @var array $params
|
||||||
*/
|
*/
|
||||||
protected $value;
|
protected $params;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $content
|
* @var string $content
|
||||||
*/
|
*/
|
||||||
protected $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;
|
$this->content = $content;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withBlock(string $content)
|
||||||
|
{
|
||||||
|
$this->content = '{ ' . $content . ' }';
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
if (is_string($this->value)) {
|
array_walk($this->params, function (&$value, $key) {
|
||||||
return $this->value;
|
if (is_string($key)) {
|
||||||
}
|
$value = $key . ' = ' . new Type($value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return '(' . implode(', ', $this->value) . ') => ' . $this->content;
|
return '(' . implode(', ', $this->params) . ') => ' . $this->content;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Tests\Functions;
|
namespace Tests\Functions;
|
||||||
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
||||||
|
use Arendsen\FluxQueryBuilder\Type\FnType;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
final class AggregateWindowFunctionTest extends TestCase
|
final class AggregateWindowFunctionTest extends TestCase
|
||||||
|
|
@ -20,18 +21,22 @@ final class AggregateWindowFunctionTest extends TestCase
|
||||||
|
|
||||||
public function testAllParameters()
|
public function testAllParameters()
|
||||||
{
|
{
|
||||||
$expression = new AggregateWindow('20s', 'mean', [
|
$expression = new AggregateWindow(
|
||||||
'period' => 'every',
|
'20s',
|
||||||
'offset' => '0s',
|
FnType::params(['r'])->withBody('r._field == "test"'),
|
||||||
'location' => 'location',
|
[
|
||||||
'column' => '_value',
|
'period' => 'every',
|
||||||
'timeSrc' => '_stop',
|
'offset' => '0s',
|
||||||
'timeDst' => '_time',
|
'location' => 'location',
|
||||||
'createEmpty' => false
|
'column' => '_value',
|
||||||
]);
|
'timeSrc' => '_stop',
|
||||||
|
'timeDst' => '_time',
|
||||||
|
'createEmpty' => false
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: mean, location: "location", ' .
|
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: (r) => r._field == "test",
|
||||||
'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
|
location: "location", ' . 'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';
|
||||||
|
|
||||||
$this->assertEquals($query, $expression->__toString());
|
$this->assertEquals($query, $expression->__toString());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,12 +81,16 @@ final class FactoryTypeTest extends TestCase
|
||||||
'this can be anything'
|
'this can be anything'
|
||||||
],
|
],
|
||||||
'FnType with only content' => [
|
'FnType with only content' => [
|
||||||
new FnType('(r) => ({ r with _value: r._value * r._value })'),
|
FnType::params([])->withBlock('r with _value: r._value * r._value'),
|
||||||
'(r) => ({ r with _value: r._value * r._value })'
|
'() => { r with _value: r._value * r._value }'
|
||||||
],
|
],
|
||||||
'FnType with params and content' => [
|
'FnType with params and a body' => [
|
||||||
new FnType(['r', 'a'], '({ r with _value: r._value * r._value })'),
|
FnType::params(['r'])->withBody('r with _value: r._value * r._value'),
|
||||||
'(r, a) => ({ 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 }'
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue