Chris@13
|
1 <?php
|
Chris@13
|
2 namespace Consolidation\OutputFormatters\StructuredData;
|
Chris@13
|
3
|
Chris@13
|
4 use Consolidation\OutputFormatters\Options\FormatterOptions;
|
Chris@13
|
5
|
Chris@13
|
6 use Consolidation\OutputFormatters\Formatters\FormatterAwareInterface;
|
Chris@13
|
7 use Consolidation\OutputFormatters\Formatters\FormatterAwareTrait;
|
Chris@13
|
8
|
Chris@13
|
9 /**
|
Chris@13
|
10 * Create a formatter to add commas to numeric data.
|
Chris@13
|
11 *
|
Chris@13
|
12 * Example:
|
Chris@13
|
13 *
|
Chris@13
|
14 * -------
|
Chris@13
|
15 * Value
|
Chris@13
|
16 * -------
|
Chris@13
|
17 * 2,384
|
Chris@13
|
18 * 143,894
|
Chris@13
|
19 * 23
|
Chris@13
|
20 * 98,538
|
Chris@13
|
21 *
|
Chris@13
|
22 * This formatter may also be re-used for other purposes where right-justified
|
Chris@13
|
23 * data is desired by simply making a subclass. See method comments below.
|
Chris@13
|
24 *
|
Chris@13
|
25 * Usage:
|
Chris@13
|
26 *
|
Chris@13
|
27 * return (new RowsOfFields($data))->addRenderer(
|
Chris@13
|
28 * new NumericCellRenderer($data, ['value'])
|
Chris@13
|
29 * );
|
Chris@13
|
30 *
|
Chris@13
|
31 */
|
Chris@13
|
32 class NumericCellRenderer implements RenderCellInterface, FormatterAwareInterface
|
Chris@13
|
33 {
|
Chris@13
|
34 use FormatterAwareTrait;
|
Chris@13
|
35
|
Chris@13
|
36 protected $data;
|
Chris@13
|
37 protected $renderedColumns;
|
Chris@13
|
38 protected $widths = [];
|
Chris@13
|
39
|
Chris@13
|
40 /**
|
Chris@13
|
41 * NumericCellRenderer constructor
|
Chris@13
|
42 */
|
Chris@13
|
43 public function __construct($data, $renderedColumns)
|
Chris@13
|
44 {
|
Chris@13
|
45 $this->data = $data;
|
Chris@13
|
46 $this->renderedColumns = $renderedColumns;
|
Chris@13
|
47 }
|
Chris@13
|
48
|
Chris@13
|
49 /**
|
Chris@13
|
50 * @inheritdoc
|
Chris@13
|
51 */
|
Chris@13
|
52 public function renderCell($key, $cellData, FormatterOptions $options, $rowData)
|
Chris@13
|
53 {
|
Chris@13
|
54 if (!$this->isRenderedFormat($options) || !$this->isRenderedColumn($key)) {
|
Chris@13
|
55 return $cellData;
|
Chris@13
|
56 }
|
Chris@13
|
57 if ($this->isRenderedData($cellData)) {
|
Chris@13
|
58 $cellData = $this->formatCellData($cellData);
|
Chris@13
|
59 }
|
Chris@13
|
60 return $this->justifyCellData($key, $cellData);
|
Chris@13
|
61 }
|
Chris@13
|
62
|
Chris@13
|
63 /**
|
Chris@13
|
64 * Right-justify the cell data.
|
Chris@13
|
65 */
|
Chris@13
|
66 protected function justifyCellData($key, $cellData)
|
Chris@13
|
67 {
|
Chris@13
|
68 return str_pad($cellData, $this->columnWidth($key), " ", STR_PAD_LEFT);
|
Chris@13
|
69 }
|
Chris@13
|
70
|
Chris@13
|
71 /**
|
Chris@13
|
72 * Determine if this format is to be formatted.
|
Chris@13
|
73 */
|
Chris@13
|
74 protected function isRenderedFormat(FormatterOptions $options)
|
Chris@13
|
75 {
|
Chris@13
|
76 return $this->isHumanReadable();
|
Chris@13
|
77 }
|
Chris@13
|
78
|
Chris@13
|
79 /**
|
Chris@13
|
80 * Determine if this is a column that should be formatted.
|
Chris@13
|
81 */
|
Chris@13
|
82 protected function isRenderedColumn($key)
|
Chris@13
|
83 {
|
Chris@13
|
84 return array_key_exists($key, $this->renderedColumns);
|
Chris@13
|
85 }
|
Chris@13
|
86
|
Chris@13
|
87 /**
|
Chris@13
|
88 * Ignore cell data that should not be formatted.
|
Chris@13
|
89 */
|
Chris@13
|
90 protected function isRenderedData($cellData)
|
Chris@13
|
91 {
|
Chris@13
|
92 return is_numeric($cellData);
|
Chris@13
|
93 }
|
Chris@13
|
94
|
Chris@13
|
95 /**
|
Chris@13
|
96 * Format the cell data.
|
Chris@13
|
97 */
|
Chris@13
|
98 protected function formatCellData($cellData)
|
Chris@13
|
99 {
|
Chris@13
|
100 return number_format($this->convertCellDataToString($cellData));
|
Chris@13
|
101 }
|
Chris@13
|
102
|
Chris@13
|
103 /**
|
Chris@13
|
104 * This formatter only works with columns whose columns are strings.
|
Chris@13
|
105 * To use this formatter for another purpose, override this method
|
Chris@13
|
106 * to ensure that the cell data is a string before it is formatted.
|
Chris@13
|
107 */
|
Chris@13
|
108 protected function convertCellDataToString($cellData)
|
Chris@13
|
109 {
|
Chris@13
|
110 return $cellData;
|
Chris@13
|
111 }
|
Chris@13
|
112
|
Chris@13
|
113 /**
|
Chris@13
|
114 * Get the cached column width for the provided key.
|
Chris@13
|
115 */
|
Chris@13
|
116 protected function columnWidth($key)
|
Chris@13
|
117 {
|
Chris@13
|
118 if (!isset($this->widths[$key])) {
|
Chris@13
|
119 $this->widths[$key] = $this->calculateColumnWidth($key);
|
Chris@13
|
120 }
|
Chris@13
|
121 return $this->widths[$key];
|
Chris@13
|
122 }
|
Chris@13
|
123
|
Chris@13
|
124 /**
|
Chris@13
|
125 * Using the cached table data, calculate the largest width
|
Chris@13
|
126 * for the data in the table for use when right-justifying.
|
Chris@13
|
127 */
|
Chris@13
|
128 protected function calculateColumnWidth($key)
|
Chris@13
|
129 {
|
Chris@13
|
130 $width = isset($this->renderedColumns[$key]) ? $this->renderedColumns[$key] : 0;
|
Chris@13
|
131 foreach ($this->data as $row) {
|
Chris@13
|
132 $data = $this->formatCellData($row[$key]);
|
Chris@13
|
133 $width = max(strlen($data), $width);
|
Chris@13
|
134 }
|
Chris@13
|
135 return $width;
|
Chris@13
|
136 }
|
Chris@13
|
137 }
|