1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\NestedAccessor\Helpers; |
4
|
|
|
|
5
|
|
|
use ReflectionMethod; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
class ObjectHelper |
10
|
|
|
{ |
11
|
|
|
public static function hasPublicProperty(object $object, string $propertyName): bool |
12
|
|
|
{ |
13
|
|
|
if ($object instanceof stdClass) { |
14
|
|
|
return static::hasProperty($object, $propertyName); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
return |
18
|
|
|
static::hasProperty($object, $propertyName) && |
19
|
|
|
static::getReflectionProperty($object, $propertyName)->isPublic(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function hasPublicMethod(object $object, string $methodName): bool |
23
|
|
|
{ |
24
|
|
|
return |
25
|
|
|
static::hasMethod($object, $methodName) && |
26
|
|
|
static::getReflectionMethod($object, $methodName)->isPublic(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function hasPropertyAccessibleByGetter(object $object, string $propertyName): bool |
30
|
|
|
{ |
31
|
|
|
return static::hasPublicMethod($object, static::getPropertyGetterName($propertyName)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function getPropertyByGetter(object $object, string $propertyName) |
35
|
|
|
{ |
36
|
|
|
return $object->{static::getPropertyGetterName($propertyName)}(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function hasProperty(object $object, string $propertyName): bool |
40
|
|
|
{ |
41
|
|
|
return property_exists($object, $propertyName); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function hasMethod(object $object, string $methodName): bool |
45
|
|
|
{ |
46
|
|
|
return method_exists($object, $methodName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty |
50
|
|
|
{ |
51
|
|
|
return new ReflectionProperty(get_class($object), $propertyName); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected static function getReflectionMethod(object $object, string $methodName): ReflectionMethod |
55
|
|
|
{ |
56
|
|
|
return new ReflectionMethod(get_class($object), $methodName); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected static function getPropertyGetterName(string $propertyName): string |
60
|
|
|
{ |
61
|
|
|
return 'get'.ucfirst($propertyName); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|