|
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
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Semantic UI Buttongroups component |
|
11
|
|
|
* @see http://semantic-ui.com/elements/button.html |
|
12
|
|
|
* @author jc |
|
13
|
|
|
* @version 1.001 |
|
14
|
|
|
*/ |
|
15
|
|
|
class HtmlButtonGroups extends HtmlSemCollection { |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($identifier, $elements=array(), $asIcons=false) { |
|
18
|
|
|
parent::__construct($identifier, "div", "ui buttons"); |
|
19
|
|
|
if ($asIcons === true) |
|
20
|
|
|
$this->asIcons(); |
|
21
|
|
|
$this->addElements($elements, $asIcons); |
|
22
|
|
|
} |
|
23
|
|
|
protected function createItem($value){ |
|
24
|
|
|
return new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content), $value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function addElement($element, $asIcon=false) { |
|
29
|
|
|
$item=$this->addItem($element); |
|
30
|
|
|
if($asIcon) |
|
31
|
|
|
$item->asIcon($element); |
|
32
|
|
|
return $item; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function addElements($elements, $asIcons=false) { |
|
36
|
|
|
foreach ( $elements as $element ) { |
|
37
|
|
|
$this->addElement($element, $asIcons); |
|
38
|
|
|
} |
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function insertOr($aferIndex=0, $or="or") { |
|
43
|
|
|
$orElement=new HtmlSemDoubleElement("or-" . $this->identifier, "div", "or"); |
|
44
|
|
|
$orElement->setProperty("data-text", $or); |
|
45
|
|
|
array_splice($this->content, $aferIndex + 1, 0, array ($orElement )); |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/* |
|
50
|
|
|
* (non-PHPdoc) |
|
51
|
|
|
* @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray() |
|
52
|
|
|
*/ |
|
53
|
|
|
public function fromArray($array) { |
|
54
|
|
|
$this->addElements($array); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function asIcons() { |
|
58
|
|
|
foreach ( $this->content as $item ) { |
|
59
|
|
|
if($item instanceof HtmlButton) |
|
60
|
|
|
$item->asIcon($item->getContent()); |
|
61
|
|
|
} |
|
62
|
|
|
return $this->addToProperty("class", "icons"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setVertical() { |
|
66
|
|
|
return $this->addToProperty("class", "vertical"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function setLabeled() { |
|
70
|
|
|
return $this->addToProperty("class", "labeled icon"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return the element at index |
|
75
|
|
|
* @param int $index |
|
76
|
|
|
* @return HtmlButton |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getElement($index) { |
|
79
|
|
|
return $this->getItem($index); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setElement($index, $button) { |
|
83
|
|
|
$this->setItem($index, $button); |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/* |
|
88
|
|
|
* (non-PHPdoc) |
|
89
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::on() |
|
90
|
|
|
*/ |
|
91
|
|
|
public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
|
92
|
|
|
foreach ( $this->content as $element ) { |
|
93
|
|
|
$element->on($event, $jsCode, $stopPropagation, $preventDefault); |
|
94
|
|
|
} |
|
95
|
|
|
return $this; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getElements() { |
|
99
|
|
|
return $this->content; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function addClasses($classes=array()) { |
|
103
|
|
|
$i=0; |
|
104
|
|
|
foreach ( $this->content as $button ) { |
|
105
|
|
|
$button->addToProperty("class", $classes[$i++]); |
|
106
|
|
|
} |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/* |
|
111
|
|
|
* (non-PHPdoc) |
|
112
|
|
|
* @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
|
113
|
|
|
*/ |
|
114
|
|
|
public function fromDatabaseObject($object, $function) { |
|
115
|
|
|
$this->addElement($function($object)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function run(JsUtils $js){ |
|
119
|
|
|
$result= parent::run($js); |
|
120
|
|
|
return $result->setItemSelector(".ui.button"); |
|
121
|
|
|
} |
|
122
|
|
|
} |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.