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 ( |
||
$rowToSum | 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 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')); }
/** * Adds a row to this table. * * If {@link setMaximumAllowedRows()} was called and the current row count is * at the maximum, the new row will be summed to the summary row. If there is no summary row, * this row is set as the summary row. * * @param Row $row * @return Row `$row` or the summary row if we're at the maximum number of rows. */ public function addRow(Row $row) { // if there is a upper limit on the number of allowed rows and the table is full, // add the new row to the summary row if ($this->maximumAllowedRows > 0 && $this->getRowsCount() >= $this->maximumAllowedRows - 1) { if ($this->summaryRow === null) { // create the summary row if necessary $columns = array('label' => self::LABEL_SUMMARY_ROW) + $row->getColumns(); $this->addSummaryRow(new Row(array(Row::COLUMNS => $columns))); } else { $this->summaryRow->sumRow($row, $enableCopyMetadata = false, $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME)); } return $this->summaryRow; } $this->rows[] = $row; if (!$this->indexNotUpToDate && $this->rebuildIndexContinuously) { $label = $row->getColumn('label'); if ($label !== false) { $this->rowsIndexByLabel[$label] = count($this->rows) - 1; } } return $row; }
/** * Test that adding two string column values results in an exception. */ public function testSumRow_stringException() { $columns = array('super' => array('this column has an array string that will be 0 when algorithm sums the value')); $row1 = new Row(array(Row::COLUMNS => $columns)); $columns2 = array('super' => array('this column has geagaean array value, amazing')); $row2 = new Row(array(Row::COLUMNS => $columns2)); $row2->sumRow($row1); $this->assertTrue($noException = true); }
private function addSummaryRow($table) { $table->filter('Sort', array($this->columnToSortByBeforeTruncating, 'desc')); if ($table->getRowsCount() <= $this->truncateAfter + 1) { return; } $rows = $table->getRows(); $count = $table->getRowsCount(); $newRow = new Row(array(Row::COLUMNS => array('label' => DataTable::LABEL_SUMMARY_ROW))); for ($i = $this->truncateAfter; $i < $count; $i++) { if (!isset($rows[$i])) { // case when the last row is a summary row, it is not indexed by $cout but by DataTable::ID_SUMMARY_ROW $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); //FIXME: I'm not sure why it could return false, but it was reported in: http://forum.piwik.org/read.php?2,89324,page=1#msg-89442 if ($summaryRow) { $newRow->sumRow($summaryRow, $enableCopyMetadata = false, $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME)); } } else { $newRow->sumRow($rows[$i], $enableCopyMetadata = false, $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME)); } } $table->filter('Limit', array(0, $this->truncateAfter)); $table->addSummaryRow($newRow); unset($rows); }
/** * Test that adding two string column values results in an exception. * * @group Core * * @expectedException Exception */ public function testSumRow_stringException() { $columns = array('super' => array('this column has an array string that will be 0 when algorithm sums the value')); $row1 = new Row(array(Row::COLUMNS => $columns)); $columns2 = array('super' => array('this column has geagaean array value, amazing')); $row2 = new Row(array(Row::COLUMNS => $columns2)); $row2->sumRow($row1); $this->fail("sumRow did not throw when adding two string columns."); }