Completed
Push — master ( b1701b...fcfed5 )
by Jean-Christophe
03:28
created

BaseHtml::compile()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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