-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractEntity.php
221 lines (163 loc) · 6.41 KB
/
AbstractEntity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Stopsopa\LiteSerializer\Libs;
use Stopsopa\LiteSerializer\Exceptions\AbstractEntityException;
use ArrayAccess;
use ReflectionException;
use ReflectionProperty;
use ReflectionMethod;
use Traversable;
abstract class AbstractEntity {
const MARKER = '__CG__';
/**
* @param object $entity
* @param string|array $path - Key like 'author.id'
* @param $default - If you provide this parameter that is optional then this method don't throw
* an exception but return this value
* @return mixed
* @throws AbstractEntityException
*/
public static function get($entity, $path) {
if (!is_string($path)) {
throw new AbstractEntityException(
"Parameter 'attr' is not a string, it is: " . gettype($path),
AbstractEntityException::ATTR_IS_NOT_STRING
);
}
$args = func_get_args();
if (strpos($path, '.') !== false) {
$path = trim($path);
$exp = static::cascadeExplode($path);
if ( count($exp) > 1 ) {
foreach ($exp as $k) {
$args[0] = $entity;
$args[1] = $k;
$entity = call_user_func_array(
array(__CLASS__, 'internalValueByMethodOrAttribute'),
$args
);
}
return $entity;
}
$args[1] = $exp[0];
}
return call_user_func_array(array(__CLASS__, 'internalValueByMethodOrAttribute'), $args);
}
protected static function internalValueByMethodOrAttribute($entity, $path) {
$args = func_get_args();
$isdefault = (count($args) > 2);
$short = ucfirst($path);
if (is_array($entity) && array_key_exists($path, $entity)) {
return $entity[$path];
}
if (is_object($entity)) {
if ($entity instanceof ArrayAccess) {
if ($entity->offsetExists($path)) {
return $entity[$path];
}
}
if ($path === '') {
throw new AbstractEntityException(
"Parameter 'attr' is empty string: ".json_encode($path),
AbstractEntityException::ATTR_IS_EMPTY_STRING
);
}
if (strpos($path, '()') !== false) {
$method = rtrim($path, '()');
$access = false;
if (method_exists($entity, $method)) {
$access = true;
$reflection = new ReflectionMethod($entity, $method);
if ($reflection->isPublic()) {
return $entity->$method();
}
}
$class = self::getClass($entity);
throw new AbstractEntityException(
sprintf("Method %s() %s in class %s",
$method,
$access ? "is not public" : "doesn't exist",
$class
),
AbstractEntityException::DIRECTLY_CALLED_METHOD_DOESNT_EXIST
);
}
else {
$short = ucfirst($short);
$method = "get$short";
if (method_exists($entity, $method)) {
$reflection = new ReflectionMethod($entity, $method);
if ($reflection->isPublic()) {
return $entity->$method();
}
}
$method = "is$short";
if (method_exists($entity, $method)) {
$reflection = new ReflectionMethod($entity, $method);
if ($reflection->isPublic()) {
return $entity->$method();
}
}
$method = "has$short";
if (method_exists($entity, $method)) {
$reflection = new ReflectionMethod($entity, $method);
if ($reflection->isPublic()) {
return $entity->$method();
}
}
if (isset($entity->$path)) {
return $entity->$path;
}
try {
$rp = new ReflectionProperty($entity, $path);
$rp->setAccessible(true);
return $rp->getValue($entity);
}
catch (ReflectionException $e) {
}
}
}
if ($isdefault) {
return $args[2];
}
$class = self::getClass($entity);
throw new AbstractEntityException(
__METHOD__." error: Property '$path' doesn't exist and ".
"methods get$short(), is$short(), has$short(), $path() are not accessible in '$class'",
AbstractEntityException::WRONG_KEY
);
}
public static function cascadeExplode($key)
{
$key = preg_split("#(?<!\\\\)\.#", $key);
foreach ($key as $k => &$d) {
$d = str_replace('\\.', '.', $d);
}
return $key;
}
public static function getClass($obj) {
if (is_object($obj)) {
return static::cleanClassNamespace(get_class($obj));
}
return gettype($obj);
}
/**
* Unfortunately Doctrine generates different namespaces for proxy classes, like:
* Proxies\__CG__\Site\CMS\ArticleBundle\Entity\Article
*/
public static function cleanClassNamespace($classNamespace) {
if ( strpos( $classNamespace, "\\" . preg_quote(static::MARKER) . "\\" ) !== false ) {
$classNamespace = preg_replace('#^[^\\\\]+\\\\[^\\\\]+\\\\(.*)$#', '$1', $classNamespace);
}
return $classNamespace;
}
protected function getMethodName($object)
{
$parts = explode('\\', static::getClass($object));
$last = array_pop($parts);
$parts = implode('', $parts);
return 'dump'.ucfirst("{$parts}_$last");
}
public static function isForeachable($object) {
return is_array($object) || ( is_object($object) && $object instanceof Traversable);
}
}