1 | <?php |
||
21 | class Column implements PartInterface |
||
22 | { |
||
23 | use PartTrait { |
||
24 | PartTrait::attachToCompound as attachToCompoundInternal; |
||
25 | } |
||
26 | use ChildNodeTrait; |
||
27 | |||
28 | /** |
||
29 | * Text label that will be rendered in table header. |
||
30 | * |
||
31 | * @var string|null |
||
32 | */ |
||
33 | protected $label; |
||
34 | |||
35 | /** @var ComponentInterface */ |
||
36 | protected $dataCell; |
||
37 | |||
38 | /** @var ComponentInterface */ |
||
39 | protected $titleCell; |
||
40 | |||
41 | /** @var ComponentInterface */ |
||
42 | protected $titleView; |
||
43 | |||
44 | /** @var ComponentInterface */ |
||
45 | protected $dataView; |
||
46 | |||
47 | /** @var Part|null */ |
||
48 | protected $titleCellPart; |
||
49 | |||
50 | /** @var Part|null */ |
||
51 | protected $dataCellPart; |
||
52 | |||
53 | /** @var string|null */ |
||
54 | protected $dataFieldName; |
||
55 | |||
56 | /** @var callable|null */ |
||
57 | protected $valueCalculator; |
||
58 | |||
59 | /** @var callable|null */ |
||
60 | protected $valueFormatter; |
||
61 | |||
62 | /** |
||
63 | * Constructor. |
||
64 | * |
||
65 | * @param string|null $columnId unique column name for internal usage |
||
66 | * @param string|null $label column label |
||
67 | */ |
||
68 | 2 | public function __construct($columnId, $label = null) |
|
76 | |||
77 | /** |
||
78 | * Returns formatted value of current data cell. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 2 | public function getCurrentValueFormatted() |
|
86 | |||
87 | /** |
||
88 | * Formats value extracted from data row. |
||
89 | * |
||
90 | * @param $value |
||
91 | * @return string |
||
92 | */ |
||
93 | 2 | public function formatValue($value) |
|
98 | |||
99 | /** |
||
100 | * Returns current data cell value. |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | 2 | public function getCurrentValue() |
|
114 | |||
115 | /** |
||
116 | * Returns component that renders data (content of 'td'). |
||
117 | * |
||
118 | * @return DataViewComponentInterface |
||
119 | */ |
||
120 | public function getDataView() |
||
124 | |||
125 | /** |
||
126 | * Returns view component that displays column title. |
||
127 | * |
||
128 | * @return DataViewComponentInterface |
||
129 | */ |
||
130 | public function getTitleView() |
||
134 | |||
135 | /** |
||
136 | * Sets name of associated field in data rows returned from data provider. |
||
137 | * |
||
138 | * @param string|null $dataFieldName |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function setDataFieldName($dataFieldName) |
||
146 | |||
147 | /** |
||
148 | * Returns name of associated field in data rows returned from data provider. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 2 | public function getDataFieldName() |
|
156 | |||
157 | /** |
||
158 | * Returns function calculating column value. |
||
159 | * |
||
160 | * This function accepts data row as first argument. |
||
161 | * |
||
162 | * @return callable|null |
||
163 | */ |
||
164 | 2 | public function getValueCalculator() |
|
168 | |||
169 | /** |
||
170 | * Sets function for calculating column value. |
||
171 | * |
||
172 | * This function accepts data row as first argument. |
||
173 | * |
||
174 | * @param $valueCalculator |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setValueCalculator(callable $valueCalculator = null) |
||
182 | |||
183 | /** |
||
184 | * Sets function for column value formatting. |
||
185 | * |
||
186 | * @param callable|null $valueFormatter |
||
187 | * @return Column |
||
188 | */ |
||
189 | public function setValueFormatter(callable $valueFormatter = null) |
||
194 | |||
195 | /** |
||
196 | * Returns function fir column value formatting. |
||
197 | * |
||
198 | * @return callable|null |
||
199 | */ |
||
200 | 2 | public function getValueFormatter() |
|
204 | |||
205 | /** |
||
206 | * Sets component for rendering table cell with data (<td>). |
||
207 | * |
||
208 | * @param ContainerComponentInterface $cell |
||
209 | * @return $this |
||
210 | */ |
||
211 | 2 | public function setDataCell(ContainerComponentInterface $cell) |
|
220 | |||
221 | /** |
||
222 | * Returns title cell component ('th' tag). |
||
223 | * |
||
224 | * @return ComponentInterface |
||
225 | */ |
||
226 | 2 | public function getTitleCell() |
|
233 | |||
234 | /** |
||
235 | * Returns component that renders data cell ('td' tag). |
||
236 | * |
||
237 | * @return ComponentInterface |
||
238 | */ |
||
239 | 2 | public function getDataCell() |
|
248 | |||
249 | /** |
||
250 | * Sets title cell component ('th' tag). |
||
251 | * |
||
252 | * @param ContainerComponentInterface $cell |
||
253 | * @return $this |
||
254 | */ |
||
255 | 2 | public function setTitleCell(ContainerComponentInterface $cell) |
|
264 | |||
265 | /** |
||
266 | * Returns text label that will be rendered in table header. |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 2 | public function getLabel() |
|
277 | |||
278 | /** |
||
279 | * Sets text label that will be rendered in table header. |
||
280 | * |
||
281 | * @param string|null $label |
||
282 | * @return $this |
||
283 | */ |
||
284 | 2 | public function setLabel($label) |
|
289 | |||
290 | /** |
||
291 | * @param Compound $root |
||
292 | */ |
||
293 | 2 | public function attachToCompound(Compound $root) |
|
306 | |||
307 | /** |
||
308 | * @return Part |
||
309 | */ |
||
310 | 2 | protected function getDataCellPart() |
|
321 | |||
322 | /** |
||
323 | * @return Part |
||
324 | */ |
||
325 | 2 | protected function getTitleCellPart() |
|
336 | |||
337 | /** |
||
338 | * @return null|Grid |
||
339 | */ |
||
340 | 2 | protected function getGrid() |
|
344 | } |
||
345 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: