-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEnumPostLoadEntityListener.php
143 lines (124 loc) · 3.63 KB
/
EnumPostLoadEntityListener.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
<?php
namespace Consistence\Doctrine\Enum;
use Consistence\Enum\Enum;
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
use ReflectionClass;
class EnumPostLoadEntityListener
{
const EMBEDDED_SEPARATOR = '.';
/** @var \Doctrine\Common\Annotations\Reader */
private $annotationReader;
public function __construct(Reader $annotationReader)
{
$this->annotationReader = $annotationReader;
}
public function postLoad(LifecycleEventArgs $event)
{
$entity = $event->getEntity();
$entityManager = $event->getEntityManager();
$metadata = $this->getClassMetadata($entityManager, get_class($entity));
foreach ($metadata->getFieldNames() as $fieldName) {
$this->processField($entityManager, $entity, $fieldName);
}
}
/**
* @param \Doctrine\ORM\EntityManager $entityManager
* @param object $entity
* @param string $fieldName
*/
private function processField(
EntityManager $entityManager,
$entity,
$fieldName
)
{
$metadata = $this->getClassMetadata($entityManager, get_class($entity));
list($object, $classReflection, $propertyName) = $this->resolveObjectAndProperty(
$entityManager,
$entity,
$fieldName
);
$property = $this->getProperty($classReflection, $propertyName);
$annotation = $this->annotationReader->getPropertyAnnotation($property, EnumAnnotation::class);
if ($annotation !== null) {
$property->setAccessible(true);
$scalarValue = $property->getValue($object);
if (is_scalar($scalarValue)) {
$enumClass = $metadata->fullyQualifiedClassName($annotation->class);
if (!is_a($enumClass, Enum::class, true)) {
throw new \Consistence\Doctrine\Enum\NotEnumException($enumClass);
}
$enum = $enumClass::get($scalarValue);
$property->setValue($object, $enum);
$entityManager->getUnitOfWork()->setOriginalEntityProperty(
spl_object_hash($entity),
$fieldName,
$enum
);
}
}
}
/**
* @param \ReflectionClass $classReflection
* @param string $propertyName
* @return \ReflectionProperty
*/
private function getProperty(ReflectionClass $classReflection, $propertyName)
{
try {
return $classReflection->getProperty($propertyName);
} catch (\ReflectionException $e) {
$parentClass = $classReflection->getParentClass();
if ($parentClass === false) {
throw $e;
}
return $this->getProperty($parentClass, $propertyName);
}
}
/**
* @param \Doctrine\ORM\EntityManager $entityManager
* @param object $object
* @param string $fieldName
* @return mixed[]
*/
private function resolveObjectAndProperty(
EntityManager $entityManager,
$object,
$fieldName
)
{
$metadata = $this->getClassMetadata($entityManager, get_class($object));
$parts = explode(self::EMBEDDED_SEPARATOR, $fieldName);
if (count($parts) === 1) {
return [
$object,
$metadata->getReflectionClass(),
$fieldName
];
}
$propertyName = array_shift($parts);
$property = $metadata->getReflectionClass()->getProperty($propertyName);
$property->setAccessible(true);
return $this->resolveObjectAndProperty(
$entityManager,
$property->getValue($object),
implode(self::EMBEDDED_SEPARATOR, $parts)
);
}
/**
* @param \Doctrine\ORM\EntityManager $entityManager
* @param string $class
* @return \Doctrine\ORM\Mapping\ClassMetadata
*/
private function getClassMetadata(EntityManager $entityManager, $class)
{
$metadata = $entityManager->getClassMetadata($class);
if (!($metadata instanceof ClassMetadata)) {
throw new \Consistence\Doctrine\Enum\UnsupportedClassMetadataException(get_class($metadata));
}
return $metadata;
}
}