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;
}
}