Piwik\DataTable\Row::sumRow PHP Method

sumRow() public method

Only the int or float values will be summed. Label columns will be ignored even if they have a numeric value. Columns in $rowToSum that don't exist in $this are added to $this.
public sumRow ( Row $rowToSum, boolean $enableCopyMetadata = true, array | boolean $aggregationOperations = false )
$rowToSum Row The row to sum to this row.
$enableCopyMetadata boolean Whether metadata should be copied or not.
$aggregationOperations array | boolean for columns that should not be summed, determine which aggregation should be used (min, max). format: `array('column name' => 'function name')`
    public function sumRow(Row $rowToSum, $enableCopyMetadata = true, $aggregationOperations = false)
    {
        foreach ($rowToSum as $columnToSumName => $columnToSumValue) {
            if (!$this->isSummableColumn($columnToSumName)) {
                continue;
            }
            $thisColumnValue = $this->getColumn($columnToSumName);
            $operation = 'sum';
            if (is_array($aggregationOperations) && isset($aggregationOperations[$columnToSumName])) {
                if (is_string($aggregationOperations[$columnToSumName])) {
                    $operation = strtolower($aggregationOperations[$columnToSumName]);
                } elseif (is_callable($aggregationOperations[$columnToSumName])) {
                    $operation = $aggregationOperations[$columnToSumName];
                }
            }
            // max_actions is a core metric that is generated in ArchiveProcess_Day. Therefore, it can be
            // present in any data table and is not part of the $aggregationOperations mechanism.
            if ($columnToSumName == Metrics::INDEX_MAX_ACTIONS) {
                $operation = 'max';
            }
            if (empty($operation)) {
                throw new Exception("Unknown aggregation operation for column {$columnToSumName}.");
            }
            $newValue = $this->getColumnValuesMerged($operation, $thisColumnValue, $columnToSumValue, $this, $rowToSum);
            $this->setColumn($columnToSumName, $newValue);
        }
        if ($enableCopyMetadata) {
            $this->sumRowMetadata($rowToSum, $aggregationOperations);
        }
    }

Usage Example

Exemplo n.º 1
0
 public function test_SumRow_shouldIgnoreCallableValues_AndNotRaiseAnyException()
 {
     $columns = array('nb_visits' => 5, 'label' => 'Test', 'closure' => function () {
         return 7;
     });
     $this->row->setColumns($columns);
     $secondRow = new Row(array(Row::COLUMNS => $columns));
     $this->row->sumRow($secondRow);
     $this->assertEquals(10, $this->row->getColumn('nb_visits'));
     $this->assertEquals(7, $this->row->getColumn('closure'));
 }
All Usage Examples Of Piwik\DataTable\Row::sumRow