Add reduce function to QueryBuilder
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
6014384afc
commit
bdc832aaab
3 changed files with 79 additions and 0 deletions
49
src/Function/Reduce.php
Normal file
49
src/Function/Reduce.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
class Reduce extends Base {
|
||||
|
||||
/**
|
||||
* @var array $settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* @var array $identity
|
||||
*/
|
||||
private $identity;
|
||||
|
||||
public function __construct(array $settings, array $identity)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->identity = $identity;
|
||||
}
|
||||
|
||||
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)) . '}) ';
|
||||
}
|
||||
|
||||
protected function formatSettings(array $settings)
|
||||
{
|
||||
array_walk($settings, function(&$value, $key) {
|
||||
$value = $key . ': ' . $value;
|
||||
});
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
protected function format(array $settings)
|
||||
{
|
||||
array_walk($settings, function(&$value, $key) {
|
||||
$value = $key . ': ' . (is_string($value) ? '"' . $value . '"' : $value);
|
||||
});
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue