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