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

InstanceViewer::removeField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
use Ajax\service\JString;
4
use Ajax\service\JArray;
5
6
class InstanceViewer {
7
	protected $widgetIdentifier;
8
	protected $instance;
9
	protected $reflect;
10
	protected $properties;
11
	protected $visibleProperties;
12
	protected $values;
13
	protected $afterCompile;
14
	protected $captions;
15
	protected $captionCallback;
16
	protected $defaultValueFunction;
17
18
19
	public static $index=0;
20
21
	public function __construct($identifier,$instance=NULL,$captions=NULL){
22
		$this->widgetIdentifier=$identifier;
23
		$this->values=[];
24
		$this->afterCompile=[];
25
		if(isset($instance))
26
			$this->setInstance($instance);
27
		$this->setCaptions($captions);
28
		$this->captionCallback=NULL;
29
		$this->defaultValueFunction=function($name,$value){return $value;};
30
	}
31
32
	public function moveFieldTo($from,$to){
33
		if(JArray::moveElementTo($this->visibleProperties, $from, $to)){
34
			return JArray::moveElementTo($this->values, $from, $to);
35
		}
36
		return false;
37
	}
38
39
	public function swapFields($index1,$index2){
40
		if(JArray::swapElements($this->visibleProperties, $index1, $index2)){
41
			return JArray::swapElements($this->values, $index1, $index2);
42
		}
43
		return false;
44
	}
45
46
	public function removeField($index){
47
		\array_splice($this->visibleProperties,$index,1);
48
		\array_splice($this->values,$index,1);
49
		\array_splice($this->captions,$index,1);
50
		return $this;
51
	}
52
53
	public function getValues(){
54
		$values=[];
55
		$index=0;
56
		$count=$this->count();
57
		while($index<$count){
58
			$values[]=$this->getValue($index++);
59
		}
60
		return $values;
61
	}
62
63
	public function getIdentifier($index=NULL){
64
		if(!isset($index))
65
			$index=self::$index;
66
		$value=$index;
67
		if(isset($this->values["identifier"]))
68
			$value=$this->values["identifier"]($index,$this->instance);
69
		return $value;
70
	}
71
72
	public function getValue($index){
73
		$property=$this->properties[$index];
74
		return $this->_getValue($property, $index);
75
	}
76
77
	protected function _beforeAddProperty($index,&$field){
78
79
	}
80
81
	protected function _getDefaultValue($name,$value,$index){
82
		$func=$this->defaultValueFunction;
83
		return $func($name,$value,$index,$this->instance);
84
	}
85
86
	protected function _getPropertyValue(\ReflectionProperty $property,$index){
87
		$property->setAccessible(true);
88
		$value=$property->getValue($this->instance);
89
		if(isset($this->values[$index])){
90
			$value= $this->values[$index]($value,$this->instance,$index);
91
		}else{
92
			$value=$this->_getDefaultValue($property->getName(),$value, $index);
93
		}
94
		return $value;
95
	}
96
97
	protected function _getValue($property,$index){
98
		$value=null;
99
		if($property instanceof \ReflectionProperty){
100
			$value=$this->_getPropertyValue($property, $index);
101
		}else{
102
			if(\is_callable($property))
103
				$value=$property($this->instance);
104
			elseif(\is_array($property)){
105
				$values=\array_map(function($v) use ($index){return $this->_getValue($v, $index);}, $property);
106
				$value=\implode("", $values);
107
			}else{
108
				if(isset($this->values[$index])){
109
					$value= $this->values[$index]($property,$this->instance,$index);
110
				}elseif(isset($this->instance->{$property})){
111
					$value=$this->instance->{$property};
112
				}
113
			}
114
		}
115
		if(isset($this->afterCompile[$index])){
116
			if(\is_callable($this->afterCompile[$index])){
117
				$this->afterCompile[$index]($value,$this->instance,$index);
118
			}
119
		}
120
		return $value;
121
	}
122
123
	public function insertField($index,$field){
124
		array_splice( $this->visibleProperties, $index, 0, $field );
125
		return $this;
126
	}
127
128
	public function insertInField($index,$field){
129
		$vb=$this->visibleProperties;
130
		if(isset($vb[$index])){
131
			if(\is_array($vb[$index])){
132
				$this->visibleProperties[$index][]=$field;
133
			}else{
134
				$this->visibleProperties[$index]=[$vb[$index],$field];
135
			}
136
		}else{
137
			return $this->insertField($index, $field);
138
		}
139
		return $this;
140
	}
141
142
	public function addField($field){
143
		$this->visibleProperties[]=$field;
144
		return $this;
145
	}
146
147
	public function count(){
148
		return \sizeof($this->properties);
149
	}
150
151
	public function visiblePropertiesCount(){
152
		return \sizeof($this->visibleProperties);
153
	}
154
155
	public function getProperty($index){
156
		return $this->properties[$index];
157
	}
158
159
	public function getFieldName($index){
160
		$property=$this->getProperty($index);
161
		if($property instanceof \ReflectionProperty){
162
			$result=$property->getName();
163
		}elseif(\is_callable($property)){
164
			$result=$this->visibleProperties[$index];
165
		}else{
166
			$result=$property;
167
		}
168
		return $result;
169
	}
170
171
172
	protected function showableProperty(\ReflectionProperty $rProperty){
173
		return JString::startswith($rProperty->getName(),"_")===false;
174
	}
175
176
	public function setInstance($instance) {
177
		if(\is_string($instance)){
178
			$instance=new $instance();
179
		}
180
		$this->instance=$instance;
181
		$this->properties=[];
182
		$this->reflect=new \ReflectionClass($instance);
183
		if(\sizeof($this->visibleProperties)===0){
184
			$this->properties=$this->getDefaultProperties();
185
		}else{
186
			foreach ($this->visibleProperties as $property){
187
				$this->setInstanceProperty($property);
188
			}
189
		}
190
		return $this;
191
	}
192
193
	private function setInstanceProperty($property){
194
		if(\is_callable($property)){
195
			$this->properties[]=$property;
196
		}elseif(\is_string($property)){
197
			try{
198
				$this->_beforeAddProperty(\sizeof($this->properties), $property);
199
				$rProperty=$this->reflect->getProperty($property);
200
				$this->properties[]=$rProperty;
201
			}catch(\Exception $e){
202
				$this->_beforeAddProperty(\sizeof($this->properties), $property);
203
				$this->properties[]=$property;
204
			}
205
		}elseif(\is_int($property)){
206
			$props=$this->getDefaultProperties();
207
			if(isset($props[$property]))
208
				$this->properties[]=$props[$property];
209
				else
210
					$this->properties[]=$property;
211
		}else{
212
			$this->properties[]=$property;
213
		}
214
	}
215
216
	protected function getDefaultProperties(){
217
		$result=[];
218
		$properties=$this->reflect->getProperties();
219
		foreach ($properties as $property){
220
			$showable=$this->showableProperty($property);
221
			if($showable!==false){
222
				$result[]=$property;
223
			}
224
		}
225
		return $result;
226
	}
227
228
	public function setVisibleProperties($visibleProperties) {
229
		$this->visibleProperties=$visibleProperties;
230
		return $this;
231
	}
232
233
	public function setValueFunction($index,$callback){
234
		$this->values[$index]=$callback;
235
		return $this;
236
	}
237
238
	public function setIdentifierFunction($callback){
239
		$this->values["identifier"]=$callback;
240
		return $this;
241
	}
242
243
	public static function setIndex($index) {
244
		self::$index=$index;
245
	}
246
247
	public function getProperties() {
248
		return $this->properties;
249
	}
250
251
	public function getCaption($index){
252
		if(isset($this->captions[$index])){
253
			return $this->captions[$index];
254
		}
255
		if($this->properties[$index] instanceof \ReflectionProperty)
256
			return $this->properties[$index]->getName();
257
		elseif(\is_callable($this->properties[$index]))
258
			return "";
259
		else
260
			return $this->properties[$index];
261
	}
262
263
	public function getCaptions(){
264
		if(isset($this->captions)){
265
			$captions= \array_values($this->captions);
266
			for($i=\sizeof($captions);$i<$this->count();$i++){
267
				$captions[]="";
268
			}
269
		}else{
270
			$captions=[];
271
			$index=0;
272
			$count=$this->count();
273
			while($index<$count){
274
				$captions[]=$this->getCaption($index++);
275
			}
276
		}
277
		if(isset($this->captionCallback) && \is_callable($this->captionCallback)){
278
			$callback=$this->captionCallback;
279
			$callback($captions,$this->instance);
280
		}
281
		return $captions;
282
	}
283
284
	public function setCaption($index,$caption){
285
		if(isset($this->captions)===false)
286
			$this->captions=[];
287
		$this->captions[$index]=$caption;
288
		return $this;
289
	}
290
291
	public function setCaptions($captions) {
292
		$this->captions=$captions;
293
		return $this;
294
	}
295
296
	/**
297
	 * Associates a $callback function after the compilation of the field at $index position
298
	 * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position
299
	 * @param int $index postion of the compiled field
300
	 * @param callable $callback function called after the field compilation
301
	 * @return \Ajax\semantic\widgets\datatable\InstanceViewer
302
	 */
303
	public function afterCompile($index,$callback){
304
		$this->afterCompile[$index]=$callback;
305
		return $this;
306
	}
307
308
	/**
309
	 * Defines a callback function to call for modifying captions
310
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
311
	 * @param callable $captionCallback
312
	 * @return \Ajax\semantic\widgets\base\InstanceViewer
313
	 */
314
	public function setCaptionCallback($captionCallback) {
315
		$this->captionCallback=$captionCallback;
316
		return $this;
317
	}
318
319
	/**
320
	 * Defines the default function which displays fields value
321
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
322
	 * @return \Ajax\semantic\widgets\base\InstanceViewer
323
	 */
324
	public function setDefaultValueFunction($defaultValueFunction) {
325
		$this->defaultValueFunction=$defaultValueFunction;
326
		return $this;
327
	}
328
}