HtmlButton::fromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\JsUtils;
6
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
7
use Ajax\bootstrap\html\base\CssRef;
8
9
/**
10
 * Twitter Bootstrap Button component
11
 * @see http://getbootstrap.com/css/#buttons
12
 * @author jc
13
 * @version 1.001
14
 */
15
class HtmlButton extends HtmlBsDoubleElement {
16
17
	/**
18
	 * Constructs an HTML Bootstrap button
19
	 * @param string $identifier HTML id
20
	 * @param string $value value of the Button
21
	 * @param string $cssStyle btn-default, btn-primary...
22
	 * @param string $onClick JS Code for click event
23
	 */
24 View Code Duplication
	public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) {
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...
25
		parent::__construct($identifier, "button");
26
		$this->setProperty("class", "btn btn-default");
27
		$this->setProperty("role", "button");
28
		$this->content=$value;
29
		if (isset($cssStyle)) {
30
			$this->setStyle($cssStyle);
31
		}
32
		if (isset($onClick)) {
33
			$this->onClick($onClick);
34
		}
35
	}
36
37
	/**
38
	 * Set the button value
39
	 * @param string $value
40
	 * @return \Ajax\bootstrap\html\HtmlButton
41
	 */
42
	public function setValue($value) {
43
		$this->content=$value;
44
		return $this;
45
	}
46
47
	/**
48
	 * define the button style
49
	 * avaible values : "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger"
50
	 * @param string|int $cssStyle
51
	 * @return \Ajax\bootstrap\html\HtmlButton default : "btn-default"
52
	 */
53
	public function setStyle($cssStyle) {
54
		return $this->addToPropertyCtrl("class", CssRef::getStyle($cssStyle, "btn"), CssRef::Styles("btn"));
55
	}
56
57
	/**
58
	 * define the button size
59
	 * available values : "btn-lg","","btn-sm","btn-xs"
60
	 * @param string|int $size
61
	 * @return \Ajax\bootstrap\html\HtmlButton default : ""
62
	 */
63
	public function setSize($size) {
64
		if (is_int($size)) {
65
			return $this->addToPropertyUnique("class", CssRef::sizes()[$size], CssRef::sizes());
66
		}
67
		return $this->addToPropertyCtrl("class", $size, CssRef::sizes());
68
	}
69
70
	/**
71
	 * Sets the "active" css class to the button
72
	 * @return \Ajax\bootstrap\html\HtmlButton
73
	 */
74
	public function setActive() {
75
		return $this->addToPropertyCtrl("class", "active", array (
76
				"active"
77
		));
78
	}
79
80
	/**
81
	 * Disables the button
82
	 * @return \Ajax\bootstrap\html\HtmlButton
83
	 */
84
	public function setDisabled() {
85
		return $this->addToPropertyCtrl("class", "disabled", array (
86
				"disabled"
87
		));
88
	}
89
90
	public function setToggled() {
91
		return $this->setProperty("data-toggle", "button");
92
	}
93
94
	/*
95
	 * (non-PHPdoc)
96
	 * @see BaseHtml::run()
97
	 */
98
	public function run(JsUtils $js) {
99
		$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
100
		if (array_key_exists("data-toggle", $this->properties)) {
101
			$this->_bsComponent->addCode("$.fn.toggled=function(){return this.hasClass('active');};");
102
		}
103
		$this->addEventsOnRun($js);
104
		return $this->_bsComponent;
105
	}
106
107
108
109
	/*
110
	 * (non-PHPdoc)
111
	 * @see \Ajax\bootstrap\html\BaseHtml::fromArray()
112
	 */
113
	public function fromArray($array) {
114
		$array=parent::fromArray($array);
115
		foreach ( $array as $key => $value ) {
116
			$this->setProperty($key, $value);
117
		}
118
		return $array;
119
	}
120
}