Completed
Push — master ( da7bd7...a4feba )
by Jean-Christophe
03:22
created

BaseHtml::compile()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 5.7377
c 0
b 0
f 0
cc 8
eloc 17
nc 24
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
			$flag=false;
108
			$index=0;
109
			while ( !$flag && $index < sizeof($elements) ) {
110
				if ($elements[$index] instanceof BaseHtml)
111
					$flag=($callback($elements[$index]));
112
					$index++;
113
			}
114
			if ($flag === true)
115
				return $elements[$index - 1];
116
		} elseif ($elements instanceof BaseHtml) {
117
			if ($callback($elements))
118
				return $elements;
119
		}
120
		return null;
121
	}
122
123
	protected function setWrapBefore($wrapBefore) {
124
		$this->_wrapBefore=$wrapBefore;
125
		return $this;
126
	}
127
128
	protected function setWrapAfter($wrapAfter) {
129
		$this->_wrapAfter=$wrapAfter;
130
		return $this;
131
	}
132
133
	public function getTagName() {
134
		return $this->tagName;
135
	}
136
137
	public function setTagName($tagName) {
138
		$this->tagName=$tagName;
139
		return $this;
140
	}
141
142
	public function fromArray($array) {
143 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...
144
			if(array_key_exists($key, $array)===true)
145
				$this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array);
146
		}
147 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...
148
			if($this->_callSetter($key, $key, $value, $array)===false){
149
				$this->_callSetter("set" . ucfirst($key), $key, $value, $array);
150
			}
151
		}
152
		return $array;
153
	}
154
155
	public function fromDatabaseObjects($objects, $function) {
156
		if (isset($objects)) {
157
			foreach ( $objects as $object ) {
158
				$this->fromDatabaseObject($object, $function);
159
			}
160
		}
161
		return $this;
162
	}
163
164
	public function fromDatabaseObject($object, $function) {
165
	}
166
167
	public function wrap($before, $after="") {
168
		if (isset($before)) {
169
			array_unshift($this->_wrapBefore, $before);
170
		}
171
		$this->_wrapAfter[]=$after;
172
		return $this;
173
	}
174
175
176
177
	public function getElementById($identifier, $elements) {
178
		return $this->_getElementBy(function($element) use ($identifier){return $element->getIdentifier()===$identifier;}, $elements);
179
	}
180
181
	public function getBsComponent() {
182
		return $this->_bsComponent;
183
	}
184
185
	public function setBsComponent($bsComponent) {
186
		$this->_bsComponent=$bsComponent;
187
		return $this;
188
	}
189
190
	protected function compile_once(JsUtils $js=NULL, &$view=NULL) {
0 ignored issues
show
Unused Code introduced by
The parameter $js is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $view is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
191
	}
192
	public function compile(JsUtils $js=NULL, &$view=NULL) {
193
		if(!$this->_compiled){
194
			$this->compile_once($js,$view);
195
			$this->_compiled=true;
196
		}
197
		$result=$this->getTemplate($js);
198
		foreach ( $this as $key => $value ) {
199
			if (JString::startswith($key, "_") === false && $key !== "events") {
200
				if (\is_array($value)) {
201
					$v=PropertyWrapper::wrap($value, $js);
202
				} else {
203
					$v=$value;
204
				}
205
				$result=str_ireplace("%" . $key . "%", $v, $result);
206
			}
207
		}
208
		if (isset($js)===true) {
209
			$this->run($js);
210
			if (isset($view) === true) {
211
				$js->addViewElement($this->_identifier, $result, $view);
212
			}
213
		}
214
		return $result;
215
	}
216
217
	public function __toString() {
218
		return $this->compile();
219
	}
220
}