Passed
Push — master ( e985e7...9cead0 )
by Chauncey
03:51
created

UiItemTrait::setPriority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A UiItemTrait::title() 0 4 1
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
     * Activates/deactivates the UI item.
123
     *
124
     * @param  boolean $active Activate (TRUE) or deactivate (FALSE) the UI item.
125
     * @return self
126
     */
127
    public function setActive($active)
128
    {
129
        $this->active = !!$active;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Determine if the UI item is active.
136
     *
137
     * @return boolean
138
     */
139
    public function active()
140
    {
141
        return $this->active;
142
    }
143
144
    /**
145
     * Set the UI item type.
146
     *
147
     * @param  string|null $type The UI item type.
148
     * @throws InvalidArgumentException If the type is not a string (or null).
149
     * @return self
150
     */
151 View Code Duplication
    public function setType($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
152
    {
153
        if (is_string($type) || $type === null) {
154
            $this->type = $type;
155
        } else {
156
            throw new InvalidArgumentException(
157
                'Can not set UI item config type: Type must be a string or NULL'
158
            );
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * Retrieve the UI item type.
166
     *
167
     * If it is not explicitely set (or null), then return the object's FQN.
168
     *
169
     * @return string
170
     */
171
    public function type()
172
    {
173
        if ($this->type === null) {
174
            return static::class;
175
        }
176
        return $this->type;
177
    }
178
179
    /**
180
     * Set the UI item's template.
181
     *
182
     * Usually, a path to a file containing the template to be rendered.
183
     *
184
     * @param  string $template A template (identifier).
185
     * @throws InvalidArgumentException If the template is not a string.
186
     * @return self
187
     */
188
    public function setTemplate($template)
189
    {
190
        if (!is_string($template)) {
191
            throw new InvalidArgumentException(
192
                'The UI Item can not set the template, must be a string'
193
            );
194
        }
195
196
        $this->template = $template;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Retrieve the UI item's template.
203
     *
204
     * @return string If unset, returns the UI item type.
205
     */
206
    public function template()
207
    {
208
        if ($this->template === null) {
209
            return $this->type();
210
        }
211
212
        return $this->template;
213
    }
214
215
    /**
216
     * Set the UI item's title.
217
     *
218
     * @param  mixed $title A title.
219
     * @return self
220
     */
221
    public function setTitle($title)
222
    {
223
        $this->title = $this->translator()->translation($title);
224
        return $this;
225
    }
226
227
    /**
228
     * Retrieve the title.
229
     *
230
     * @return \Charcoal\Translator\Translation|null
231
     */
232
    public function title()
233
    {
234
        return $this->title;
235
    }
236
237
    /**
238
     * Set the UI item's sub-title.
239
     *
240
     * @param  mixed $subtitle A sub-title.
241
     * @return self
242
     */
243
    public function setSubtitle($subtitle)
244
    {
245
        $this->subtitle = $this->translator()->translation($subtitle);
246
        return $this;
247
    }
248
249
    /**
250
     * Retrieve the sub-title.
251
     *
252
     * @return \Charcoal\Translator\Translation|null
253
     */
254
    public function subtitle()
255
    {
256
        return $this->subtitle;
257
    }
258
259
    /**
260
     * Set the UI item's description.
261
     *
262
     * @param  mixed $description A description.
263
     * @return self
264
     */
265
    public function setDescription($description)
266
    {
267
        $this->description = $this->translator()->translation($description);
268
        return $this;
269
    }
270
271
    /**
272
     * Retrieve the description.
273
     *
274
     * @return \Charcoal\Translator\Translation|null
275
     */
276
    public function description()
277
    {
278
        return $this->description;
279
    }
280
281
    /**
282
     * Set notes about the UI item.
283
     *
284
     * @param  mixed $notes Notes.
285
     * @return self
286
     */
287
    public function setNotes($notes)
288
    {
289
        $this->notes = $this->translator()->translation($notes);
290
        return $this;
291
    }
292
293
    /**
294
     * Retrieve the notes.
295
     *
296
     * @return \Charcoal\Translator\Translation|null
297
     */
298
    public function notes()
299
    {
300
        return $this->notes;
301
    }
302
303
    /**
304
     * Retrieve the path to the item's icon.
305
     *
306
     * @todo  [mcaskill 2016-09-16] Move this to a tab interface in charcoal-admin
307
     *     so as to focus the icon getter/setter on being a Glyphicon.
308
     * @return string
309
     */
310
    public function icon()
311
    {
312
        return $this->icon;
313
    }
314
315
    /**
316
     * Set the path to the item's icon associated with the object.
317
     *
318
     * @param  string $icon A path to an image.
319
     * @return self
320
     */
321
    public function setIcon($icon)
322
    {
323
        $this->icon = $icon;
324
325
        return $this;
326
    }
327
328
    /**
329
     * Show/hide the UI item's title.
330
     *
331
     * @param  boolean $show Show (TRUE) or hide (FALSE) the title.
332
     * @return self
333
     */
334
    public function setShowTitle($show)
335
    {
336
        $this->showTitle = !!$show;
337
338
        return $this;
339
    }
340
341
    /**
342
     * Determine if the title is to be displayed.
343
     *
344
     * @return boolean If TRUE or unset, check if there is a title.
345
     */
346
    public function showTitle()
347
    {
348
        if ($this->showTitle === false) {
349
            return false;
350
        } else {
351
            return !!$this->title();
352
        }
353
    }
354
355
    /**
356
     * Show/hide the UI item's sub-title.
357
     *
358
     * @param  boolean $show Show (TRUE) or hide (FALSE) the sub-title.
359
     * @return self
360
     */
361
    public function setShowSubtitle($show)
362
    {
363
        $this->showSubtitle = !!$show;
364
365
        return $this;
366
    }
367
368
    /**
369
     * Determine if the sub-title is to be displayed.
370
     *
371
     * @return boolean If TRUE or unset, check if there is a sub-title.
372
     */
373
    public function showSubtitle()
374
    {
375
        if ($this->showSubtitle === false) {
376
            return false;
377
        } else {
378
            return !!$this->subtitle();
379
        }
380
    }
381
382
    /**
383
     * Show/hide the UI item's description.
384
     *
385
     * @param  boolean $show Show (TRUE) or hide (FALSE) the description.
386
     * @return self
387
     */
388
    public function setShowDescription($show)
389
    {
390
        $this->showDescription = !!$show;
391
392
        return $this;
393
    }
394
395
    /**
396
     * Determine if the description is to be displayed.
397
     *
398
     * @return boolean If TRUE or unset, check if there is a description.
399
     */
400
    public function showDescription()
401
    {
402
        if ($this->showDescription === false) {
403
            return false;
404
        } else {
405
            return !!$this->description();
406
        }
407
    }
408
409
    /**
410
     * Show/hide the UI item's notes.
411
     *
412
     * @param  boolean $show Show (TRUE) or hide (FALSE) the notes.
413
     * @return self
414
     */
415
    public function setShowNotes($show)
416
    {
417
        $this->showNotes = !!$show;
418
419
        return $this;
420
    }
421
422
    /**
423
     * Determine if the notes is to be displayed.
424
     *
425
     * @return boolean If TRUE or unset, check if there are notes.
426
     */
427
    public function showNotes()
428
    {
429
        if ($this->showNotes === false) {
430
            return false;
431
        } else {
432
            return !!$this->notes();
433
        }
434
    }
435
436
    /**
437
     * Show/hide the UI item's icon.
438
     *
439
     * @param  boolean $show Show (TRUE) or hide (FALSE) the icon.
440
     * @return self
441
     */
442
    public function setShowIcon($show)
443
    {
444
        $this->showIcon = !!$show;
445
446
        return $this;
447
    }
448
449
    /**
450
     * Determine if the icon is to be displayed.
451
     *
452
     * @return boolean If TRUE or unset, check if there is an icon.
453
     */
454
    public function showIcon()
455
    {
456
        if ($this->showIcon === false) {
457
            return false;
458
        } else {
459
            return !!$this->icon();
460
        }
461
    }
462
463
    /**
464
     * Show/hide the UI item's header.
465
     *
466
     * @param  boolean $show Show (TRUE) or hide (FALSE) the header.
467
     * @return self
468
     */
469
    public function setShowHeader($show)
470
    {
471
        $this->showHeader = !!$show;
472
473
        return $this;
474
    }
475
476
    /**
477
     * Determine if the header is to be displayed.
478
     *
479
     * @return boolean If TRUE or unset, check if there is a title.
480
     */
481
    public function showHeader()
482
    {
483
        if ($this->showHeader === false) {
484
            return false;
485
        } else {
486
            return $this->showTitle();
487
        }
488
    }
489
490
    /**
491
     * Show/hide the UI item's footer.
492
     *
493
     * @param  boolean $show Show (TRUE) or hide (FALSE) the footer.
494
     * @return self
495
     */
496
    public function setShowFooter($show)
497
    {
498
        $this->showFooter = !!$show;
499
500
        return $this;
501
    }
502
503
    /**
504
     * Determine if the footer is to be displayed.
505
     *
506
     * @return boolean If TRUE or unset, check if there are notes.
507
     */
508
    public function showFooter()
509
    {
510
        if ($this->showFooter === false) {
511
            return false;
512
        } else {
513
            return $this->showNotes();
514
        }
515
    }
516
517
    /**
518
     * Comparison function used by {@see uasort()}.
519
     *
520
     * @param  PrioritizableInterface $a Sortable entity A.
521
     * @param  PrioritizableInterface $b Sortable entity B.
522
     * @return integer Sorting value: -1 or 1.
523
     */
524
    protected function sortItemsByPriority(
525
        PrioritizableInterface $a,
526
        PrioritizableInterface $b
527
    ) {
528
        $priorityA = $a->priority();
529
        $priorityB = $b->priority();
530
531
        if ($priorityA === $priorityB) {
532
            return 0;
533
        }
534
        return ($priorityA < $priorityB) ? (-1) : 1;
535
    }
536
537
    /**
538
     * All UI objects are translatable, therefore are translator-aware.
539
     *
540
     * @return \Charcoal\Translator\Translator
541
     */
542
    abstract protected function translator();
543
}
544