Completed
Push — master ( 6831db...b65ced )
by Jean-Christophe
03:10
created

BaseHtml::fromArray()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 12
Code Lines 8

Duplication

Lines 9
Ratio 75 %

Importance

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