Completed
Push — master ( b90c12...235574 )
by Jean-Christophe
03:37
created

InstanceViewer::afterCompile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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