Completed
Push — master ( fdf098...e2a56b )
by Chauncey
10:31
created

UiItemTrait::showIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Charcoal\Ui;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Provides an implementation of {@see \Charcoal\Ui\UiItemInterface}.
9
 */
10
trait UiItemTrait
11
{
12
    /**
13
     * The UI item type.
14
     *
15
     * @var string|null
16
     */
17
    private $type;
18
19
    /**
20
     * The UI item's template.
21
     *
22
     * @var string|null
23
     */
24
    private $template;
25
26
    /**
27
     * The UI item's icon.
28
     *
29
     * Note: Only icons from the {@link http://fontawesome.io/ Font Awesome}
30
     * library are supported.
31
     *
32
     * @var string|null
33
     */
34
    private $icon;
35
36
    /**
37
     * The UI item's title.
38
     *
39
     * @var \Charcoal\Translator\Translation
40
     */
41
    private $title = '';
42
43
    /**
44
     * The UI item's sub-title.
45
     *
46
     * @var \Charcoal\Translator\Translation
47
     */
48
    private $subtitle = '';
49
50
    /**
51
     * The UI item's description.
52
     *
53
     * @var \Charcoal\Translator\Translation
54
     */
55
    private $description = '';
56
57
    /**
58
     * The UI item's notes.
59
     *
60
     * @var \Charcoal\Translator\Translation
61
     */
62
    private $notes = '';
63
64
    /**
65
     * The title is displayed by default.
66
     *
67
     * @var boolean
68
     */
69
    private $showTitle = true;
70
71
    /**
72
     * The sub-title is displayed by default.
73
     *
74
     * @var boolean
75
     */
76
    private $showSubtitle = true;
77
78
    /**
79
     * The description is displayed by default.
80
     *
81
     * @var boolean
82
     */
83
    private $showDescription = true;
84
85
    /**
86
     * The notes are displayed by default.
87
     *
88
     * @var boolean
89
     */
90
    private $showNotes = true;
91
92
    /**
93
     * The icon is displayed by default.
94
     *
95
     * @var boolean
96
     */
97
    private $showIcon = true;
98
99
    /**
100
     * The header is displayed by default.
101
     *
102
     * @var boolean
103
     */
104
    private $showHeader = true;
105
106
    /**
107
     * The footer is displayed by default.
108
     *
109
     * @var boolean
110
     */
111
    private $showFooter = true;
112
113
    /**
114
     * Set the UI item type.
115
     *
116
     * @param string|null $type The UI item type.
117
     * @throws InvalidArgumentException If the type is not a string.
118
     * @return UiItemInterface Chainable
119
     */
120 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...
121
    {
122
        if (is_string($type) || $type === null) {
123
            $this->type = $type;
124
        } else {
125
            throw new InvalidArgumentException(
126
                'Can not set UI item config type: Type must be a string or NULL'
127
            );
128
        }
129
130
        return $this;
131
    }
132
133
    /**
134
     * Retrieve the UI item type.
135
     *
136
     * @return string
137
     */
138
    public function type()
139
    {
140
        return $this->type;
141
    }
142
143
    /**
144
     * Set the UI item's template.
145
     *
146
     * Usually, a path to a file containing the template to be rendered.
147
     *
148
     * @param string $template A template (identifier).
149
     * @throws InvalidArgumentException If the template is not a string.
150
     * @return UiItemInterface Chainable
151
     */
152
    public function setTemplate($template)
153
    {
154
        if (!is_string($template)) {
155
            throw new InvalidArgumentException(
156
                'The UI Item can not set the template, must be a string'
157
            );
158
        }
159
160
        $this->template = $template;
161
162
        return $this;
163
    }
164
165
    /**
166
     * Retrieve the UI item's template.
167
     *
168
     * @return string If unset, returns the UI item type.
169
     */
170
    public function template()
171
    {
172
        if ($this->template === null) {
173
            return $this->type();
174
        }
175
176
        return $this->template;
177
    }
178
179
    /**
180
     * Set the UI item's title.
181
     *
182
     * @param mixed $title A title.
183
     * @return UiItemInterface Chainable
184
     */
185
    public function setTitle($title)
186
    {
187
        $this->title = $this->translator()->translation($title);
188
        return $this;
189
    }
190
191
    /**
192
     * Retrieve the title.
193
     *
194
     * @return \Charcoal\Translator\Translation|null
195
     */
196
    public function title()
197
    {
198
        return $this->title;
199
    }
200
201
    /**
202
     * Set the UI item's sub-title.
203
     *
204
     * @param mixed $subtitle A sub-title.
205
     * @return UiItemInterface Chainable
206
     */
207
    public function setSubtitle($subtitle)
208
    {
209
        $this->subtitle = $this->translator()->translation($subtitle);
210
        return $this;
211
    }
212
213
    /**
214
     * Retrieve the sub-title.
215
     *
216
     * @return \Charcoal\Translator\Translation|null
217
     */
218
    public function subtitle()
219
    {
220
        return $this->subtitle;
221
    }
222
223
    /**
224
     * Set the UI item's description.
225
     *
226
     * @param mixed $description A description.
227
     * @return UiItemInterface Chainable
228
     */
229
    public function setDescription($description)
230
    {
231
        $this->description = $this->translator()->translation($description);
232
        return $this;
233
    }
234
235
    /**
236
     * Retrieve the description.
237
     *
238
     * @return \Charcoal\Translator\Translation|null
239
     */
240
    public function description()
241
    {
242
        return $this->description;
243
    }
244
245
    /**
246
     * Set notes about the UI item.
247
     *
248
     * @param mixed $notes Notes.
249
     * @return UiItemInterface Chainable
250
     */
251
    public function setNotes($notes)
252
    {
253
        $this->notes = $this->translator()->translation($notes);
254
        return $this;
255
    }
256
257
    /**
258
     * Retrieve the notes.
259
     *
260
     * @return \Charcoal\Translator\Translation|null
261
     */
262
    public function notes()
263
    {
264
        return $this->notes;
265
    }
266
267
    /**
268
     * Retrieve the path to the item's icon.
269
     *
270
     * @todo  [mcaskill 2016-09-16] Move this to a tab interface in charcoal-admin
271
     *     so as to focus the icon getter/setter on being a Glyphicon.
272
     * @return string
273
     */
274
    public function icon()
275
    {
276
        return $this->icon;
277
    }
278
279
    /**
280
     * Set the path to the item's icon associated with the object.
281
     *
282
     * @param  string $icon A path to an image.
283
     * @return UiItemInterface Chainable
284
     */
285
    public function setIcon($icon)
286
    {
287
        $this->icon = $icon;
288
289
        return $this;
290
    }
291
292
    /**
293
     * Show/hide the UI item's title.
294
     *
295
     * @param boolean $show Show (TRUE) or hide (FALSE) the title.
296
     * @return UiItemInterface Chainable
297
     */
298
    public function setShowTitle($show)
299
    {
300
        $this->showTitle = !!$show;
301
302
        return $this;
303
    }
304
305
    /**
306
     * Determine if the title is to be displayed.
307
     *
308
     * @return boolean If TRUE or unset, check if there is a title.
309
     */
310
    public function showTitle()
311
    {
312
        if ($this->showTitle === false) {
313
            return false;
314
        } else {
315
            return !!$this->title();
316
        }
317
    }
318
319
    /**
320
     * Show/hide the UI item's sub-title.
321
     *
322
     * @param boolean $show Show (TRUE) or hide (FALSE) the sub-title.
323
     * @return UiItemInterface Chainable
324
     */
325
    public function setShowSubtitle($show)
326
    {
327
        $this->showSubtitle = !!$show;
328
329
        return $this;
330
    }
331
332
    /**
333
     * Determine if the sub-title is to be displayed.
334
     *
335
     * @return boolean If TRUE or unset, check if there is a sub-title.
336
     */
337
    public function showSubtitle()
338
    {
339
        if ($this->showSubtitle === false) {
340
            return false;
341
        } else {
342
            return !!$this->subtitle();
343
        }
344
    }
345
346
    /**
347
     * Show/hide the UI item's description.
348
     *
349
     * @param boolean $show Show (TRUE) or hide (FALSE) the description.
350
     * @return UiItemInterface Chainable
351
     */
352
    public function setShowDescription($show)
353
    {
354
        $this->showDescription = !!$show;
355
356
        return $this;
357
    }
358
359
    /**
360
     * Determine if the description is to be displayed.
361
     *
362
     * @return boolean If TRUE or unset, check if there is a description.
363
     */
364
    public function showDescription()
365
    {
366
        if ($this->showDescription === false) {
367
            return false;
368
        } else {
369
            return !!$this->description();
370
        }
371
    }
372
373
    /**
374
     * Show/hide the UI item's notes.
375
     *
376
     * @param boolean $show Show (TRUE) or hide (FALSE) the notes.
377
     * @return UiItemInterface Chainable
378
     */
379
    public function setShowNotes($show)
380
    {
381
        $this->showNotes = !!$show;
382
383
        return $this;
384
    }
385
386
    /**
387
     * Determine if the notes is to be displayed.
388
     *
389
     * @return boolean If TRUE or unset, check if there are notes.
390
     */
391
    public function showNotes()
392
    {
393
        if ($this->showNotes === false) {
394
            return false;
395
        } else {
396
            return !!$this->notes();
397
        }
398
    }
399
400
    /**
401
     * Show/hide the UI item's icon.
402
     *
403
     * @param boolean $show Show (TRUE) or hide (FALSE) the icon.
404
     * @return UiItemInterface Chainable
405
     */
406
    public function setShowIcon($show)
407
    {
408
        $this->showIcon = !!$show;
409
410
        return $this;
411
    }
412
413
    /**
414
     * Determine if the icon is to be displayed.
415
     *
416
     * @return boolean If TRUE or unset, check if there is an icon.
417
     */
418
    public function showIcon()
419
    {
420
        if ($this->showIcon === false) {
421
            return false;
422
        } else {
423
            return !!$this->icon();
424
        }
425
    }
426
427
    /**
428
     * Show/hide the UI item's header.
429
     *
430
     * @param boolean $show Show (TRUE) or hide (FALSE) the header.
431
     * @return UiItemInterface Chainable
432
     */
433
    public function setShowHeader($show)
434
    {
435
        $this->showHeader = !!$show;
436
437
        return $this;
438
    }
439
440
    /**
441
     * Determine if the header is to be displayed.
442
     *
443
     * @return boolean If TRUE or unset, check if there is a title.
444
     */
445
    public function showHeader()
446
    {
447
        if ($this->showHeader === false) {
448
            return false;
449
        } else {
450
            return $this->showTitle();
451
        }
452
    }
453
454
    /**
455
     * Show/hide the UI item's footer.
456
     *
457
     * @param boolean $show Show (TRUE) or hide (FALSE) the footer.
458
     * @return UiItemInterface Chainable
459
     */
460
    public function setShowFooter($show)
461
    {
462
        $this->showFooter = !!$show;
463
464
        return $this;
465
    }
466
467
    /**
468
     * Determine if the footer is to be displayed.
469
     *
470
     * @return boolean If TRUE or unset, check if there are notes.
471
     */
472
    public function showFooter()
473
    {
474
        if ($this->showFooter === false) {
475
            return false;
476
        } else {
477
            return $this->showNotes();
478
        }
479
    }
480
481
    /**
482
     * @return \Charcoal\Translator\Translator
483
     */
484
    abstract protected function translator();
485
}
486