Completed
Push — master ( 0ed595...25ea1a )
by Jean-Christophe
03:24
created

InstanceViewer::setInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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