Completed
Push — master ( 8f54cc...872df9 )
by Jean-Christophe
03:13
created

HtmlButtonGroups::addDropdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
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
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
		return $this->addElement($dd);
36
	}
37
38
39
	public function addElement($element, $asIcon=false) {
40
		$item=$this->addItem($element);
41
		if($asIcon)
42
			$item->asIcon($element);
43
		return $item;
44
	}
45
46
	public function addElements($elements, $asIcons=false) {
47
		foreach ( $elements as $element ) {
48
			$this->addElement($element, $asIcons);
49
		}
50
		return $this;
51
	}
52
53
	public function insertOr($aferIndex=0, $or="or") {
54
		$orElement=new HtmlSemDoubleElement("", "div", "or");
55
		$orElement->setProperty("data-text", $or);
56
		array_splice($this->content, $aferIndex + 1, 0, array ($orElement ));
57
		return $this;
58
	}
59
60
	/*
61
	 * (non-PHPdoc)
62
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
63
	 */
64
	public function fromArray($array) {
65
		$this->addElements($array);
66
	}
67
68
	public function asIcons() {
69
		foreach ( $this->content as $item ) {
70
			if($item instanceof HtmlButton)
71
			$item->asIcon($item->getContent());
72
		}
73
		return $this->addToProperty("class", "icons");
74
	}
75
76
	public function setVertical() {
77
		return $this->addToProperty("class", "vertical");
78
	}
79
80
	public function setLabeled() {
81
		return $this->addToProperty("class", "labeled icon");
82
	}
83
84
	/**
85
	 * Return the element at index
86
	 * @param int $index
87
	 * @return HtmlButton
88
	 */
89
	public function getElement($index) {
90
		return $this->getItem($index);
91
	}
92
93
	public function setElement($index, $button) {
94
		$this->setItem($index, $button);
95
		return $this;
96
	}
97
98
	/*
99
	 * (non-PHPdoc)
100
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
101
	 */
102
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
103
		foreach ( $this->content as $element ) {
104
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
105
		}
106
		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...
107
	}
108
109
	public function getElements() {
110
		return $this->content;
111
	}
112
113
	public function addClasses($classes=array()) {
114
		$i=0;
115
		if(!\is_array($classes)){
116
			$classes=array_fill (0,$this->count(),$classes);
117
		}
118
		foreach ( $this->content as $button ) {
119
			$button->addToProperty("class", $classes[$i++]);
120
		}
121
		return $this;
122
	}
123
124
	/*
125
	 * (non-PHPdoc)
126
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
127
	 */
128
	public function fromDatabaseObject($object, $function) {
129
		$this->addElement($function($object));
130
	}
131
132
	public function run(JsUtils $js){
133
		$result= parent::run($js);
134
		return $result->setItemSelector(".ui.button");
135
	}
136
}