Completed
Pull Request — master (#66)
by Greg
06:45
created

NumericCellRenderer::columnWidth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Consolidation\OutputFormatters\StructuredData;
3
4
use Consolidation\OutputFormatters\Options\FormatterOptions;
5
6
/**
7
 * Create a formatter to add commas to numeric data.
8
 *
9
 * Example:
10
 *
11
 *    -------
12
 *     Value
13
 *    -------
14
 *      2,384
15
 *    143,894
16
 *         23
17
 *     98,538
18
 *
19
 * This formatter may also be re-used for other purposes where right-justified
20
 * data is desired by simply making a subclass. See method comments below.
21
 *
22
 * Usage:
23
 *
24
 *     return (new RowsOfFields($data))->addRenderer(
25
 *          new NumericCellRenderer($data, ['value'])
26
 *     );
27
 *
28
 */
29
class NumericCellRenderer implements RenderCellInterface
30
{
31
    protected $data;
32
    protected $renderedColumns;
33
    protected $renderedFormats;
34
    protected $widths = [];
35
36
    /**
37
     * NumericCellRenderer constructor
38
     */
39
    public function __construct($data, $renderedColumns, $renderedFormats = null)
40
    {
41
        $this->data = $data;
42
        $this->renderedColumns = $renderedColumns;
43
        $this->renderedFormats = $renderedFormats ?: ['table'];
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function renderCell($key, $cellData, FormatterOptions $options, $rowData)
50
    {
51
        if (!$this->isRenderedFormat($options) || !$this->isRenderedColumn($key)) {
52
            return $cellData;
53
        }
54
        if ($this->isRenderedData($cellData)) {
55
            $cellData = $this->formatCellData($cellData);
56
        }
57
        return $this->justifyCellData($key, $cellData);
58
    }
59
60
    /**
61
     * Right-justify the cell data.
62
     */
63
    protected function justifyCellData($key, $cellData)
64
    {
65
        return str_pad($cellData, $this->columnWidth($key), " ", STR_PAD_LEFT);
66
    }
67
68
    /**
69
     * Determine if this format is to be formatted.
70
     */
71
    protected function isRenderedFormat(FormatterOptions $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
        return true;
74
        return in_array($options->get(FormatterOptions::FORMAT), $this->renderedFormats);
0 ignored issues
show
Unused Code introduced by
return in_array($options...this->renderedFormats); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
75
    }
76
77
    /**
78
     * Determine if this is a column that should be formatted.
79
     */
80
    protected function isRenderedColumn($key)
81
    {
82
        return array_key_exists($key, $this->renderedColumns);
83
    }
84
85
    /**
86
     * Ignore cell data that should not be formatted.
87
     */
88
    protected function isRenderedData($cellData)
89
    {
90
        return is_numeric($cellData);
91
    }
92
93
    /**
94
     * Format the cell data.
95
     */
96
    protected function formatCellData($cellData)
97
    {
98
        return number_format($this->convertCellDataToString($cellData));
99
    }
100
101
    /**
102
     * This formatter only works with columns whose columns are strings.
103
     * To use this formatter for another purpose, override this method
104
     * to ensure that the cell data is a string before it is formatted.
105
     */
106
    protected function convertCellDataToString($cellData)
107
    {
108
        return $cellData;
109
    }
110
111
    /**
112
     * Get the cached column width for the provided key.
113
     */
114
    protected function columnWidth($key)
115
    {
116
        if (!isset($this->widths[$key])) {
117
            $this->widths[$key] = $this->calculateColumnWidth($key);
118
        }
119
        return $this->widths[$key];
120
    }
121
122
    /**
123
     * Using the cached table data, calculate the largest width
124
     * for the data in the table for use when right-justifying.
125
     */
126
    protected function calculateColumnWidth($key)
127
    {
128
        $width = isset($this->renderedColumns[$key]) ? $this->renderedColumns[$key] : 0;
129
        foreach ($this->data as $row) {
130
            $data = $this->formatCellData($row[$key]);
131
            $width = max(strlen($data), $width);
132
        }
133
        return $width;
134
    }
135
}
136