1 | <?php |
||
9 | class TableRenderer implements Renderer { |
||
10 | |||
11 | /** @var RendererRegistry */ |
||
12 | private $renderers; |
||
13 | |||
14 | public function __construct(RendererRegistry $renderers) { |
||
15 | $this->renderers = $renderers; |
||
16 | } |
||
17 | |||
18 | /** |
||
19 | * @param mixed $value |
||
20 | * @return bool |
||
21 | */ |
||
22 | public function handles($value) { |
||
25 | |||
26 | /** |
||
27 | * @param Table $value |
||
28 | * @return mixed |
||
29 | */ |
||
30 | public function render($value) { |
||
31 | $climate = new CLImate(); |
||
32 | $climate->output->defaultTo('buffer'); |
||
33 | |||
34 | $data = $this->prepareData($value); |
||
35 | if (!$data) { |
||
|
|||
36 | return '(empty)'; |
||
37 | } |
||
38 | |||
39 | $climate->table($data); |
||
40 | |||
41 | /** @var \League\CLImate\Util\Writer\Buffer $buffer */ |
||
42 | $buffer = $climate->output->get('buffer'); |
||
43 | |||
44 | return PHP_EOL . $buffer->get(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param Table $object |
||
49 | * @return array |
||
50 | */ |
||
51 | protected function prepareData($object) { |
||
52 | $headers = $this->getHeaders($object); |
||
53 | |||
54 | $data = []; |
||
55 | foreach ($this->getRows($object) as $row) { |
||
56 | $dataRow = []; |
||
57 | foreach ($row as $col => $value) { |
||
58 | $dataRow[$headers[$col]] = str_replace("\n", " ", $this->renderers->getRenderer($value)->render($value)); |
||
59 | } |
||
60 | $data[] = $dataRow; |
||
61 | } |
||
62 | return $data; |
||
63 | } |
||
64 | |||
65 | protected function canDrawTables() { |
||
68 | |||
69 | /** |
||
70 | * @param Table $table |
||
71 | * @return mixed |
||
72 | */ |
||
73 | protected function getRows($table) { |
||
76 | |||
77 | /** |
||
78 | * @param Table $table |
||
79 | * @return mixed |
||
80 | */ |
||
81 | protected function getHeaders($table) { |
||
84 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.