|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Foo\Grid; |
|
6
|
|
|
|
|
7
|
|
|
use Foo\Grid\Cell\Cell; |
|
8
|
|
|
use Foo\Grid\Collection\Actions; |
|
9
|
|
|
use Foo\Grid\Collection\Cells; |
|
10
|
|
|
use Foo\Grid\Collection\Rows; |
|
11
|
|
|
use Foo\Grid\Row\Row; |
|
12
|
|
|
use Foo\Grid\Table\Table; |
|
13
|
|
|
use Foo\Translate\ITranslator; |
|
14
|
|
|
use Opulence\Orm\IEntity; |
|
15
|
|
|
|
|
16
|
|
|
class Factory |
|
17
|
|
|
{ |
|
18
|
|
|
const CELL_ACTIONS_CONTENT = 'grid:actions'; |
|
19
|
|
|
const CELL_ACTIONS_GROUP = 'actions'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param array $entities |
|
23
|
|
|
* @param array $getters |
|
24
|
|
|
* @param array $headers |
|
25
|
|
|
* @param array $headerAttributes |
|
26
|
|
|
* @param array $bodyAttributes |
|
27
|
|
|
* @param array $tableAttributes |
|
28
|
|
|
* @param array $gridAttributes |
|
29
|
|
|
* @param Actions|null $cellActions |
|
30
|
|
|
* @param Actions|null $gridActions |
|
31
|
|
|
* @param ITranslator|null $translator |
|
32
|
|
|
* |
|
33
|
|
|
* @return Grid |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function createGrid( |
|
36
|
|
|
array $entities, |
|
37
|
|
|
array $getters, |
|
38
|
|
|
array $headers, |
|
39
|
|
|
array $headerAttributes = [], |
|
40
|
|
|
array $bodyAttributes = [], |
|
41
|
|
|
array $tableAttributes = [], |
|
42
|
|
|
array $gridAttributes = [], |
|
43
|
|
|
Actions $cellActions = null, |
|
44
|
|
|
Actions $gridActions = null, |
|
45
|
|
|
ITranslator $translator = null |
|
46
|
|
|
) { |
|
47
|
|
|
$tableBody = static::createTableBody($entities, $getters, $bodyAttributes, $cellActions); |
|
|
|
|
|
|
48
|
|
|
$tableHeader = static::createTableHeader($headers, $headerAttributes, $cellActions); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$table = new Table($tableBody, $tableHeader, $tableAttributes); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$grid = new Grid($table, null, $gridActions, $gridAttributes); |
|
53
|
|
|
|
|
54
|
|
|
$grid->setIndentation(8); |
|
55
|
|
|
|
|
56
|
|
|
if ($translator) { |
|
57
|
|
|
$grid->setTranslator($translator); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $grid; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $entities |
|
65
|
|
|
* @param array $getters |
|
66
|
|
|
* @param array $bodyAttributes |
|
67
|
|
|
* @param Actions|null $actions |
|
68
|
|
|
* |
|
69
|
|
|
* @return array|Rows |
|
70
|
|
|
*/ |
|
71
|
|
|
private static function createTableBody( |
|
72
|
|
|
array $entities, |
|
73
|
|
|
array $getters, |
|
74
|
|
|
array $bodyAttributes = [], |
|
75
|
|
|
Actions $actions = null |
|
76
|
|
|
) { |
|
77
|
|
|
$tableBody = new Rows(); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($entities as $entity) { |
|
80
|
|
|
$cells = static::createTableRowCell($getters, $bodyAttributes, $entity); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$rowActions = $actions ? $actions->duplicate() : null; |
|
83
|
|
|
|
|
84
|
|
|
$row = new Row($cells, $rowActions); |
|
|
|
|
|
|
85
|
|
|
$row->setEntity($entity); |
|
86
|
|
|
|
|
87
|
|
|
$tableBody[] = $row; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $tableBody; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $getters |
|
95
|
|
|
* @param array $bodyAttributes |
|
96
|
|
|
* @param IEntity $entity |
|
97
|
|
|
* |
|
98
|
|
|
* @return array|Cells |
|
99
|
|
|
*/ |
|
100
|
|
|
private static function createTableRowCell( |
|
101
|
|
|
array $getters, |
|
102
|
|
|
array $bodyAttributes, |
|
103
|
|
|
IEntity $entity |
|
104
|
|
|
) { |
|
105
|
|
|
$cells = new Cells(); |
|
106
|
|
|
|
|
107
|
|
|
foreach ($getters as $group => $getter) { |
|
108
|
|
|
$content = is_callable($getter) ? $getter($entity) : (string)$entity->$getter(); |
|
109
|
|
|
|
|
110
|
|
|
$cells[] = new Cell($content, $group, $bodyAttributes, Cell::BODY); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $cells; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $headers |
|
118
|
|
|
* @param array $headerAttributes |
|
119
|
|
|
* @param Actions|null $actions |
|
120
|
|
|
* |
|
121
|
|
|
* @return array|Cells |
|
122
|
|
|
*/ |
|
123
|
|
|
private static function createTableHeader(array $headers, array $headerAttributes = [], Actions $actions = null) |
|
124
|
|
|
{ |
|
125
|
|
|
$cells = new Cells(Cells::HEAD); |
|
126
|
|
|
foreach ($headers as $group => $content) { |
|
127
|
|
|
$cells[] = new Cell($content, $group, $headerAttributes, Cell::HEAD); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($actions) { |
|
131
|
|
|
$cells[] = new Cell(static::CELL_ACTIONS_CONTENT, static::CELL_ACTIONS_GROUP, [], Cell::HEAD); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $cells; |
|
135
|
|
|
} |
|
136
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: