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