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