1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\collections\menus; |
4
|
|
|
|
5
|
|
|
use Ajax\common\html\HtmlDoubleElement; |
6
|
|
|
use Ajax\semantic\html\base\constants\Direction; |
7
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
8
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
9
|
|
|
use Ajax\semantic\html\base\constants\Wide; |
10
|
|
|
use Ajax\common\html\html5\HtmlImg; |
11
|
|
|
use Ajax\semantic\html\modules\HtmlDropdown; |
12
|
|
|
use Ajax\common\html\BaseHtml; |
13
|
|
|
use Ajax\semantic\html\modules\HtmlPopup; |
14
|
|
|
use Ajax\semantic\html\elements\HtmlIcon; |
15
|
|
|
use Ajax\semantic\html\elements\html5\HtmlLink; |
16
|
|
|
use Ajax\semantic\html\elements\HtmlInput; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Semantic Menu component |
20
|
|
|
* @see http://semantic-ui.com/collections/menu.html |
21
|
|
|
* @author jc |
22
|
|
|
* @version 1.001 |
23
|
|
|
*/ |
24
|
|
|
class HtmlMenu extends HtmlSemCollection { |
25
|
|
|
private $_itemHeader; |
26
|
|
|
|
27
|
|
|
public function __construct($identifier, $items=array()) { |
28
|
|
|
parent::__construct($identifier, "div", "ui menu"); |
29
|
|
|
$this->addItems($items); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the menu type |
34
|
|
|
* @param string $type one of text,item |
35
|
|
|
* @return \Ajax\semantic\html\collections\HtmlMenu |
36
|
|
|
*/ |
37
|
|
|
public function setType($type="") { |
38
|
|
|
return $this->addToPropertyCtrl("class", $type, array ("","item","text" )); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setActiveItem($index) { |
42
|
|
|
$item=$this->getItem($index); |
43
|
|
|
if ($item !== null) { |
44
|
|
|
$item->addToProperty("class", "active"); |
45
|
|
|
} |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getItemToInsert($item) { |
50
|
|
|
if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon) { |
51
|
|
|
$itemO=new HtmlSemDoubleElement("item-" . $this->identifier, "div", ""); |
52
|
|
|
$itemO->setContent($item); |
53
|
|
|
$item=$itemO; |
54
|
|
|
} |
55
|
|
|
return $item; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function afterInsert($item) { |
59
|
|
|
if (!$item instanceof HtmlMenu) |
60
|
|
|
$item->addToPropertyCtrl("class", "item", array ("item" )); |
61
|
|
|
else { |
62
|
|
|
$this->setSecondary(); |
63
|
|
|
} |
64
|
|
|
return $item; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
* |
71
|
|
|
* @see \Ajax\common\html\html5\HtmlCollection::addItem() |
72
|
|
|
*/ |
73
|
|
|
public function addItem($item) { |
74
|
|
|
$item=parent::addItem($this->getItemToInsert($item)); |
75
|
|
|
return $this->afterInsert($item); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
* |
82
|
|
|
* @see \Ajax\common\html\HtmlCollection::insertItem() |
83
|
|
|
*/ |
84
|
|
|
public function insertItem($item, $position=0) { |
85
|
|
|
$item=parent::insertItem($this->getItemToInsert($item), $position); |
86
|
|
|
return $this->afterInsert($item); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function generateMenuAsItem($menu, $header=null) { |
90
|
|
|
$count=$this->count(); |
91
|
|
|
$item=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "div"); |
92
|
|
|
if (isset($header)) { |
93
|
|
|
$headerItem=new HtmlSemDoubleElement("item-header-" . $this->identifier . "-" . $count, "div", "header"); |
94
|
|
|
$headerItem->setContent($header); |
95
|
|
|
$item->addContent($headerItem); |
96
|
|
|
$this->_itemHeader=$headerItem; |
97
|
|
|
} |
98
|
|
|
$menu->setClass("menu"); |
99
|
|
|
$item->addContent($menu); |
100
|
|
|
return $item; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function addMenuAsItem($menu, $header=null) { |
104
|
|
|
return $this->addItem($this->generateMenuAsItem($menu, $header)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function addPopupAsItem($value, $identifier, $content="") { |
108
|
|
|
$value=new HtmlSemDoubleElement($identifier, "a", "browse item", $value); |
109
|
|
|
$value->addContent(new HtmlIcon("", "dropdown")); |
110
|
|
|
$value=$this->addItem($value); |
111
|
|
|
$popup=new HtmlPopup($value, "popup-" . $this->identifier . "-" . $this->count(), $content); |
112
|
|
|
$popup->setFlowing()->setPosition("bottom left")->setOn("click"); |
113
|
|
|
$this->wrap("", $popup); |
114
|
|
|
return $popup; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function addDropdownAsItem($value, $items=NULL) { |
118
|
|
|
$dd=$value; |
119
|
|
|
if (\is_string($value)) { |
120
|
|
|
$dd=new HtmlDropdown("dropdown-" . $this->identifier . "-" . $this->count(), $value, $items); |
121
|
|
|
} |
122
|
|
|
return $this->addItem($dd); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
* |
129
|
|
|
* @see \Ajax\common\html\html5\HtmlCollection::createItem() |
130
|
|
|
*/ |
131
|
|
|
protected function createItem($value) { |
132
|
|
|
$itemO=new HtmlLink("item-" . \sizeof($this->content), "", $value); |
133
|
|
|
return $itemO->setClass("item"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setInverted() { |
137
|
|
|
return $this->addToProperty("class", "inverted"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setSecondary() { |
141
|
|
|
return $this->addToProperty("class", "secondary"); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setVertical() { |
145
|
|
|
return $this->addToPropertyCtrl("class", "vertical", array ("vertical" )); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setPosition($value="right") { |
149
|
|
|
return $this->addToPropertyCtrl("class", $value, array ("right","left" )); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function setPointing($value=Direction::NONE) { |
153
|
|
|
return $this->addToPropertyCtrl("class", $value . " pointing", Direction::getConstantValues("pointing")); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function asTab($vertical=false) { |
157
|
|
|
$this->apply(function (HtmlDoubleElement &$item) { |
158
|
|
|
$item->setTagName("a"); |
159
|
|
|
}); |
160
|
|
|
if ($vertical === true) |
161
|
|
|
$this->setVertical(); |
162
|
|
|
return $this->addToProperty("class", "tabular"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function asPagination() { |
166
|
|
|
$this->apply(function (HtmlDoubleElement &$item) { |
167
|
|
|
$item->setTagName("a"); |
168
|
|
|
}); |
169
|
|
|
return $this->addToProperty("class", "pagination"); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function setFixed() { |
173
|
|
|
return $this->addToProperty("class", "fixed"); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setFluid() { |
177
|
|
|
return $this->addToProperty("class", "fluid"); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function setCompact() { |
181
|
|
|
return $this->addToProperty("class", "compact"); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/* |
185
|
|
|
* (non-PHPdoc) |
186
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
187
|
|
|
*/ |
188
|
|
|
public function fromDatabaseObject($object, $function) { |
189
|
|
|
$return=$function($object); |
190
|
|
|
if (\is_array($return)) |
191
|
|
|
$this->addItems($return); |
192
|
|
|
else |
193
|
|
|
$this->addItem($return); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Defines the menu width |
198
|
|
|
* @param int $width |
199
|
|
|
* @return \Ajax\semantic\html\collections\menus\HtmlMenu |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function setWidth($width) { |
|
|
|
|
202
|
|
|
if (\is_int($width)) { |
203
|
|
|
$width=Wide::getConstants()["W" . $width]; |
204
|
|
|
} |
205
|
|
|
$this->addToPropertyCtrl("class", $width, Wide::getConstants()); |
206
|
|
|
return $this->addToPropertyCtrl("class", "item", array ("item" )); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function addImage($identifier, $src="", $alt="") { |
210
|
|
|
return $this->addItem(new HtmlImg($identifier, $src, $alt)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public static function vertical($identifier, $items=array()) { |
214
|
|
|
return (new HtmlMenu($identifier, $items))->setVertical(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getItemHeader() { |
218
|
|
|
return $this->_itemHeader; |
219
|
|
|
} |
220
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.