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