Completed
Push — master ( bda9b8...aa1d0c )
by Jean-Christophe
03:12
created

InstanceViewer::getCaptions()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 8.8571
c 1
b 0
f 0
cc 6
eloc 15
nc 8
nop 0
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
				if(\is_callable($property)){
142
					$this->properties[]=$property;
143
				}elseif(\is_string($property)){
144
					try{
145
						$this->_beforeAddProperty(\sizeof($this->properties), $property);
146
						$rProperty=$this->reflect->getProperty($property);
147
						$this->properties[]=$rProperty;
148
					}catch(\Exception $e){
149
						$this->_beforeAddProperty(\sizeof($this->properties), $property);
150
						$this->properties[]=$property;
151
					}
152
				}elseif(\is_int($property)){
153
					$props=$this->getDefaultProperties();
154
					if(isset($props[$property]))
155
						$this->properties[]=$props[$property];
156
					else
157
						$this->properties[]=$property;
158
				}else{
159
					$this->properties[]=$property;
160
				}
161
			}
162
		}
163
		return $this;
164
	}
165
166
	protected function getDefaultProperties(){
167
		$result=[];
168
		$properties=$this->reflect->getProperties();
169
		foreach ($properties as $property){
170
			$showable=$this->showableProperty($property);
171
			if($showable!==false){
172
				$result[]=$property;
173
			}
174
		}
175
		return $result;
176
	}
177
178
	public function setVisibleProperties($visibleProperties) {
179
		$this->visibleProperties=$visibleProperties;
180
		return $this;
181
	}
182
183
	public function setValueFunction($index,$callback){
184
		$this->values[$index]=$callback;
185
		return $this;
186
	}
187
188
	public function setIdentifierFunction($callback){
189
		$this->values["identifier"]=$callback;
190
		return $this;
191
	}
192
193
	public static function setIndex($index) {
194
		self::$index=$index;
195
	}
196
197
	public function getProperties() {
198
		return $this->properties;
199
	}
200
201
	public function getCaption($index){
202
		if(isset($this->captions[$index])){
203
			return $this->captions[$index];
204
		}
205
		if($this->properties[$index] instanceof \ReflectionProperty)
206
			return $this->properties[$index]->getName();
207
		elseif(\is_callable($this->properties[$index]))
208
			return "";
209
		else
210
			return $this->properties[$index];
211
	}
212
213
	public function getCaptions(){
214
		if(isset($this->captions)){
215
			$captions= $this->captions;
216
			for($i=\sizeof($captions);$i<$this->count();$i++){
217
				$captions[]="";
218
			}
219
		}else{
220
			$captions=[];
221
			$index=0;
222
			$count=$this->count();
223
			while($index<$count){
224
				$captions[]=$this->getCaption($index++);
225
			}
226
		}
227
		if(isset($this->captionCallback) && \is_callable($this->captionCallback)){
228
			$callback=$this->captionCallback;
229
			$callback($captions,$this->instance);
230
		}
231
		return $captions;
232
	}
233
234
	public function setCaption($index,$caption){
235
		if(isset($this->captions)===false)
236
			$this->captions=[];
237
		$this->captions[$index]=$caption;
238
		return $this;
239
	}
240
241
	public function setCaptions($captions) {
242
		$this->captions=$captions;
243
		return $this;
244
	}
245
246
	/**
247
	 * Associates a $callback function after the compilation of the field at $index position
248
	 * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position
249
	 * @param int $index postion of the compiled field
250
	 * @param callable $callback function called after the field compilation
251
	 * @return \Ajax\semantic\widgets\datatable\InstanceViewer
252
	 */
253
	public function afterCompile($index,$callback){
254
		$this->afterCompile[$index]=$callback;
255
		return $this;
256
	}
257
258
	/**
259
	 * Defines a callback function to call for modifying captions
260
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
261
	 * @param callable $captionCallback
262
	 * @return \Ajax\semantic\widgets\base\InstanceViewer
263
	 */
264
	public function setCaptionCallback($captionCallback) {
265
		$this->captionCallback=$captionCallback;
266
		return $this;
267
	}
268
269
}