NumericCellRenderer.php 3.51 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
<?php
namespace Consolidation\OutputFormatters\StructuredData;

use Consolidation\OutputFormatters\Options\FormatterOptions;

use Consolidation\OutputFormatters\Formatters\FormatterAwareInterface;
use Consolidation\OutputFormatters\Formatters\FormatterAwareTrait;

/**
 * Create a formatter to add commas to numeric data.
 *
 * Example:
 *
 *    -------
 *     Value
 *    -------
 *      2,384
 *    143,894
 *         23
 *     98,538
 *
 * This formatter may also be re-used for other purposes where right-justified
 * data is desired by simply making a subclass. See method comments below.
 *
 * Usage:
 *
 *     return (new RowsOfFields($data))->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;
    }
}