|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Accessor; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\DeserializerLogicException; |
|
8
|
|
|
use Chubbyphp\Deserialization\Doctrine\Accessor\PropertyAccessor as DoctrinePropertyAccessor; |
|
9
|
|
|
use Doctrine\Common\Persistence\Proxy; |
|
10
|
|
|
|
|
11
|
|
|
final class PropertyAccessor implements AccessorInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $property; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $property |
|
20
|
|
|
*/ |
|
21
|
6 |
|
public function __construct(string $property) |
|
22
|
|
|
{ |
|
23
|
6 |
|
$this->property = $property; |
|
24
|
6 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param object $object |
|
28
|
|
|
* @param mixed $value |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function setValue($object, $value) |
|
31
|
|
|
{ |
|
32
|
3 |
|
$reflectionProperty = $this->getReflectionProperty($this->getClass($object)); |
|
33
|
2 |
|
$reflectionProperty->setAccessible(true); |
|
34
|
2 |
|
$reflectionProperty->setValue($object, $value); |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param object $object |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function getValue($object) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$reflectionProperty = $this->getReflectionProperty($this->getClass($object)); |
|
45
|
3 |
|
$reflectionProperty->setAccessible(true); |
|
46
|
|
|
|
|
47
|
3 |
|
return $reflectionProperty->getValue($object); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param object $object |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
6 |
|
private function getClass($object): string |
|
56
|
|
|
{ |
|
57
|
6 |
|
if (interface_exists('Doctrine\Common\Persistence\Proxy') && $object instanceof Proxy) { |
|
58
|
2 |
|
$parentClass = (new \ReflectionClass($object))->getParentClass()->name; |
|
59
|
2 |
|
@trigger_error( |
|
|
|
|
|
|
60
|
2 |
|
sprintf( |
|
61
|
2 |
|
'Use "%s" instead of "%s" for "%s:%s"', |
|
62
|
2 |
|
DoctrinePropertyAccessor::class, |
|
63
|
2 |
|
self::class, |
|
64
|
2 |
|
$parentClass, |
|
65
|
2 |
|
$this->property |
|
66
|
|
|
), |
|
67
|
2 |
|
E_USER_DEPRECATED |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
2 |
|
if (!$object->__isInitialized()) { |
|
71
|
2 |
|
$object->__load(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $parentClass; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
return get_class($object); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $class |
|
82
|
|
|
* |
|
83
|
|
|
* @return \ReflectionProperty |
|
84
|
|
|
*/ |
|
85
|
6 |
View Code Duplication |
private function getReflectionProperty(string $class): \ReflectionProperty |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
try { |
|
88
|
6 |
|
return new \ReflectionProperty($class, $this->property); |
|
89
|
2 |
|
} catch (\ReflectionException $e) { |
|
90
|
2 |
|
throw DeserializerLogicException::createMissingProperty($class, $this->property); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: