Completed
Push — master ( 489615...9fed17 )
by Jean-Christophe
03:15
created

HtmlButtongroups::addElements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\bootstrap\html\base\CssRef;
6
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
7
8
/**
9
 * Twitter Bootstrap Buttongroups component
10
 * @see http://getbootstrap.com/components/#btn-groups
11
 * @author jc
12
 * @version 1.001
13
 */
14
class HtmlButtongroups extends HtmlBsDoubleElement {
15
16
	/**
17
	 * @var array[HtmlButton]
18
	 */
19
	protected $elements;
20
21 View Code Duplication
	public function __construct($identifier, $elements=array(), $cssStyle=NULL, $size=NULL, $tagName="div") {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
		parent::__construct($identifier, $tagName);
23
		$this->_template="<%tagName% id='%identifier%' %properties%>%elements%</%tagName%>";
24
		$this->setProperty("class", "btn-group");
25
		$this->setRole("group");
26
		$this->addElements($elements);
27
		if (isset($cssStyle)) {
28
			$this->setStyle($cssStyle);
29
		}
30
		if (isset($size)) {
31
			$this->setSize($size);
32
		}
33
	}
34
35
	/**
36
	 * define the buttons size
37
	 * available values : "btn-group-lg","","btn-group-sm","btn-group-xs"
38
	 * @param string|int $size
39
	 * @return HtmlButtongroups default : ""
40
	 */
41
	public function setSize($size) {
42
		foreach ( $this->elements as $element ) {
43
			$element->setSize($size);
44
		}
45 View Code Duplication
		if (is_int($size)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
			return $this->addToPropertyUnique("class", CssRef::sizes("btn-group")[$size], CssRef::sizes("btn-group"));
47
		}
48
		return $this->addToPropertyCtrl("class", $size, CssRef::sizes("btn-group"));
49
	}
50
51
	public function setStyle($value) {
52
		foreach ( $this->elements as $element )
53
			$element->setStyle($value);
54
	}
55
56
	private function dropdownAsButton($bt) {
57
		$this->addExistingDropDown($bt);
58
		$bt->setTagName("button");
59
		$bt->addBtnClass("dropdown-toogle");
60
		$bt->addBtnClass("btn-default");
61
	}
62
63
	private function addExistingDropDown($bt) {
64
		$bt->setMTagName("div");
65
		$bt->setRole("group");
66
		$bt->setMClass("btn-group");
67
	}
68
69
	public function addElement($element) {
70
		$result=$element;
71
		$iid=sizeof($this->elements)+1;
72
		if (($element instanceof HtmlDropdown)||($element instanceof HtmlSplitbutton)) {
73
			$this->addExistingDropDown($element);
74
		} elseif (\is_array($element)) {
75
			$result=$this->_addArrayElement($element,$iid);
76
		} elseif (is_string($element)) {
77
			$result=new HtmlButton($this->identifier."-button-".$iid,$element);
78
		}
79
		if($result instanceof HtmlButton)
80
			$this->elements[]=$result;
81
		return $result;
82
	}
83
84
	private function _addArrayElement(array $element,int $iid){
85
		if (array_key_exists("glyph", $element))
86
			$bt=new HtmlGlyphButton($this->identifier."-button-".$iid);
87
		elseif (array_key_exists("btnCaption", $element)) {
88
			if (array_key_exists("split", $element))
89
				$bt=new HtmlSplitbutton($this->identifier."-dropdown-".$iid);
90
			else{
91
				$bt=new HtmlDropdown($this->identifier."-dropdown-".$iid);
92
				$this->dropdownAsButton($bt);
93
			}
94
		} else
95
			$bt=new HtmlButton($this->identifier."-button-".$iid);
96
		$bt->fromArray($element);
97
		return $bt;
98
	}
99
100
	public function addElements($elements) {
101
		foreach ( $elements as $element ) {
102
			$this->addElement($element);
103
		}
104
		return $this;
105
	}
106
107
	/*
108
	 * (non-PHPdoc)
109
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
110
	 */
111
	public function fromArray($array) {
112
		$this->addElements($array);
113
	}
114
115
	public function setAlignment($value) {
116
		if (is_int($value)) {
117
			$value=CssRef::alignment("btn-group")[$value];
118
		} else
119
			$value="btn-group-".$value;
120
		if (strstr($value, "justified")) {
121
			foreach ( $this->elements as $element ) {
122
				$element->wrap('<div class="btn-group" role="group">', '</div>');
123
			}
124
		}
125
		$this->addToPropertyCtrl("class", $value, CssRef::alignment("btn-group-"));
126
	}
127
128
	/**
129
	 * Return the element at index
130
	 * @param int $index
131
	 * @return HtmlButton
132
	 */
133
	public function getElement($index) {
134
		if (is_int($index))
135
			return $this->elements[$index];
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->elements[$index]; of type Ajax\bootstrap\html\Html...p\html\HtmlButtongroups adds the type Ajax\bootstrap\html\HtmlButtongroups to the return on line 135 which is incompatible with the return type documented by Ajax\bootstrap\html\HtmlButtongroups::getElement of type Ajax\bootstrap\html\HtmlButton.
Loading history...
136
		else {
137
			$elm=$this->getElementById($index, $this->elements);
138
			return $elm;
139
		}
140
	}
141
142
	public function setElement($index, $button) {
143
		$this->elements[$index]=$button;
144
		return $this;
145
	}
146
147
	/*
148
	 * (non-PHPdoc)
149
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
150
	 */
151
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
152
		foreach ( $this->elements as $element ) {
153
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
154
		}
155
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ajax\bootstrap\html\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...
156
	}
157
158
	public function getElements() {
159
		return $this->elements;
160
	}
161
162
	/* (non-PHPdoc)
163
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
164
	 */
165
	public function fromDatabaseObject($object, $function) {
166
		$this->addElement($function($object));
167
	}
168
169
}