1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Ui; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-ui' |
8
|
|
|
use Charcoal\Ui\PrioritizableInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides an implementation of {@see \Charcoal\Ui\UiItemInterface}. |
12
|
|
|
*/ |
13
|
|
|
trait UiItemTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var boolean |
17
|
|
|
*/ |
18
|
|
|
private $active = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The UI item type. |
22
|
|
|
* |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
private $type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The UI item's template. |
29
|
|
|
* |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $template; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The UI item's icon. |
36
|
|
|
* |
37
|
|
|
* Note: Only icons from the {@link http://fontawesome.io/ Font Awesome} |
38
|
|
|
* library are supported. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
private $icon; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The UI item's title. |
46
|
|
|
* |
47
|
|
|
* @var \Charcoal\Translator\Translation |
48
|
|
|
*/ |
49
|
|
|
private $title = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The UI item's sub-title. |
53
|
|
|
* |
54
|
|
|
* @var \Charcoal\Translator\Translation |
55
|
|
|
*/ |
56
|
|
|
private $subtitle = ''; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The UI item's description. |
60
|
|
|
* |
61
|
|
|
* @var \Charcoal\Translator\Translation |
62
|
|
|
*/ |
63
|
|
|
private $description = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The UI item's notes. |
67
|
|
|
* |
68
|
|
|
* @var \Charcoal\Translator\Translation |
69
|
|
|
*/ |
70
|
|
|
private $notes = ''; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The title is displayed by default. |
74
|
|
|
* |
75
|
|
|
* @var boolean |
76
|
|
|
*/ |
77
|
|
|
private $showTitle = true; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The sub-title is displayed by default. |
81
|
|
|
* |
82
|
|
|
* @var boolean |
83
|
|
|
*/ |
84
|
|
|
private $showSubtitle = true; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The description is displayed by default. |
88
|
|
|
* |
89
|
|
|
* @var boolean |
90
|
|
|
*/ |
91
|
|
|
private $showDescription = true; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The notes are displayed by default. |
95
|
|
|
* |
96
|
|
|
* @var boolean |
97
|
|
|
*/ |
98
|
|
|
private $showNotes = true; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The icon is displayed by default. |
102
|
|
|
* |
103
|
|
|
* @var boolean |
104
|
|
|
*/ |
105
|
|
|
private $showIcon = true; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The header is displayed by default. |
109
|
|
|
* |
110
|
|
|
* @var boolean |
111
|
|
|
*/ |
112
|
|
|
private $showHeader = true; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The footer is displayed by default. |
116
|
|
|
* |
117
|
|
|
* @var boolean |
118
|
|
|
*/ |
119
|
|
|
private $showFooter = true; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* The tab title is dislpayed by default. |
123
|
|
|
* |
124
|
|
|
* @var boolean |
125
|
|
|
*/ |
126
|
|
|
private $showTabTitle = true; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Activates/deactivates the UI item. |
130
|
|
|
* |
131
|
|
|
* @param boolean $active Activate (TRUE) or deactivate (FALSE) the UI item. |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
|
|
public function setActive($active) |
135
|
|
|
{ |
136
|
|
|
$this->active = !!$active; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Determine if the UI item is active. |
143
|
|
|
* |
144
|
|
|
* @return boolean |
145
|
|
|
*/ |
146
|
|
|
public function active() |
147
|
|
|
{ |
148
|
|
|
return $this->active; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the UI item type. |
153
|
|
|
* |
154
|
|
|
* @param string|null $type The UI item type. |
155
|
|
|
* @throws InvalidArgumentException If the type is not a string (or null). |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function setType($type) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
if (is_string($type) || $type === null) { |
161
|
|
|
$this->type = $type; |
162
|
|
|
} else { |
163
|
|
|
throw new InvalidArgumentException( |
164
|
|
|
'Can not set UI item config type: Type must be a string or NULL' |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Retrieve the UI item type. |
173
|
|
|
* |
174
|
|
|
* If it is not explicitely set (or null), then return the object's FQN. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function type() |
179
|
|
|
{ |
180
|
|
|
if ($this->type === null) { |
181
|
|
|
return static::class; |
182
|
|
|
} |
183
|
|
|
return $this->type; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the UI item's template. |
188
|
|
|
* |
189
|
|
|
* Usually, a path to a file containing the template to be rendered. |
190
|
|
|
* |
191
|
|
|
* @param string $template A template (identifier). |
192
|
|
|
* @throws InvalidArgumentException If the template is not a string. |
193
|
|
|
* @return self |
194
|
|
|
*/ |
195
|
|
|
public function setTemplate($template) |
196
|
|
|
{ |
197
|
|
|
if (!is_string($template)) { |
198
|
|
|
throw new InvalidArgumentException( |
199
|
|
|
'The UI Item can not set the template, must be a string' |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->template = $template; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Retrieve the UI item's template. |
210
|
|
|
* |
211
|
|
|
* @return string If unset, returns the UI item type. |
212
|
|
|
*/ |
213
|
|
|
public function template() |
214
|
|
|
{ |
215
|
|
|
if ($this->template === null) { |
216
|
|
|
return $this->type(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this->template; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set the UI item's title. |
224
|
|
|
* |
225
|
|
|
* @param mixed $title A title. |
226
|
|
|
* @return self |
227
|
|
|
*/ |
228
|
|
|
public function setTitle($title) |
229
|
|
|
{ |
230
|
|
|
$this->title = $this->translator()->translation($title); |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Retrieve the title. |
236
|
|
|
* |
237
|
|
|
* @return \Charcoal\Translator\Translation|null |
238
|
|
|
*/ |
239
|
|
|
public function title() |
240
|
|
|
{ |
241
|
|
|
return $this->title; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Set the UI item's sub-title. |
246
|
|
|
* |
247
|
|
|
* @param mixed $subtitle A sub-title. |
248
|
|
|
* @return self |
249
|
|
|
*/ |
250
|
|
|
public function setSubtitle($subtitle) |
251
|
|
|
{ |
252
|
|
|
$this->subtitle = $this->translator()->translation($subtitle); |
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Retrieve the sub-title. |
258
|
|
|
* |
259
|
|
|
* @return \Charcoal\Translator\Translation|null |
260
|
|
|
*/ |
261
|
|
|
public function subtitle() |
262
|
|
|
{ |
263
|
|
|
return $this->subtitle; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Set the UI item's description. |
268
|
|
|
* |
269
|
|
|
* @param mixed $description A description. |
270
|
|
|
* @return self |
271
|
|
|
*/ |
272
|
|
|
public function setDescription($description) |
273
|
|
|
{ |
274
|
|
|
$this->description = $this->translator()->translation($description); |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Retrieve the description. |
280
|
|
|
* |
281
|
|
|
* @return \Charcoal\Translator\Translation|null |
282
|
|
|
*/ |
283
|
|
|
public function description() |
284
|
|
|
{ |
285
|
|
|
return $this->description; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Set notes about the UI item. |
290
|
|
|
* |
291
|
|
|
* @param mixed $notes Notes. |
292
|
|
|
* @return self |
293
|
|
|
*/ |
294
|
|
|
public function setNotes($notes) |
295
|
|
|
{ |
296
|
|
|
$this->notes = $this->translator()->translation($notes); |
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Retrieve the notes. |
302
|
|
|
* |
303
|
|
|
* @return \Charcoal\Translator\Translation|null |
304
|
|
|
*/ |
305
|
|
|
public function notes() |
306
|
|
|
{ |
307
|
|
|
return $this->notes; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Retrieve the path to the item's icon. |
312
|
|
|
* |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
|
|
public function icon() |
316
|
|
|
{ |
317
|
|
|
return $this->icon; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Set the path to the item's icon associated with the object. |
322
|
|
|
* |
323
|
|
|
* @param string $icon A path to an image. |
324
|
|
|
* @return self |
325
|
|
|
*/ |
326
|
|
|
public function setIcon($icon) |
327
|
|
|
{ |
328
|
|
|
$this->icon = $icon; |
329
|
|
|
|
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Show/hide the UI item's title. |
335
|
|
|
* |
336
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the title. |
337
|
|
|
* @return self |
338
|
|
|
*/ |
339
|
|
|
public function setShowTitle($show) |
340
|
|
|
{ |
341
|
|
|
$this->showTitle = !!$show; |
342
|
|
|
|
343
|
|
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Determine if the title is to be displayed. |
348
|
|
|
* |
349
|
|
|
* @return boolean If TRUE or unset, check if there is a title. |
350
|
|
|
*/ |
351
|
|
|
public function showTitle() |
352
|
|
|
{ |
353
|
|
|
if ($this->showTitle === false) { |
354
|
|
|
return false; |
355
|
|
|
} else { |
356
|
|
|
return !!$this->title(); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Show/hide the UI item's sub-title. |
362
|
|
|
* |
363
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the sub-title. |
364
|
|
|
* @return self |
365
|
|
|
*/ |
366
|
|
|
public function setShowSubtitle($show) |
367
|
|
|
{ |
368
|
|
|
$this->showSubtitle = !!$show; |
369
|
|
|
|
370
|
|
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Determine if the sub-title is to be displayed. |
375
|
|
|
* |
376
|
|
|
* @return boolean If TRUE or unset, check if there is a sub-title. |
377
|
|
|
*/ |
378
|
|
|
public function showSubtitle() |
379
|
|
|
{ |
380
|
|
|
if ($this->showSubtitle === false) { |
381
|
|
|
return false; |
382
|
|
|
} else { |
383
|
|
|
return !!$this->subtitle(); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Show/hide the UI item's description. |
389
|
|
|
* |
390
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the description. |
391
|
|
|
* @return self |
392
|
|
|
*/ |
393
|
|
|
public function setShowDescription($show) |
394
|
|
|
{ |
395
|
|
|
$this->showDescription = !!$show; |
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Determine if the description is to be displayed. |
402
|
|
|
* |
403
|
|
|
* @return boolean If TRUE or unset, check if there is a description. |
404
|
|
|
*/ |
405
|
|
|
public function showDescription() |
406
|
|
|
{ |
407
|
|
|
if ($this->showDescription === false) { |
408
|
|
|
return false; |
409
|
|
|
} else { |
410
|
|
|
return !!$this->description(); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Show/hide the UI item's notes. |
416
|
|
|
* |
417
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the notes. |
418
|
|
|
* @return self |
419
|
|
|
*/ |
420
|
|
|
public function setShowNotes($show) |
421
|
|
|
{ |
422
|
|
|
$this->showNotes = !!$show; |
423
|
|
|
|
424
|
|
|
return $this; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Determine if the notes is to be displayed. |
429
|
|
|
* |
430
|
|
|
* @return boolean If TRUE or unset, check if there are notes. |
431
|
|
|
*/ |
432
|
|
|
public function showNotes() |
433
|
|
|
{ |
434
|
|
|
if ($this->showNotes === false) { |
435
|
|
|
return false; |
436
|
|
|
} else { |
437
|
|
|
return !!$this->notes(); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Show/hide the UI item's icon. |
443
|
|
|
* |
444
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the icon. |
445
|
|
|
* @return self |
446
|
|
|
*/ |
447
|
|
|
public function setShowIcon($show) |
448
|
|
|
{ |
449
|
|
|
$this->showIcon = !!$show; |
450
|
|
|
|
451
|
|
|
return $this; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Determine if the icon is to be displayed. |
456
|
|
|
* |
457
|
|
|
* @return boolean If TRUE or unset, check if there is an icon. |
458
|
|
|
*/ |
459
|
|
|
public function showIcon() |
460
|
|
|
{ |
461
|
|
|
if ($this->showIcon === false) { |
462
|
|
|
return false; |
463
|
|
|
} else { |
464
|
|
|
return !!$this->icon(); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Show/hide the UI item's header. |
470
|
|
|
* |
471
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the header. |
472
|
|
|
* @return self |
473
|
|
|
*/ |
474
|
|
|
public function setShowHeader($show) |
475
|
|
|
{ |
476
|
|
|
$this->showHeader = !!$show; |
477
|
|
|
|
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Determine if the header is to be displayed. |
483
|
|
|
* |
484
|
|
|
* @return boolean If TRUE or unset, check if there is a title. |
485
|
|
|
*/ |
486
|
|
|
public function showHeader() |
487
|
|
|
{ |
488
|
|
|
if ($this->showHeader === false) { |
489
|
|
|
return false; |
490
|
|
|
} else { |
491
|
|
|
return $this->showTitle(); |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Show/hide the UI item's footer. |
497
|
|
|
* |
498
|
|
|
* @param boolean $show Show (TRUE) or hide (FALSE) the footer. |
499
|
|
|
* @return self |
500
|
|
|
*/ |
501
|
|
|
public function setShowFooter($show) |
502
|
|
|
{ |
503
|
|
|
$this->showFooter = !!$show; |
504
|
|
|
|
505
|
|
|
return $this; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Determine if the footer is to be displayed. |
510
|
|
|
* |
511
|
|
|
* @return boolean If TRUE or unset, check if there are notes. |
512
|
|
|
*/ |
513
|
|
|
public function showFooter() |
514
|
|
|
{ |
515
|
|
|
if ($this->showFooter === false) { |
516
|
|
|
return false; |
517
|
|
|
} else { |
518
|
|
|
return $this->showNotes(); |
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @param boolean $showTabTitle |
524
|
|
|
* @return self |
525
|
|
|
*/ |
526
|
|
|
public function setShowTabTitle($showTabTitle) |
527
|
|
|
{ |
528
|
|
|
$this->showTabTitle = !!$showTabTitle; |
529
|
|
|
|
530
|
|
|
return $this; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* @return boolean If TRUE or unset, check if there is a title. |
535
|
|
|
*/ |
536
|
|
|
public function showTabTitle() |
537
|
|
|
{ |
538
|
|
|
if ($this->showTabTitle === false) { |
539
|
|
|
return false; |
540
|
|
|
} else { |
541
|
|
|
return $this->showTitle(); |
542
|
|
|
} |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Comparison function used by {@see uasort()}. |
547
|
|
|
* |
548
|
|
|
* @param PrioritizableInterface $a Sortable entity A. |
549
|
|
|
* @param PrioritizableInterface $b Sortable entity B. |
550
|
|
|
* @return integer Sorting value: -1 or 1. |
551
|
|
|
*/ |
552
|
|
|
protected function sortItemsByPriority( |
553
|
|
|
PrioritizableInterface $a, |
554
|
|
|
PrioritizableInterface $b |
555
|
|
|
) { |
556
|
|
|
$priorityA = $a->priority(); |
557
|
|
|
$priorityB = $b->priority(); |
558
|
|
|
|
559
|
|
|
if ($priorityA === $priorityB) { |
560
|
|
|
return 0; |
561
|
|
|
} |
562
|
|
|
return ($priorityA < $priorityB) ? (-1) : 1; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* All UI objects are translatable, therefore are translator-aware. |
567
|
|
|
* |
568
|
|
|
* @return \Charcoal\Translator\Translator |
569
|
|
|
*/ |
570
|
|
|
abstract protected function translator(); |
571
|
|
|
} |
572
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.