-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDescriptionList.php
136 lines (118 loc) · 3.87 KB
/
DescriptionList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace demogorgorn\uikit;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* DescriptionList renders a Description list UIKit component, a nicely looking description list, which comes in different styles.
*
* For example:
*
* ```php
* echo DescriptionList::widget([
* 'isHorizontal' => true,
* 'items' => [
* [
* 'term' => 'Home',
* 'description' => 'Some text',
* ],
* [
* 'term' => 'Lorem ipsum',
* 'description' => 'Dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
* ],
* ],
* ]);
* ```
*
* @see http://www.getuikit.com/docs/description-list.html
*
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*/
class DescriptionList extends Widget
{
/**
* @var array list of items in the description list widget. Each array element represents a single
* item which can be an array with the following structure:
*
* - term: string, required, the title of the item. It can contain everything.
* - description: string, required, the list's description. It can contain everything.
* - termOptions: array, optional, the HTML attributes of the item's term (DT).
* - descriptionOptions: array, optional, the HTML attributes of the item's description (DD).
*/
public $items = [];
/**
* @var bool display terms and descriptions side by side.
*/
public $isHorizontal = false;
/**
* @var bool separate the description list items with lines.
*/
public $addLine = false;
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, 'uk-description-list');
if ($this->isHorizontal)
Html::addCssClass($this->options, 'uk-description-list-horizontal');
if ($this->addLine) {
if ($this->isHorizontal)
throw new InvalidConfigException("The options 'isHorizontal' and 'addLine' of the Description lists component are not combinable with each other.");
Html::addCssClass($this->options, 'uk-description-list-line');
}
}
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderItems();
$this->registerAsset();
}
/**
* Renders widget items.
*/
public function renderItems()
{
$items = [];
foreach ($this->items as $i => $item) {
$items[] = $this->renderTermItem($item);
$items[] = $this->renderDescriptionItem($item);
}
return Html::tag('dl', implode("\n", $items), $this->options);
}
/**
* Renders a term item.
* @param string|array $item the item to render.
* @return string the rendering result.
* @throws InvalidConfigException
*/
public function renderTermItem($item)
{
if (!isset($item['term'])) {
throw new InvalidConfigException("The 'term' option is required.");
}
$term = $item['term'];
$termOptions = ArrayHelper::getValue($item, 'termOptions', []);
return Html::tag('dt', $term, $termOptions);
}
/**
* Renders a description item.
* @param string|array $item the item to render.
* @return string the rendering result.
* @throws InvalidConfigException
*/
public function renderDescriptionItem($item)
{
if (!isset($item['description'])) {
throw new InvalidConfigException("The 'description' option is required.");
}
$description = $item['description'];
$descriptionOptions = ArrayHelper::getValue($item, 'descriptionOptions', []);
return Html::tag('dd', $description, $descriptionOptions);
}
}