|
1
|
|
|
<?php |
|
2
|
|
|
namespace rtens\domin\delivery\web\renderers\tables; |
|
3
|
|
|
|
|
4
|
|
|
use rtens\domin\delivery\RendererRegistry; |
|
5
|
|
|
use rtens\domin\delivery\web\Element; |
|
6
|
|
|
use rtens\domin\delivery\web\renderers\link\LinkPrinter; |
|
7
|
|
|
use rtens\domin\delivery\web\WebRenderer; |
|
8
|
|
|
use watoki\reflect\Property; |
|
9
|
|
|
|
|
10
|
|
|
class TableRenderer implements WebRenderer { |
|
11
|
|
|
|
|
12
|
|
|
/** @var RendererRegistry */ |
|
13
|
|
|
private $renderers; |
|
14
|
|
|
|
|
15
|
|
|
/** @var LinkPrinter */ |
|
16
|
|
|
private $printer; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(RendererRegistry $renderers, LinkPrinter $printer) { |
|
19
|
|
|
$this->renderers = $renderers; |
|
20
|
|
|
$this->printer = $printer; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param mixed $value |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public function handles($value) { |
|
28
|
|
|
return $value instanceof Table; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Table $value |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
public function render($value) { |
|
36
|
|
|
$rows = $this->renderRows($value); |
|
37
|
|
|
|
|
38
|
|
|
if (!$rows) { |
|
|
|
|
|
|
39
|
|
|
return null; |
|
40
|
|
|
}; |
|
41
|
|
|
|
|
42
|
|
|
return (string)new Element('table', ['class' => 'table table-striped'], array_merge([ |
|
43
|
|
|
new Element('thead', [], [new Element('tr', [], $this->renderHeaders($value))]) |
|
44
|
|
|
], $rows)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function renderHeaders($table) { |
|
48
|
|
|
$headers = [new Element('th', ['width' => '1'])]; |
|
49
|
|
|
foreach ($this->getHeaders($table) as $caption) { |
|
50
|
|
|
$headers[] = new Element('th', [], [$caption]); |
|
51
|
|
|
} |
|
52
|
|
|
return $headers; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Table $table |
|
57
|
|
|
* @return array |
|
58
|
|
|
* @throws \Exception |
|
59
|
|
|
*/ |
|
60
|
|
|
private function renderRows($table) { |
|
61
|
|
|
$rows = []; |
|
62
|
|
|
foreach ($table->getItems() as $item) { |
|
63
|
|
|
$row = [new Element('td', [], $this->printer->createDropDown($item))]; |
|
64
|
|
View Code Duplication |
foreach ($table->getCells($item) as $cell) { |
|
|
|
|
|
|
65
|
|
|
$row[] = new Element('td', [], [$this->renderers->getRenderer($cell)->render($cell)]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$rows[] = new Element('tr', [], $row); |
|
69
|
|
|
} |
|
70
|
|
|
return $rows; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Table $value |
|
75
|
|
|
* @return array|Element[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function headElements($value) { |
|
78
|
|
|
$elements = []; |
|
79
|
|
|
foreach ($value->getItems() as $item) { |
|
80
|
|
View Code Duplication |
foreach ($value->getCells($item) as $cell) { |
|
|
|
|
|
|
81
|
|
|
$renderer = $this->renderers->getRenderer($cell); |
|
82
|
|
|
if ($renderer instanceof WebRenderer) { |
|
83
|
|
|
$elements = array_merge($elements, $renderer->headElements($cell)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
return $elements; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Table $table |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getHeaders($table) { |
|
95
|
|
|
return $table->getHeaders(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
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.