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