Completed
Push — master ( 138cdf...e53430 )
by Jean-Christophe
03:16
created

BaseTrait::addBehavior()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 10
nc 3
nop 5
1
<?php
2
3
namespace Ajax\semantic\html\base\traits;
4
5
use Ajax\semantic\html\base\constants\Size;
6
use Ajax\semantic\html\base\constants\Color;
7
use Ajax\semantic\html\base\constants\Direction;
8
use Ajax\semantic\html\elements\HtmlIcon;
9
use Ajax\service\JString;
10
11
/**
12
 * @author jc
13
 * @property string $identifier
14
 */
15
trait BaseTrait {
16
	protected $_variations=[ ];
17
	protected $_states=[ ];
18
	protected $_baseClass;
19
20
	abstract protected function setPropertyCtrl($name, $value, $typeCtrl);
21
22
	abstract protected function addToPropertyCtrl($name, $value, $typeCtrl);
23
24
	abstract protected function addToPropertyCtrlCheck($name, $value, $typeCtrl);
25
26
	abstract public function addToProperty($name, $value, $separator=" ");
27
28
	abstract public function setProperty($name, $value);
29
30
	abstract public function addContent($content,$before=false);
31
32
	abstract public function onCreate($jsCode);
33
34
	public function addVariation($variation) {
35
		return $this->addToPropertyCtrlCheck("class", $variation, $this->_variations);
36
	}
37
38
	public function addState($state) {
39
		return $this->addToPropertyCtrlCheck("class", $state, $this->_states);
40
	}
41
42
	public function setVariation($variation) {
43
		$this->setPropertyCtrl("class", $variation, $this->_variations);
44
		return $this->addToProperty("class", $this->_baseClass);
45
	}
46
47 View Code Duplication
	public function setVariations($variations) {
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...
48
		$this->setProperty("class", $this->_baseClass);
49
		if (\is_string($variations))
50
			$variations=\explode(" ", $variations);
51
		foreach ( $variations as $variation ) {
52
			$this->addVariation($variation);
53
		}
54
		return $this;
55
	}
56
57
	public function setState($state) {
58
		$this->setPropertyCtrl("class", $state, $this->_states);
59
		return $this->addToProperty("class", $this->_baseClass);
60
	}
61
62
	public function addVariations($variations=array()) {
63
		if (\is_string($variations))
64
			$variations=\explode(" ", $variations);
65
		foreach ( $variations as $variation ) {
66
			$this->addVariation($variation);
67
		}
68
		return $this;
69
	}
70
71
	public function addStates($states=array()) {
72
		if (\is_string($states))
73
			$states=\explode(" ", $states);
74
		foreach ( $states as $state ) {
75
			$this->addState($state);
76
		}
77
		return $this;
78
	}
79
80 View Code Duplication
	public function setStates($states) {
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...
81
		$this->setProperty("class", $this->_baseClass);
82
		if (\is_string($states))
83
			$states=\explode(" ", $states);
84
		foreach ( $states as $state ) {
85
			$this->addState($state);
86
		}
87
		return $this;
88
	}
89
90
	public function addIcon($icon, $before=true) {
91
		return $this->addContent(new HtmlIcon("icon-" . $this->identifier, $icon), $before);
92
	}
93
94
	public function addSticky($context="body"){
95
		$this->onCreate("$('#".$this->identifier."').sticky({ context: '".$context."'});");
96
		return $this;
97
	}
98
99
	/**
100
	 *
101
	 * {@inheritDoc}
102
	 *
103
	 * @see \Ajax\common\html\HtmlSingleElement::setSize()
104
	 */
105
	public function setSize($size) {
106
		return $this->addToPropertyCtrl("class", $size, Size::getConstants());
107
	}
108
109
	/**
110
	 * show it is currently unable to be interacted with
111
	 * @param boolean $disable
112
	 * @return \Ajax\semantic\html\elements\HtmlSemDoubleElement
113
	 */
114
	public function setDisabled($disable=true) {
115
		if($disable)
116
			$this->addToProperty("class", "disabled");
117
		return $this;
118
	}
119
120
	/**
121
	 *
122
	 * @param string $color
123
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
124
	 */
125
	public function setColor($color) {
126
		return $this->addToPropertyCtrl("class", $color, Color::getConstants());
127
	}
128
129
	/**
130
	 *
131
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
132
	 */
133
	public function setFluid() {
134
		return $this->addToProperty("class", "fluid");
135
	}
136
137
	/**
138
	 *
139
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
140
	 */
141
	public function asHeader(){
142
		return $this->addToProperty("class", "header");
143
	}
144
145
	/**
146
	 * show it is currently the active user selection
147
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
148
	 */
149
	public function setActive($value=true){
150
		if($value)
151
			$this->addToProperty("class", "active");
152
		return $this;
153
	}
154
155
	public function setAttached($value=true){
156
		if($value)
157
			$this->addToPropertyCtrl("class", "attached", array ("attached" ));
158
		return $this;
159
	}
160
161
	/**
162
	 * can be formatted to appear on dark backgrounds
163
	 */
164
	public function setInverted() {
165
		return $this->addToProperty("class", "inverted");
166
	}
167
168
	public function setCircular() {
169
		return $this->addToProperty("class", "circular");
170
	}
171
172
	public function setFloated($direction="right") {
173
		return $this->addToPropertyCtrl("class", $direction . " floated", Direction::getConstantValues("floated"));
174
	}
175
176
	public function floatRight() {
177
		return $this->setFloated();
178
	}
179
180
	public function floatLeft() {
181
		return $this->setFloated("left");
182
	}
183
184
	public function getBaseClass() {
185
		return $this->_baseClass;
186
	}
187
188
	protected function addBehavior(&$array,$key,$value,$before="",$after=""){
189
		if(\is_string($value)){
190
			if(isset($array[$key])){
191
				$p=JString::replaceAtFirstAndLast($array[$key], $before, "", $after, "");
192
				$array[$key]=$before.$p.$value.$after;
193
			}else
194
				$array[$key]=$before.$value.$after;
195
		}else
196
			$array[$key]=$value;
197
		return $this;
198
	}
199
}