table = $table; $this->columnValueToRead = $columnValueToRead; $this->columnNameToAdd = $columnNameToAdd; if (is_numeric($divisorValueOrDivisorColumnName)) { $this->totalValueUsedAsDivisor = $divisorValueOrDivisorColumnName; } else { $this->columnNameUsedAsDivisor = $divisorValueOrDivisorColumnName; } $this->quotientPrecision = $quotientPrecision; $this->shouldSkipRows = $shouldSkipRows; $this->getDivisorFromSummaryRow = $getDivisorFromSummaryRow; } /** * Filters the given data table * * @param DataTable $table */ public function filter($table) { foreach ($table->getRows() as $key => $row) { $existingValue = $row->getColumn($this->columnNameToAdd); if ($existingValue !== false) { continue; } $value = $this->getDividend($row); if ($value === false && $this->shouldSkipRows) { continue; } $divisor = $this->getDivisor($row); $formattedValue = $this->formatValue($value, $divisor); $row->addColumn($this->columnNameToAdd, $formattedValue); $this->filterSubTable($row); } } /** * Formats the given value * * @param number $value * @param number $divisor * @return float|int */ protected function formatValue($value, $divisor) { $quotient = 0; if ($divisor > 0 && $value > 0) { $quotient = round($value / $divisor, $this->quotientPrecision); } return $quotient; } /** * Returns the dividend to use when calculating the new column value. Can * be overridden by descendent classes to customize behavior. * * @param Row $row The row being modified. * @return int|float */ protected function getDividend($row) { return $row->getColumn($this->columnValueToRead); } /** * Returns the divisor to use when calculating the new column value. Can * be overridden by descendent classes to customize behavior. * * @param Row $row The row being modified. * @return int|float */ protected function getDivisor($row) { if (!is_null($this->totalValueUsedAsDivisor)) { return $this->totalValueUsedAsDivisor; } else if ($this->getDivisorFromSummaryRow) { $summaryRow = $this->table->getRowFromId(DataTable::ID_SUMMARY_ROW); return $summaryRow->getColumn($this->columnNameUsedAsDivisor); } else { return $row->getColumn($this->columnNameUsedAsDivisor); } } }