From 3676909a637d0b49bb7aef5e2a9b44da59905342 Mon Sep 17 00:00:00 2001 From: DarkSide Date: Sat, 17 Jun 2017 15:04:20 +0300 Subject: [PATCH 1/2] * add min, max, count aggregate methods for Table totals * add Closure support for Table totals --- src/Table.php | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Table.php b/src/Table.php index 3375d22c51..1e15f10a45 100644 --- a/src/Table.php +++ b/src/Table.php @@ -396,13 +396,43 @@ public function jsRow() public function updateTotals() { foreach ($this->totals_plan as $key=>$val) { + + // if value is array, then we treat it as built-in or callable aggregate method if (is_array($val)) { - switch ($val[0]) { - case 'sum': - if (!isset($this->totals[$key])) { - $this->totals[$key] = 0; + $f = $val[0]; // shortcut + + // initial value is always 0 + if (!isset($this->totals[$key])) { + $this->totals[$key] = 0; + } + + // closure support + // arguments - current value, key, \atk4\ui\Table object + if ($f instanceof \Closure) { + $this->totals[$key] += ($f($this->model[$key], $key, $this) ?: 0); + } + // built-in methods + elseif (is_string($f)) { + switch ($f) { + case 'sum': + $this->totals[$key] += $this->model[$key]; + break; + case 'count': + $this->totals[$key] += 1; + break; + case 'min': + if ($this->model[$key] < $this->totals[$key]) { + $this->totals[$key] = $this->model[$key]; + } + break; + case 'max': + if ($this->model[$key] > $this->totals[$key]) { + $this->totals[$key] = $this->model[$key]; + } + break; + default: + throw new Exception(['Aggregation method does not exist', 'method'=>$f]); } - $this->totals[$key] += $this->model[$key]; } } } From 27123ec40dbad43885decf516288b26ecc499dd4 Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Sat, 17 Jun 2017 12:07:08 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Table.php b/src/Table.php index 1e15f10a45..2beb95930f 100644 --- a/src/Table.php +++ b/src/Table.php @@ -403,7 +403,7 @@ public function updateTotals() // initial value is always 0 if (!isset($this->totals[$key])) { - $this->totals[$key] = 0; + $this->totals[$key] = 0; } // closure support