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