1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\RDMBundle\Hydration; |
14
|
|
|
|
15
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
use Doctrine\Common\Util\ClassUtils; |
18
|
|
|
use Addiks\RDMBundle\Exception\InvalidMappingException; |
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
20
|
|
|
|
21
|
|
|
final class HydrationContext implements HydrationContextInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
private $entity; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $entityClass; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array<string, mixed> |
36
|
|
|
*/ |
37
|
|
|
private $registeredValues = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array<int, mixed> |
41
|
|
|
*/ |
42
|
|
|
private $hydrationStack = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var EntityManagerInterface |
46
|
|
|
*/ |
47
|
|
|
private $entityManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param object $entity |
51
|
|
|
*/ |
52
|
14 |
|
public function __construct($entity, EntityManagerInterface $entityManager) |
53
|
|
|
{ |
54
|
14 |
|
Assert::true(is_object($entity)); |
55
|
|
|
|
56
|
14 |
|
$this->entity = $entity; |
57
|
14 |
|
$this->entityClass = get_class($entity); |
58
|
14 |
|
$this->hydrationStack[] = $entity; |
59
|
14 |
|
$this->entityManager = $entityManager; |
60
|
|
|
|
61
|
14 |
|
if (class_exists(ClassUtils::class)) { |
62
|
14 |
|
$this->entityClass = ClassUtils::getRealClass($this->entityClass); |
63
|
|
|
} |
64
|
14 |
|
} |
65
|
|
|
|
66
|
1 |
|
public function getEntity() |
67
|
|
|
{ |
68
|
1 |
|
return $this->entity; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function getEntityClass(): string |
72
|
|
|
{ |
73
|
2 |
|
return $this->entityClass; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function registerValue(string $id, $value): void |
77
|
|
|
{ |
78
|
1 |
|
$this->registeredValues[$id] = $value; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
public function hasRegisteredValue(string $id): bool |
82
|
|
|
{ |
83
|
1 |
|
return array_key_exists($id, $this->registeredValues); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function getRegisteredValue(string $id) |
87
|
|
|
{ |
88
|
2 |
|
if (!array_key_exists($id, $this->registeredValues)) { |
89
|
1 |
|
throw new InvalidMappingException(sprintf( |
90
|
1 |
|
"Tried to load unknown value '%s' from register!", |
91
|
1 |
|
$id |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $this->registeredValues[$id]; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function getObjectHydrationStack(): array |
99
|
|
|
{ |
100
|
1 |
|
return $this->hydrationStack; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function pushOnObjectHydrationStack($value): void |
104
|
|
|
{ |
105
|
2 |
|
$this->hydrationStack[] = $value; |
106
|
2 |
|
} |
107
|
|
|
|
108
|
3 |
|
public function popFromObjectHydrationStack() |
109
|
|
|
{ |
110
|
3 |
|
Assert::greaterThan(count($this->hydrationStack), 1); |
111
|
|
|
|
112
|
2 |
|
return array_pop($this->hydrationStack); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function getEntityManager(): EntityManagerInterface |
116
|
|
|
{ |
117
|
2 |
|
return $this->entityManager; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|