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