X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fconsolidation%2Foutput-formatters%2Fsrc%2FStructuredData%2FNumericCellRenderer.php;fp=vendor%2Fconsolidation%2Foutput-formatters%2Fsrc%2FStructuredData%2FNumericCellRenderer.php;h=372b9b1ba133b051660292e1b4c03dca62df621c;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=0000000000000000000000000000000000000000;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/vendor/consolidation/output-formatters/src/StructuredData/NumericCellRenderer.php b/vendor/consolidation/output-formatters/src/StructuredData/NumericCellRenderer.php new file mode 100644 index 000000000..372b9b1ba --- /dev/null +++ b/vendor/consolidation/output-formatters/src/StructuredData/NumericCellRenderer.php @@ -0,0 +1,137 @@ +addRenderer( + * new NumericCellRenderer($data, ['value']) + * ); + * + */ +class NumericCellRenderer implements RenderCellInterface, FormatterAwareInterface +{ + use FormatterAwareTrait; + + protected $data; + protected $renderedColumns; + protected $widths = []; + + /** + * NumericCellRenderer constructor + */ + public function __construct($data, $renderedColumns) + { + $this->data = $data; + $this->renderedColumns = $renderedColumns; + } + + /** + * @inheritdoc + */ + public function renderCell($key, $cellData, FormatterOptions $options, $rowData) + { + if (!$this->isRenderedFormat($options) || !$this->isRenderedColumn($key)) { + return $cellData; + } + if ($this->isRenderedData($cellData)) { + $cellData = $this->formatCellData($cellData); + } + return $this->justifyCellData($key, $cellData); + } + + /** + * Right-justify the cell data. + */ + protected function justifyCellData($key, $cellData) + { + return str_pad($cellData, $this->columnWidth($key), " ", STR_PAD_LEFT); + } + + /** + * Determine if this format is to be formatted. + */ + protected function isRenderedFormat(FormatterOptions $options) + { + return $this->isHumanReadable(); + } + + /** + * Determine if this is a column that should be formatted. + */ + protected function isRenderedColumn($key) + { + return array_key_exists($key, $this->renderedColumns); + } + + /** + * Ignore cell data that should not be formatted. + */ + protected function isRenderedData($cellData) + { + return is_numeric($cellData); + } + + /** + * Format the cell data. + */ + protected function formatCellData($cellData) + { + return number_format($this->convertCellDataToString($cellData)); + } + + /** + * This formatter only works with columns whose columns are strings. + * To use this formatter for another purpose, override this method + * to ensure that the cell data is a string before it is formatted. + */ + protected function convertCellDataToString($cellData) + { + return $cellData; + } + + /** + * Get the cached column width for the provided key. + */ + protected function columnWidth($key) + { + if (!isset($this->widths[$key])) { + $this->widths[$key] = $this->calculateColumnWidth($key); + } + return $this->widths[$key]; + } + + /** + * Using the cached table data, calculate the largest width + * for the data in the table for use when right-justifying. + */ + protected function calculateColumnWidth($key) + { + $width = isset($this->renderedColumns[$key]) ? $this->renderedColumns[$key] : 0; + foreach ($this->data as $row) { + $data = $this->formatCellData($row[$key]); + $width = max(strlen($data), $width); + } + return $width; + } +}