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 | public function __construct($columnId, $label = null) |
||
76 | |||
77 | /** |
||
78 | * Returns formatted value of current data cell. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getCurrentValueFormatted() |
||
86 | |||
87 | /** |
||
88 | * Formats value extracted from data row. |
||
89 | * |
||
90 | * @param $value |
||
91 | * @return string |
||
92 | */ |
||
93 | public function formatValue($value) |
||
98 | |||
99 | /** |
||
100 | * Returns current data cell value. |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function getCurrentValue() |
||
113 | |||
114 | /** |
||
115 | * Returns component that renders data (content of 'td'). |
||
116 | * |
||
117 | * @return DataViewComponentInterface |
||
118 | */ |
||
119 | public function getDataView() |
||
123 | |||
124 | /** |
||
125 | * Returns view component that displays column title. |
||
126 | * |
||
127 | * @return DataViewComponentInterface |
||
128 | */ |
||
129 | public function getTitleView() |
||
133 | |||
134 | /** |
||
135 | * Sets name of associated field in data rows returned from data provider. |
||
136 | * |
||
137 | * @param string|null $dataFieldName |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setDataFieldName($dataFieldName) |
||
145 | |||
146 | /** |
||
147 | * Returns name of associated field in data rows returned from data provider. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getDataFieldName() |
||
155 | |||
156 | /** |
||
157 | * Returns function calculating column value. |
||
158 | * |
||
159 | * This function accepts data row as first argument. |
||
160 | * |
||
161 | * @return callable|null |
||
162 | */ |
||
163 | public function getValueCalculator() |
||
167 | |||
168 | /** |
||
169 | * Sets function for calculating column value. |
||
170 | * |
||
171 | * This function accepts data row as first argument. |
||
172 | * |
||
173 | * @param $valueCalculator |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setValueCalculator(callable $valueCalculator = null) |
||
181 | |||
182 | /** |
||
183 | * Sets function for column value formatting. |
||
184 | * |
||
185 | * @param callable|null $valueFormatter |
||
186 | * @return Column |
||
187 | */ |
||
188 | public function setValueFormatter($valueFormatter) |
||
193 | |||
194 | /** |
||
195 | * Returns function fir column value formatting. |
||
196 | * |
||
197 | * @return callable|null |
||
198 | */ |
||
199 | public function getValueFormatter() |
||
203 | |||
204 | /** |
||
205 | * Sets component for rendering table cell with data (<td>). |
||
206 | * |
||
207 | * @param ContainerComponentInterface $cell |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setDataCell(ContainerComponentInterface $cell) |
||
219 | |||
220 | /** |
||
221 | * Returns title cell component ('th' tag). |
||
222 | * |
||
223 | * @return ComponentInterface |
||
224 | */ |
||
225 | public function getTitleCell() |
||
232 | |||
233 | /** |
||
234 | * Returns component that renders data cell ('td' tag). |
||
235 | * |
||
236 | * @return ComponentInterface |
||
237 | */ |
||
238 | public function getDataCell() |
||
247 | |||
248 | /** |
||
249 | * Sets title cell component ('th' tag). |
||
250 | * |
||
251 | * @param ContainerComponentInterface $cell |
||
252 | * @return $this |
||
253 | */ |
||
254 | public function setTitleCell(ContainerComponentInterface $cell) |
||
263 | |||
264 | /** |
||
265 | * Returns text label that will be rendered in table header. |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getLabel() |
||
276 | |||
277 | /** |
||
278 | * Sets text label that will be rendered in table header. |
||
279 | * |
||
280 | * @param string|null $label |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function setLabel($label) |
||
288 | |||
289 | /** |
||
290 | * @param Compound $root |
||
291 | */ |
||
292 | public function attachToCompound(Compound $root) |
||
305 | |||
306 | /** |
||
307 | * @return Part |
||
308 | */ |
||
309 | protected function getDataCellPart() |
||
320 | |||
321 | /** |
||
322 | * @return Part |
||
323 | */ |
||
324 | protected function getTitleCellPart() |
||
335 | |||
336 | /** |
||
337 | * @return null|Grid |
||
338 | */ |
||
339 | protected function getGrid() |
||
343 | } |
||
344 |
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: