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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
}else{ |
|
|
|
|
71
|
|
|
if(isset($this->values[$index])){ |
72
|
|
|
$value= $this->values[$index]($property,$this->instance,$index); |
73
|
|
|
}else{ |
74
|
|
|
$value=$property; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
if(isset($this->afterCompile[$index])){ |
79
|
|
|
if(\is_callable($this->afterCompile[$index])){ |
80
|
|
|
$this->afterCompile[$index]($value,$this->instance,$index); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function insertField($index,$field){ |
87
|
|
|
array_splice( $this->visibleProperties, $index, 0, $field ); |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function insertInField($index,$field){ |
92
|
|
|
$vb=$this->visibleProperties; |
93
|
|
|
if(isset($vb[$index])){ |
94
|
|
|
if(\is_array($vb[$index])){ |
95
|
|
|
$this->visibleProperties[$index][]=$field; |
96
|
|
|
}else{ |
97
|
|
|
$this->visibleProperties[$index]=[$vb[$index],$field]; |
98
|
|
|
} |
99
|
|
|
}else{ |
100
|
|
|
return $this->insertField($index, $field); |
101
|
|
|
} |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function addField($field){ |
106
|
|
|
$this->visibleProperties[]=$field; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function count(){ |
111
|
|
|
return \sizeof($this->properties); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function visiblePropertiesCount(){ |
115
|
|
|
return \sizeof($this->visibleProperties); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getProperty($index){ |
119
|
|
|
return $this->properties[$index]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function showableProperty(\ReflectionProperty $rProperty){ |
123
|
|
|
return JString::startswith($rProperty->getName(),"_")===false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setInstance($instance) { |
127
|
|
|
if(\is_string($instance)){ |
128
|
|
|
$instance=new $instance(); |
129
|
|
|
} |
130
|
|
|
$this->instance=$instance; |
131
|
|
|
$this->properties=[]; |
132
|
|
|
$this->reflect=new \ReflectionClass($instance); |
133
|
|
|
if(\sizeof($this->visibleProperties)===0){ |
134
|
|
|
$this->properties=$this->getDefaultProperties(); |
135
|
|
|
}else{ |
136
|
|
|
foreach ($this->visibleProperties as $property){ |
137
|
|
|
if(\is_callable($property)){ |
138
|
|
|
$this->properties[]=$property; |
139
|
|
|
}elseif(\is_string($property)){ |
140
|
|
|
try{ |
141
|
|
|
$this->_beforeAddProperty(\sizeof($this->properties), $property); |
142
|
|
|
$rProperty=$this->reflect->getProperty($property); |
143
|
|
|
$this->properties[]=$rProperty; |
144
|
|
|
}catch(\Exception $e){ |
145
|
|
|
$this->_beforeAddProperty(\sizeof($this->properties), $property); |
146
|
|
|
$this->properties[]=$property; |
147
|
|
|
} |
148
|
|
|
}elseif(\is_int($property)){ |
149
|
|
|
$props=$this->getDefaultProperties(); |
150
|
|
|
if(isset($props[$property])) |
151
|
|
|
$this->properties[]=$props[$property]; |
152
|
|
|
else |
153
|
|
|
$this->properties[]=$property; |
154
|
|
|
}else{ |
155
|
|
|
$this->properties[]=$property; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function getDefaultProperties(){ |
163
|
|
|
$result=[]; |
164
|
|
|
$properties=$this->reflect->getProperties(); |
165
|
|
|
foreach ($properties as $property){ |
166
|
|
|
$showable=$this->showableProperty($property); |
167
|
|
|
if($showable!==false){ |
168
|
|
|
$result[]=$property; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
return $result; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function setVisibleProperties($visibleProperties) { |
175
|
|
|
$this->visibleProperties=$visibleProperties; |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function setValueFunction($index,$callback){ |
180
|
|
|
$this->values[$index]=$callback; |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function setIdentifierFunction($callback){ |
185
|
|
|
$this->values["identifier"]=$callback; |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public static function setIndex($index) { |
190
|
|
|
self::$index=$index; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getProperties() { |
194
|
|
|
return $this->properties; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getCaption($index){ |
198
|
|
|
if(isset($this->captions[$index])){ |
199
|
|
|
return $this->captions[$index]; |
200
|
|
|
} |
201
|
|
|
if($this->properties[$index] instanceof \ReflectionProperty) |
202
|
|
|
return $this->properties[$index]->getName(); |
203
|
|
|
elseif(\is_callable($this->properties[$index])) |
204
|
|
|
return ""; |
205
|
|
|
else |
206
|
|
|
return $this->properties[$index]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getCaptions(){ |
210
|
|
|
if(isset($this->captions)){ |
211
|
|
|
$result= $this->captions; |
212
|
|
|
for($i=\sizeof($result);$i<$this->count();$i++){ |
213
|
|
|
$result[]=""; |
214
|
|
|
} |
215
|
|
|
return $result; |
216
|
|
|
} |
217
|
|
|
$captions=[]; |
218
|
|
|
$index=0; |
219
|
|
|
$count=$this->count(); |
220
|
|
|
while($index<$count){ |
221
|
|
|
$captions[]=$this->getCaption($index++); |
222
|
|
|
} |
223
|
|
|
return $captions; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function setCaption($index,$caption){ |
227
|
|
|
if(isset($this->captions)===false) |
228
|
|
|
$this->captions=[]; |
229
|
|
|
$this->captions[$index]=$caption; |
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function setCaptions($captions) { |
234
|
|
|
$this->captions=$captions; |
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Associates a $callback function after the compilation of the field at $index position |
240
|
|
|
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
241
|
|
|
* @param int $index postion of the compiled field |
242
|
|
|
* @param callable $callback function called after the field compilation |
243
|
|
|
* @return \Ajax\semantic\widgets\datatable\InstanceViewer |
244
|
|
|
*/ |
245
|
|
|
public function afterCompile($index,$callback){ |
246
|
|
|
$this->afterCompile[$index]=$callback; |
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
} |
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.