HtmlDropdown::setMClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\JsUtils;
6
use Ajax\bootstrap\html\content\HtmlDropdownItem;
7
use Ajax\bootstrap\html\base\CssRef;
8
use Ajax\service\PhalconUtils;
9
10
/**
11
 * Twitter Bootstrap HTML Dropdown component
12
 * @author jc
13
 * @version 1.001
14
 */
15
class HtmlDropdown extends HtmlButton {
16
	protected $btnCaption="Dropdown button";
17
	protected $class="dropdown-toggle";
18
	protected $mClass="dropdown";
19
	protected $mTagName="div";
20
	protected $items=array ();
21
22
	/**
23
	 *
24
	 * @param string $identifier the id
25
	 */
26
	public function __construct($identifier, $value="", $items=array(), $cssStyle=null, $onClick=null) {
27
		parent::__construct($identifier, "", $cssStyle, $onClick);
28
		$this->_template=include 'templates/tplDropdown.php';
29
		$this->btnCaption=$value;
30
		$this->tagName="a";
31
		$this->fromArray($items);
32
		if ($cssStyle!==NULL) {
33
			$this->asButton($cssStyle);
34
		}
35
	}
36
37
	/**
38
	 * Define the tagName of the main element
39
	 * @param string $value default : div
40
	 */
41
	public function setMTagName($value) {
42
		$this->mTagName=$value;
43
	}
44
45
	/**
46
	 * define the button style
47
	 * avaible values : "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger"
48
	 * @param string|int $cssStyle
49
	 * @return \Ajax\bootstrap\html\HtmlDropdown default : "btn-default"
50
	 */
51
	public function setStyle($cssStyle) {
52
		if (is_int($cssStyle)) {
53
			return $this->addToMember($this->class, CssRef::buttonStyles()[$cssStyle]);
54
		}
55
		if (PhalconUtils::startsWith($cssStyle, "btn-")===false) {
56
			$cssStyle="btn".$cssStyle;
57
		}
58
		return $this->addToMemberCtrl($this->class, $cssStyle, CssRef::buttonStyles());
59
	}
60
61
	/*
62
	 * (non-PHPdoc)
63
	 * @see \Ajax\bootstrap\html\HtmlButton::setValue()
64
	 */
65
	public function setValue($value) {
66
		$this->btnCaption=$value;
67
	}
68
69
	/**
70
	 * define the buttons size
71
	 * available values : "btn-group-lg","","btn-group-sm","btn-group-xs"
72
	 * @param string|int $size
73
	 * @return HtmlDropdown
74
	 * default : ""
75
	 */
76
	public function setSize($size) {
77
		if (is_int($size)) {
78
			return $this->addToProperty("class", CssRef::sizes("btn-group")[$size]);
79
		}
80
		return $this->addToPropertyCtrl("class", $size, CssRef::sizes("btn-group"));
81
	}
82
83
	/**
84
	 * add an HtmlDropdownItem
85
	 * @param string $caption
86
	 * @param string $href
87
	 * @return HtmlDropdownItem
88
	 */
89
	public function addItem($caption, $href="#") {
90
		if($caption instanceof HtmlDropdownItem){
91
			$item=$caption;
92
		}else{
93
			$iid=$this->getItemsCount()+1;
94
			$item=new HtmlDropdownItem($this->identifier."-dropdown-item-".$iid);
95
			$item->setCaption($caption)->setHref($href);
96
		}
97
		$this->items []=$item;
98
		return $item;
99
	}
100
101
	public function addDivider() {
102
		return $this->addItem("-");
103
	}
104
105
	public function addHeader($caption) {
106
		return $this->addItem("-".$caption);
107
	}
108
109
	public function addItems($items) {
110
		$iid=$this->getItemsCount()+1;
111
		if (is_array($items)) {
112
			foreach ( $items as $item ) {
113
				if (is_string($item)) {
114
					$this->addItem($item);
115
				} else if (is_array($item)) {
116
					$dropDownItem=new HtmlDropdownItem($this->identifier."-dropdown-item-".$iid);
117
					$dropDownItem->fromArray($item);
118
					$this->items []=$dropDownItem;
119
				} else if ($item instanceof HtmlDropdownItem) {
120
					$this->items []=$item;
121
				}
122
			}
123
		}
124
		return $this;
125
	}
126
127
	/*
128
	 * (non-PHPdoc)
129
	 * @see BaseHtml::fromArray()
130
	 */
131
	public function fromArray($array) {
132
		if (array_keys($array)!==range(0, count($array)-1))
133
			return parent::fromArray($array);
134
		else
135
			return $this->addItems($array);
136
	}
137
138
	public function setItems($items) {
139
		$this->items=array ();
140
		$this->addItems($items);
141
	}
142
143
	/**
144
	 * Return the item at $index
145
	 * @param int $index
146
	 * @return HtmlDropdownItem
147
	 */
148
	public function getItem($index) {
149
		return $this->items [$index];
150
	}
151
152
	public function setBtnClass($value) {
153
		$this->class=$value;
154
	}
155
156
	public function setMClass($value) {
157
		$this->mClass=$value;
158
	}
159
160
	public function addBtnClass($value) {
161
		$this->addToMember($this->class, $value);
162
	}
163
164
	public function addmClass($value) {
165
		$this->addToMember($this->mClass, $value);
166
	}
167
168
	/*
169
	 * (non-PHPdoc)
170
	 * @see BaseHtml::run()
171
	 */
172 View Code Duplication
	public function run(JsUtils $js) {
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...
173
		if ($this->getProperty("role")==="nav") {
174
			foreach ( $this->items as $dropdownItem ) {
175
				$dropdownItem->runNav($js);
176
			}
177
		}
178
		$this->_bsComponent=$js->bootstrap()->dropdown("#".$this->identifier);
179
		$this->addEventsOnRun($js);
180
		return $this->_bsComponent;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_bsComponent; (Ajax\bootstrap\components\Dropdown) is incompatible with the return type of the parent method Ajax\bootstrap\html\HtmlButton::run of type Ajax\common\components\GenericComponent.

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...
181
	}
182
183
	/**
184
	 * Sets the tagName's dropdown
185
	 * @see \Ajax\bootstrap\html\BaseHtml::setTagName()
186
	 */
187
	public function setTagName($tagName) {
188
		if ($tagName=="button")
189
			$this->class="btn dropdown-toggle";
190
		return parent::setTagName($tagName);
191
	}
192
193
	public function __toString() {
194
		return $this->compile();
195
	}
196
197
	public function setBtnCaption($btnCaption) {
198
		$this->btnCaption=$btnCaption;
199
		return $this;
200
	}
201
202
	public function getItemsCount() {
203
		return sizeof($this->items);
204
	}
205
206
	public function setAlignment($alignment) {
207
		if (is_int($alignment))
208
			$alignment="dropdown-menu-".CssRef::alignment()[$alignment];
209
		return $this->addToMemberCtrl($this->class, $alignment, CssRef::alignment());
210
	}
211
212
	public function dropup() {
213
		$this->addToMember($this->mClass, "dropup");
214
	}
215
216
	public function getItems() {
217
		return $this->items;
218
	}
219
220
	public function asButton($cssStyle="btn-primary") {
221
		$this->setTagName("button");
222
		$this->setBtnClass("btn dropdown-toggle");
223
		$this->setStyle($cssStyle);
224
	}
225
226
	/**
227
	 * This event fires immediately when the show instance method is called.
228
	 * @param string $jsCode
229
	 * @return $this
230
	 */
231
	public function onShow($jsCode) {
232
		return $this->addEvent("show.bs.dropdown", $jsCode);
233
	}
234
235
	/**
236
	 * This event is fired when a dropdown element has been made visible to the user (will wait for CSS transitions to complete).
237
	 * @param string $jsCode
238
	 * @return $this
239
	 */
240
	public function onShown($jsCode) {
241
		return $this->addEvent("shown.bs.dropdown", $jsCode);
242
	}
243
244
	/**
245
	 * This event is fired immediately when the hide method has been called.
246
	 * @param string $jsCode
247
	 * @return $this
248
	 */
249
	public function onHide($jsCode) {
250
		return $this->addEvent("hide.bs.dropdown", $jsCode);
251
	}
252
253
	/**
254
	 * This event is fired when a dropdown element has been hidden from the user (will wait for CSS transitions to complete).
255
	 * @param string $jsCode
256
	 * @return $this
257
	 */
258
	public function onHidden($jsCode) {
259
		return $this->addEvent("hidden.bs.dropdown", $jsCode);
260
	}
261
262
263
	/* (non-PHPdoc)
264
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
265
	 */
266
	public function on($event, $jsCode, $stopPropagation = false, $preventDefault = false) {
267
		foreach ($this->items as $item){
268
			$item->on($event, $jsCode,$stopPropagation,$preventDefault);
269
		}
270
	}
271
272
	/* (non-PHPdoc)
273
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
274
	 */
275
	public function fromDatabaseObject($object, $function) {
276
		$this->addItem($function($object));
277
	}
278
279
}