Completed
Push — master ( 3e8704...4367a8 )
by Jean-Christophe
03:21
created

HtmlButton::addLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
7
/**
8
 * Semantic Button component
9
 * @see http://semantic-ui.com/elements/button.html
10
 * @author jc
11
 * @version 1.001
12
 */
13
class HtmlButton extends HtmlSemDoubleElement {
14
15
	/**
16
	 * Constructs an HTML Semantic button
17
	 * @param string $identifier HTML id
18
	 * @param string $value value of the Button
19
	 * @param string $cssStyle btn-default, btn-primary...
20
	 * @param string $onClick JS Code for click event
21
	 */
22 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...
23
		parent::__construct($identifier, "button");
24
		$this->setProperty("class", "ui button");
25
		$this->content=$value;
26
		if (isset($cssStyle)) {
27
			$this->setStyle($cssStyle);
28
		}
29
		if (isset($onClick)) {
30
			$this->onClick($onClick);
31
		}
32
	}
33
34
	/**
35
	 * Set the button value
36
	 * @param string $value
37
	 * @return \Ajax\semantic\html\HtmlButton
38
	 */
39
	public function setValue($value) {
40
		$this->content=$value;
41
		return $this;
42
	}
43
44
	/**
45
	 * define the button style
46
	 * @param string|int $cssStyle
47
	 * @return \Ajax\semantic\html\HtmlButton default : ""
48
	 */
49
	public function setStyle($cssStyle) {
50
		return $this->addToProperty("class",$cssStyle);
51
	}
52
53
	public function setFocusable(){
54
		$this->setProperty("tabindex", "0");
55
	}
56
57
	public function setAnimated($content,$animation=""){
58
		$this->setTagName("div");
59
		$this->addToProperty("class", "animated ".$animation);
60
		$visible=new HtmlSemDoubleElement("visible-".$this->identifier,"div");
61
		$visible->setClass("visible content");
62
		$visible->setContent($this->content);
63
		$hidden=new HtmlSemDoubleElement("hidden-".$this->identifier,"div");
64
		$hidden->setClass("hidden content");
65
		$hidden->setContent($content);
66
		$this->content=$visible.$hidden;
67
	}
68
69
	/**
70
	 * Adds an icon before or after
71
	 * @param string|HtmlIcon $icon
72
	 * @param boolean $before
73
	 * @param boolean $labeled
74
	 * @return \Ajax\semantic\html\elements\HtmlIcon
75
	 */
76
	public function addIcon($icon,$before=true,$labeled=false){
77
		$iconO=$icon;
78
		if(\is_string($icon)){
79
			$iconO=new HtmlIcon("icon-".$this->identifier, $icon);
80
		}
81
		if($labeled!==false){
82
			$this->addToProperty("class", "labeled icon");
83
			$this->tagName="div";
84
		}
85
		$this->addContent($iconO,$before);
86
		return $iconO;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $iconO; of type string|Ajax\semantic\html\elements\HtmlIcon is incompatible with the return type documented by Ajax\semantic\html\elements\HtmlButton::addIcon of type Ajax\semantic\html\elements\HtmlIcon as it can also be of type string which is not included in this return type.
Loading history...
87
	}
88
89
	/**
90
	 * @param string|HtmlIcon $icon
91
	 * @return \Ajax\semantic\html\elements\HtmlButton
92
	 */
93
	public function asIcon($icon){
94
		$iconO=$icon;
95
		if(\is_string($icon)){
96
			$iconO=new HtmlIcon("icon-".$this->identifier, $icon);
97
		}
98
		$this->addToProperty("class", "icon");
99
		$this->content=$iconO;
100
		return $this;
101
	}
102
103
	/**
104
	 * Add and return a button label
105
	 * @param string $caption
106
	 * @param string $before
107
	 * @return \Ajax\semantic\html\elements\HtmlLabel
108
	 */
109
	public function addLabel($caption,$before=false){
110
		$this->tagName="div";
111
		$this->addToProperty("class", "labeled");
112
		$this->content=new HtmlButton("button-".$this->identifier,$this->content);
113
		$this->content->setTagName("div");
114
		$label=new HtmlLabel("label-".$this->identifier,$caption,"a");
115
		$label->setBasic();
116
		$this->addContent($label,$before);
0 ignored issues
show
Bug introduced by
It seems like $before defined by parameter $before on line 109 can also be of type string; however, Ajax\common\html\HtmlDoubleElement::addContent() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
117
		return $label;
118
	}
119
	/*
120
	 * (non-PHPdoc)
121
	 * @see \Ajax\common\html\BaseHtml::fromArray()
122
	 */
123
	public function fromArray($array) {
124
		$array=parent::fromArray($array);
125
		foreach ( $array as $key => $value ) {
126
			$this->setProperty($key, $value);
127
		}
128
		return $array;
129
	}
130
131
	/**
132
	 * can be formatted to appear on dark backgrounds
133
	 * @return \Ajax\semantic\html\elements\HtmlButton
134
	 */
135
	public function setInverted(){
136
		return $this->addToProperty("class", "inverted");
137
	}
138
139
	/**
140
	 *  show it is currently the active user selection
141
	 * @return \Ajax\semantic\html\elements\HtmlButton
142
	 */
143
	public function setActive(){
144
		return $this->addToProperty("class", "active");
145
	}
146
147
	/**
148
	 * show a loading indicator
149
	 * @return \Ajax\semantic\html\elements\HtmlButton
150
	 */
151
	public function asLoader(){
152
		return $this->addToProperty("class", "loading");
153
	}
154
155
	/**
156
	 * hint towards a positive consequence
157
	 * @return \Ajax\semantic\html\elements\HtmlButton
158
	 */
159
	public function setPositive(){
160
		return $this->addToProperty("class", "positive");
161
	}
162
163
	/**
164
	 * hint towards a negative consequence
165
	 * @return \Ajax\semantic\html\elements\HtmlButton
166
	 */
167
	public function setNegative(){
168
		return $this->addToProperty("class", "negative");
169
	}
170
171
	/**
172
	 * formatted to toggle on/off
173
	 * @return \Ajax\semantic\html\elements\HtmlButton
174
	 */
175
	public function setToggle(){
176
		return $this->addToProperty("class", "toggle");
177
	}
178
179
	/**
180
	 * @return \Ajax\semantic\html\elements\HtmlButton
181
	 */
182
	public function setFluid(){
183
		return $this->addToProperty("class", "fluid");
184
	}
185
186
	/**
187
	 * @return \Ajax\semantic\html\elements\HtmlButton
188
	 */
189
	public function setCircular(){
190
		return $this->addToProperty("class", "circular");
191
	}
192
193
	/**
194
	 *  button is less pronounced
195
	 * @return \Ajax\semantic\html\elements\HtmlButton
196
	 */
197
	public function setBasic(){
198
		return $this->addToProperty("class", "basic");
199
	}
200
}