Skip to content

Commit

Permalink
Make the reduce function optional in GroupBy filter to allow grouping…
Browse files Browse the repository at this point in the history
… by column value
  • Loading branch information
mattab committed Oct 8, 2014
1 parent d5c1324 commit e49a81d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions core/DataTable/Filter/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class GroupBy extends BaseFilter
* @param DataTable $table The DataTable to filter.
* @param string $groupByColumn The column name to reduce.
* @param callable $reduceFunction The reduce function. This must alter the `$groupByColumn`
* columng in some way.
* columng in some way. If not set then the filter will group by the raw column value.
* @param array $parameters deprecated - use an [anonymous function](http://php.net/manual/en/functions.anonymous.php)
* instead.
*/
public function __construct($table, $groupByColumn, $reduceFunction, $parameters = array())
public function __construct($table, $groupByColumn, $reduceFunction = null, $parameters = array())
{
parent::__construct($table);

Expand All @@ -80,10 +80,14 @@ public function filter($table)
continue;
}

// reduce the group by column of this row
$groupByColumnValue = $row->getColumn($this->groupByColumn);
$parameters = array_merge(array($groupByColumnValue), $this->parameters);
$groupByValue = call_user_func_array($this->reduceFunction, $parameters);
$groupByValue = $groupByColumnValue;

// reduce the group by column of this row
if($this->reduceFunction) {
$parameters = array_merge(array($groupByColumnValue), $this->parameters);
$groupByValue = call_user_func_array($this->reduceFunction, $parameters);
}

if (!isset($groupByRows[$groupByValue])) {
// if we haven't encountered this group by value before, we mark this row as a
Expand Down

0 comments on commit e49a81d

Please sign in to comment.