Chris@13: addRenderer( Chris@13: * new NumericCellRenderer($data, ['value']) Chris@13: * ); Chris@13: * Chris@13: */ Chris@13: class NumericCellRenderer implements RenderCellInterface, FormatterAwareInterface Chris@13: { Chris@13: use FormatterAwareTrait; Chris@13: Chris@13: protected $data; Chris@13: protected $renderedColumns; Chris@13: protected $widths = []; Chris@13: Chris@13: /** Chris@13: * NumericCellRenderer constructor Chris@13: */ Chris@13: public function __construct($data, $renderedColumns) Chris@13: { Chris@13: $this->data = $data; Chris@13: $this->renderedColumns = $renderedColumns; Chris@13: } Chris@13: Chris@13: /** Chris@13: * @inheritdoc Chris@13: */ Chris@13: public function renderCell($key, $cellData, FormatterOptions $options, $rowData) Chris@13: { Chris@13: if (!$this->isRenderedFormat($options) || !$this->isRenderedColumn($key)) { Chris@13: return $cellData; Chris@13: } Chris@13: if ($this->isRenderedData($cellData)) { Chris@13: $cellData = $this->formatCellData($cellData); Chris@13: } Chris@13: return $this->justifyCellData($key, $cellData); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Right-justify the cell data. Chris@13: */ Chris@13: protected function justifyCellData($key, $cellData) Chris@13: { Chris@13: return str_pad($cellData, $this->columnWidth($key), " ", STR_PAD_LEFT); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Determine if this format is to be formatted. Chris@13: */ Chris@13: protected function isRenderedFormat(FormatterOptions $options) Chris@13: { Chris@13: return $this->isHumanReadable(); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Determine if this is a column that should be formatted. Chris@13: */ Chris@13: protected function isRenderedColumn($key) Chris@13: { Chris@13: return array_key_exists($key, $this->renderedColumns); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Ignore cell data that should not be formatted. Chris@13: */ Chris@13: protected function isRenderedData($cellData) Chris@13: { Chris@13: return is_numeric($cellData); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Format the cell data. Chris@13: */ Chris@13: protected function formatCellData($cellData) Chris@13: { Chris@13: return number_format($this->convertCellDataToString($cellData)); Chris@13: } Chris@13: Chris@13: /** Chris@13: * This formatter only works with columns whose columns are strings. Chris@13: * To use this formatter for another purpose, override this method Chris@13: * to ensure that the cell data is a string before it is formatted. Chris@13: */ Chris@13: protected function convertCellDataToString($cellData) Chris@13: { Chris@13: return $cellData; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get the cached column width for the provided key. Chris@13: */ Chris@13: protected function columnWidth($key) Chris@13: { Chris@13: if (!isset($this->widths[$key])) { Chris@13: $this->widths[$key] = $this->calculateColumnWidth($key); Chris@13: } Chris@13: return $this->widths[$key]; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Using the cached table data, calculate the largest width Chris@13: * for the data in the table for use when right-justifying. Chris@13: */ Chris@13: protected function calculateColumnWidth($key) Chris@13: { Chris@13: $width = isset($this->renderedColumns[$key]) ? $this->renderedColumns[$key] : 0; Chris@13: foreach ($this->data as $row) { Chris@13: $data = $this->formatCellData($row[$key]); Chris@13: $width = max(strlen($data), $width); Chris@13: } Chris@13: return $width; Chris@13: } Chris@13: }