1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\elements; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
6
|
|
|
use Ajax\semantic\html\base\HtmlSemCollection; |
7
|
|
|
use Ajax\JsUtils; |
8
|
|
|
use Ajax\semantic\html\modules\HtmlDropdown; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Semantic UI Buttongroups component |
12
|
|
|
* @see http://semantic-ui.com/elements/button.html |
13
|
|
|
* @author jc |
14
|
|
|
* @version 1.001 |
15
|
|
|
*/ |
16
|
|
|
class HtmlButtonGroups extends HtmlSemCollection { |
17
|
|
|
protected $_dropdown; |
18
|
|
|
public function __construct($identifier, $elements=array(), $asIcons=false) { |
19
|
|
|
parent::__construct($identifier, "div", "ui buttons"); |
20
|
|
|
if ($asIcons === true) |
21
|
|
|
$this->asIcons(); |
22
|
|
|
$this->addElements($elements, $asIcons); |
23
|
|
|
} |
24
|
|
|
protected function createItem($value){ |
25
|
|
|
return new HtmlButton("", $value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function addDropdown($items,$asCombo=false){ |
29
|
|
|
$dd= new HtmlDropdown("dd-".$this->identifier,null,$items); |
30
|
|
|
$dd->asButton(); |
31
|
|
|
if($asCombo){ |
32
|
|
|
$dd->setAction("combo"); |
33
|
|
|
$dd->addToProperty("class", "combo"); |
34
|
|
|
} |
35
|
|
|
$this->_dropdown=$dd; |
36
|
|
|
return $this->addElement($dd); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public function addElement($element, $asIcon=false) { |
41
|
|
|
$item=$this->addItem($element); |
42
|
|
|
if($asIcon) |
43
|
|
|
$item->asIcon($element); |
44
|
|
|
return $item; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function addElements($elements, $asIcons=false) { |
48
|
|
|
foreach ( $elements as $element ) { |
49
|
|
|
$this->addElement($element, $asIcons); |
50
|
|
|
} |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function insertOr($aferIndex=0, $or="or") { |
55
|
|
|
$orElement=new HtmlSemDoubleElement("", "div", "or"); |
56
|
|
|
$orElement->setProperty("data-text", $or); |
57
|
|
|
array_splice($this->content, $aferIndex + 1, 0, array ($orElement )); |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/* |
62
|
|
|
* (non-PHPdoc) |
63
|
|
|
* @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
64
|
|
|
*/ |
65
|
|
|
public function fromArray($array) { |
66
|
|
|
$this->addElements($array); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function asIcons() { |
70
|
|
|
foreach ( $this->content as $item ) { |
71
|
|
|
if($item instanceof HtmlButton) |
72
|
|
|
$item->asIcon($item->getContent()); |
73
|
|
|
} |
74
|
|
|
return $this->addToProperty("class", "icons"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setVertical() { |
78
|
|
|
return $this->addToProperty("class", "vertical"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setLabeled() { |
82
|
|
|
return $this->addToProperty("class", "labeled icon"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the element at index |
87
|
|
|
* @param int $index |
88
|
|
|
* @return HtmlButton |
89
|
|
|
*/ |
90
|
|
|
public function getElement($index) { |
91
|
|
|
return $this->getItem($index); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setElement($index, $button) { |
95
|
|
|
$this->setItem($index, $button); |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* (non-PHPdoc) |
101
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::on() |
102
|
|
|
*/ |
103
|
|
|
public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
104
|
|
|
foreach ( $this->content as $element ) { |
105
|
|
|
$element->on($event, $jsCode, $stopPropagation, $preventDefault); |
106
|
|
|
} |
107
|
|
|
return $this; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getElements() { |
111
|
|
|
return $this->content; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function addClasses($classes=array()) { |
115
|
|
|
$i=0; |
116
|
|
|
if(!\is_array($classes)){ |
117
|
|
|
$classes=array_fill (0,$this->count(),$classes); |
118
|
|
|
} |
119
|
|
|
foreach ( $this->content as $button ) { |
120
|
|
|
$button->addToProperty("class", $classes[$i++]); |
121
|
|
|
} |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* (non-PHPdoc) |
127
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
128
|
|
|
*/ |
129
|
|
|
public function fromDatabaseObject($object, $function) { |
130
|
|
|
$this->addElement($function($object)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function run(JsUtils $js){ |
134
|
|
|
$result= parent::run($js); |
135
|
|
|
return $result->setItemSelector(".ui.button"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getDropdown() { |
139
|
|
|
return $this->_dropdown; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
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.