Completed
Push — master ( 4bf172...5f7aee )
by Jean-Christophe
03:35
created

HtmlButtonGroups::getDropdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ajax\semantic\html\elements\HtmlButtonGroups) is incompatible with the return type of the parent method Ajax\common\html\BaseHtml::on of type Ajax\common\html\traits\BaseHtmlEventsTrait.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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
}