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(){ |
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,$this->instance); |
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
|
|
|
public function getFieldName($index){ |
130
|
|
|
$property=$this->getProperty($index); |
131
|
|
|
if($property instanceof \ReflectionProperty){ |
132
|
|
|
$result=$property->getName(); |
133
|
|
|
}elseif(\is_callable($property)){ |
134
|
|
|
$result=$this->visibleProperties[$index]; |
135
|
|
|
}else{ |
136
|
|
|
$result=$property; |
137
|
|
|
} |
138
|
|
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
protected function showableProperty(\ReflectionProperty $rProperty){ |
143
|
|
|
return JString::startswith($rProperty->getName(),"_")===false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setInstance($instance) { |
147
|
|
|
if(\is_string($instance)){ |
148
|
|
|
$instance=new $instance(); |
149
|
|
|
} |
150
|
|
|
$this->instance=$instance; |
151
|
|
|
$this->properties=[]; |
152
|
|
|
$this->reflect=new \ReflectionClass($instance); |
153
|
|
|
if(\sizeof($this->visibleProperties)===0){ |
154
|
|
|
$this->properties=$this->getDefaultProperties(); |
155
|
|
|
}else{ |
156
|
|
|
foreach ($this->visibleProperties as $property){ |
157
|
|
|
$this->setInstanceProperty($property); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function setInstanceProperty($property){ |
164
|
|
|
if(\is_callable($property)){ |
165
|
|
|
$this->properties[]=$property; |
166
|
|
|
}elseif(\is_string($property)){ |
167
|
|
|
try{ |
168
|
|
|
$this->_beforeAddProperty(\sizeof($this->properties), $property); |
169
|
|
|
$rProperty=$this->reflect->getProperty($property); |
170
|
|
|
$this->properties[]=$rProperty; |
171
|
|
|
}catch(\Exception $e){ |
172
|
|
|
$this->_beforeAddProperty(\sizeof($this->properties), $property); |
173
|
|
|
$this->properties[]=$property; |
174
|
|
|
} |
175
|
|
|
}elseif(\is_int($property)){ |
176
|
|
|
$props=$this->getDefaultProperties(); |
177
|
|
|
if(isset($props[$property])) |
178
|
|
|
$this->properties[]=$props[$property]; |
179
|
|
|
else |
180
|
|
|
$this->properties[]=$property; |
181
|
|
|
}else{ |
182
|
|
|
$this->properties[]=$property; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function getDefaultProperties(){ |
187
|
|
|
$result=[]; |
188
|
|
|
$properties=$this->reflect->getProperties(); |
189
|
|
|
foreach ($properties as $property){ |
190
|
|
|
$showable=$this->showableProperty($property); |
191
|
|
|
if($showable!==false){ |
192
|
|
|
$result[]=$property; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return $result; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function setVisibleProperties($visibleProperties) { |
199
|
|
|
$this->visibleProperties=$visibleProperties; |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function setValueFunction($index,$callback){ |
204
|
|
|
$this->values[$index]=$callback; |
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setIdentifierFunction($callback){ |
209
|
|
|
$this->values["identifier"]=$callback; |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public static function setIndex($index) { |
214
|
|
|
self::$index=$index; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getProperties() { |
218
|
|
|
return $this->properties; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getCaption($index){ |
222
|
|
|
if(isset($this->captions[$index])){ |
223
|
|
|
return $this->captions[$index]; |
224
|
|
|
} |
225
|
|
|
if($this->properties[$index] instanceof \ReflectionProperty) |
226
|
|
|
return $this->properties[$index]->getName(); |
227
|
|
|
elseif(\is_callable($this->properties[$index])) |
228
|
|
|
return ""; |
229
|
|
|
else |
230
|
|
|
return $this->properties[$index]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getCaptions(){ |
234
|
|
|
if(isset($this->captions)){ |
235
|
|
|
$captions= $this->captions; |
236
|
|
|
for($i=\sizeof($captions);$i<$this->count();$i++){ |
237
|
|
|
$captions[]=""; |
238
|
|
|
} |
239
|
|
|
}else{ |
240
|
|
|
$captions=[]; |
241
|
|
|
$index=0; |
242
|
|
|
$count=$this->count(); |
243
|
|
|
while($index<$count){ |
244
|
|
|
$captions[]=$this->getCaption($index++); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
if(isset($this->captionCallback) && \is_callable($this->captionCallback)){ |
248
|
|
|
$callback=$this->captionCallback; |
249
|
|
|
$callback($captions,$this->instance); |
250
|
|
|
} |
251
|
|
|
return $captions; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function setCaption($index,$caption){ |
255
|
|
|
if(isset($this->captions)===false) |
256
|
|
|
$this->captions=[]; |
257
|
|
|
$this->captions[$index]=$caption; |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function setCaptions($captions) { |
262
|
|
|
$this->captions=$captions; |
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Associates a $callback function after the compilation of the field at $index position |
268
|
|
|
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
269
|
|
|
* @param int $index postion of the compiled field |
270
|
|
|
* @param callable $callback function called after the field compilation |
271
|
|
|
* @return \Ajax\semantic\widgets\datatable\InstanceViewer |
272
|
|
|
*/ |
273
|
|
|
public function afterCompile($index,$callback){ |
274
|
|
|
$this->afterCompile[$index]=$callback; |
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Defines a callback function to call for modifying captions |
280
|
|
|
* function parameters are $captions: the captions to modify and $instance: the active model instance |
281
|
|
|
* @param callable $captionCallback |
282
|
|
|
* @return \Ajax\semantic\widgets\base\InstanceViewer |
283
|
|
|
*/ |
284
|
|
|
public function setCaptionCallback($captionCallback) { |
285
|
|
|
$this->captionCallback=$captionCallback; |
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function setDefaultValueFunction($defaultValueFunction) { |
290
|
|
|
$this->defaultValueFunction=$defaultValueFunction; |
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
} |
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.