Completed
Push — master ( a91c54...ab06fe )
by Jean-Christophe
03:07
created

BaseHtml::setWrapBefore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Ajax\common\html;
4
5
6
use Ajax\service\JString;
7
use Ajax\common\components\SimpleExtComponent;
8
use Ajax\JsUtils;
9
use Ajax\common\html\traits\BaseHtmlEventsTrait;
10
use Ajax\common\html\traits\BaseHtmlPropertiesTrait;
11
12
/**
13
 * BaseHtml for HTML components
14
 * @author jc
15
 * @version 1.3
16
 */
17
abstract class BaseHtml extends BaseWidget {
18
	use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait;
19
	protected $_template;
20
	protected $tagName;
21
	protected $_wrapBefore=array ();
22
	protected $_wrapAfter=array ();
23
	protected $_bsComponent;
24
	protected $_compiled=false;
25
	protected $_postCompile;
26
	protected $_preCompile;
27
28
	/**
29
	 *
30
	 * @param JsUtils $js
31
	 * @return SimpleExtComponent
32
	 */
33
	abstract public function run(JsUtils $js);
34
35
	private function _callSetter($setter,$key,$value,&$array){
36
		$result=false;
37
		if (method_exists($this, $setter) && !JString::startswith($key, "_")) {
38
			try {
39
				$this->$setter($value);
40
				unset($array[$key]);
41
				$result=true;
42
			} catch ( \Exception $e ) {
43
				$result=false;
44
			}
45
		}
46
		return $result;
47
	}
48
49
	protected function getTemplate(JsUtils $js=NULL) {
50
		return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js);
51
	}
52
53
	protected function ctrl($name, $value, $typeCtrl) {
54 View Code Duplication
		if (\is_array($typeCtrl)) {
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...
55
			if (array_search($value, $typeCtrl) === false) {
56
				throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}");
57
			}
58
		} else {
59
			if (!$typeCtrl($value)) {
60
				throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name);
61
			}
62
		}
63
		return true;
64
	}
65
66
67
68
	protected function setMemberCtrl(&$name, $value, $typeCtrl) {
69
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
70
			return $name=$value;
71
		}
72
		return $this;
73
	}
74
75
	protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") {
76
		if (\is_array($typeCtrl)) {
77
			$this->removeOldValues($name, $typeCtrl);
78
			$name.=$separator . $value;
79
		}
80
		return $this;
81
	}
82
83
84
85
	protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
86
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
87
			if (\is_array($typeCtrl)) {
88
				$this->removeOldValues($name, $typeCtrl);
89
			}
90
			$name.=$separator . $value;
91
		}
92
		return $this;
93
	}
94
95
	protected function addToMember(&$name, $value, $separator=" ") {
96
		$name=str_ireplace($value, "", $name) . $separator . $value;
97
		return $this;
98
	}
99
100
101
102
	protected function removeOldValues(&$oldValue, $allValues) {
103
		$oldValue=str_ireplace($allValues, "", $oldValue);
104
		$oldValue=trim($oldValue);
105
	}
106
107
	protected function _getElementBy($callback,$elements){
108
		if (\is_array($elements)) {
109
			$elements=\array_values($elements);
110
			$flag=false;
111
			$index=0;
112
			while ( !$flag && $index < sizeof($elements) ) {
113
				if ($elements[$index] instanceof BaseHtml)
114
					$flag=($callback($elements[$index]));
115
					$index++;
116
			}
117
			if ($flag === true)
118
				return $elements[$index - 1];
119
		} elseif ($elements instanceof BaseHtml) {
120
			if ($callback($elements))
121
				return $elements;
122
		}
123
		return null;
124
	}
125
126
	protected function setWrapBefore($wrapBefore) {
127
		$this->_wrapBefore=$wrapBefore;
128
		return $this;
129
	}
130
131
	protected function setWrapAfter($wrapAfter) {
132
		$this->_wrapAfter=$wrapAfter;
133
		return $this;
134
	}
135
136
	public function getTagName() {
137
		return $this->tagName;
138
	}
139
140
	public function setTagName($tagName) {
141
		$this->tagName=$tagName;
142
		return $this;
143
	}
144
145
	public function fromArray($array) {
146 View Code Duplication
		foreach ( $this as $key => $value ) {
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...
147
			if(array_key_exists($key, $array)===true)
148
				$this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array);
149
		}
150 View Code Duplication
		foreach ( $array as $key => $value ) {
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...
151
			if($this->_callSetter($key, $key, $value, $array)===false){
152
				$this->_callSetter("set" . ucfirst($key), $key, $value, $array);
153
			}
154
		}
155
		return $array;
156
	}
157
158
	public function fromDatabaseObjects($objects, $function) {
159
		if (isset($objects)) {
160
			foreach ( $objects as $object ) {
161
				$this->fromDatabaseObject($object, $function);
162
			}
163
		}
164
		return $this;
165
	}
166
167
	public function fromDatabaseObject($object, $function) {
168
	}
169
170
	public function wrap($before, $after="") {
171
		if (isset($before)) {
172
			array_unshift($this->_wrapBefore, $before);
173
		}
174
		$this->_wrapAfter[]=$after;
175
		return $this;
176
	}
177
178
179
180
	public function getElementById($identifier, $elements) {
181
		return $this->_getElementBy(function(BaseWidget $element) use ($identifier){return $element->getIdentifier()===$identifier;}, $elements);
182
	}
183
184
	public function getBsComponent() {
185
		return $this->_bsComponent;
186
	}
187
188
	public function setBsComponent($bsComponent) {
189
		$this->_bsComponent=$bsComponent;
190
		return $this;
191
	}
192
193
	protected function compile_once(JsUtils $js=NULL, &$view=NULL) {
194
		if(!$this->_compiled){
195
			if(isset($js)){
196
				$beforeCompile=$js->getParam("beforeCompileHtml");
197
				if(\is_callable($beforeCompile)){
198
					$beforeCompile($this,$js,$view);
199
				}
200
			}
201
			if(\is_callable($this->_preCompile)){
202
				$pc=$this->_preCompile;
203
				$pc($this);
204
			}
205
			unset($this->properties["jsCallback"]);
206
			$this->_compiled=true;
207
		}
208
	}
209
210
	public function compile(JsUtils $js=NULL, &$view=NULL) {
211
		$this->compile_once($js,$view);
212
		$result=$this->getTemplate($js);
213
		foreach ( $this as $key => $value ) {
214
			if (JString::startswith($key, "_") === false && $key !== "events") {
215
				if (\is_array($value)) {
216
					$v=PropertyWrapper::wrap($value, $js);
217
				} else {
218
					if($value instanceof \stdClass)
219
						$v=\print_r($value,true);
220
					else
221
						$v=$value;
222
				}
223
				$result=str_ireplace("%" . $key . "%", $v, $result);
224
			}
225
		}
226
		if (isset($js)===true) {
227
			$this->run($js);
228
			if (isset($view) === true) {
229
				$js->addViewElement($this->_identifier, $result, $view);
230
			}
231
		}
232
233
		if(\is_callable($this->_postCompile)){
234
			$pc=$this->_postCompile;
235
			$pc($this);
236
		}
237
		return $result;
238
	}
239
240
	public function __toString() {
241
		return $this->compile();
242
	}
243
244
	public function onPostCompile($callback){
245
		$this->_postCompile=$callback;
246
	}
247
248
	public function onPreCompile($callback){
249
		$this->_preCompile=$callback;
250
	}
251
}
252