1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dwo\SimpleAccessor; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AccessInfo |
9
|
|
|
* |
10
|
|
|
* @author Dave Www <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class AccessInfo |
13
|
|
|
{ |
14
|
|
|
const METHOD = 'method'; |
15
|
|
|
const PROPERTY = 'property'; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see PropertyAccessor::readProperty() |
20
|
|
|
* |
21
|
|
|
* @param object $object |
22
|
|
|
* @param string $property |
23
|
|
|
* @param array $access |
24
|
|
|
* |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
|
|
public static function readProperty($object, $property, array $access) |
28
|
|
|
{ |
29
|
|
|
if (AccessInfo::METHOD === $access['type']) { |
30
|
|
|
$value = $object->{$access['name']}(); |
31
|
|
|
} elseif (AccessInfo::PROPERTY === $access['type']) { |
32
|
|
|
$value = $object->{$access['name']}; |
33
|
|
|
} elseif (!$access['has_property'] && property_exists($object, $property)) { |
34
|
|
|
$value = $object->$property; |
35
|
|
|
} else { |
36
|
|
|
throw new \RuntimeException($access['name']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @see PropertyAccessor::getReadAccessInfo() |
44
|
|
|
* |
45
|
|
|
* @param ReflectionClass $reflClass |
46
|
|
|
* @param string $property |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public static function getReadAccessInfo(ReflectionClass $reflClass, $property) |
51
|
|
|
{ |
52
|
|
|
$access = array(); |
53
|
|
|
$access['has_property'] = $reflClass->hasProperty($property); |
54
|
|
|
|
55
|
|
|
//method |
56
|
|
|
$camelProp = str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); |
57
|
|
|
$methods = array('get'.$camelProp, lcfirst($camelProp), 'is'.$camelProp, 'has'.$camelProp); |
58
|
|
|
foreach ($methods as $method) { |
59
|
|
|
if ($reflClass->hasMethod($method) && $reflClass->getMethod($method)->isPublic()) { |
60
|
|
|
$access['type'] = AccessInfo::METHOD; |
61
|
|
|
$access['name'] = $method; |
62
|
|
|
|
63
|
|
|
return $access; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
//property |
68
|
|
|
if ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) { |
69
|
|
|
$access['type'] = AccessInfo::PROPERTY; |
70
|
|
|
$access['name'] = $property; |
71
|
|
|
$access['ref'] = false; |
72
|
|
|
} elseif ($access['has_property'] && $reflClass->getProperty($property)->isPublic()) { |
73
|
|
|
$access['type'] = AccessInfo::PROPERTY; |
74
|
|
|
$access['name'] = $property; |
75
|
|
|
$access['ref'] = true; |
76
|
|
|
} else { |
77
|
|
|
$access['type'] = 'not_found'; |
78
|
|
|
$access['name'] = sprintf( |
79
|
|
|
'Neither the property "%s" nor one of the methods "%s()" '. |
80
|
|
|
'exist and have public access in class "%s".', |
81
|
|
|
$property, |
82
|
|
|
implode('()", "', array_merge($methods, ['__get'])), |
83
|
|
|
$reflClass->name |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $access; |
88
|
|
|
} |
89
|
|
|
} |