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 class-string |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
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 | $entityClass = ClassUtils::getRealClass($this->entityClass); |
|
63 | 14 | Assert::classExists($entityClass); |
|
64 | 14 | $this->entityClass = $entityClass; |
|
65 | } |
||
66 | } |
||
67 | |||
68 | 1 | public function getEntity() |
|
69 | { |
||
70 | 1 | return $this->entity; |
|
71 | } |
||
72 | |||
73 | /** @return class-string */ |
||
0 ignored issues
–
show
|
|||
74 | 2 | public function getEntityClass(): string |
|
75 | { |
||
76 | 2 | return $this->entityClass; |
|
77 | } |
||
78 | |||
79 | 1 | public function registerValue(string $id, $value): void |
|
80 | { |
||
81 | 1 | $this->registeredValues[$id] = $value; |
|
82 | } |
||
83 | |||
84 | 1 | public function hasRegisteredValue(string $id): bool |
|
85 | { |
||
86 | 1 | return array_key_exists($id, $this->registeredValues); |
|
87 | } |
||
88 | |||
89 | 2 | public function getRegisteredValue(string $id) |
|
90 | { |
||
91 | 2 | if (!array_key_exists($id, $this->registeredValues)) { |
|
92 | 1 | throw new InvalidMappingException(sprintf( |
|
93 | "Tried to load unknown value '%s' from register!", |
||
94 | $id |
||
95 | )); |
||
96 | } |
||
97 | |||
98 | 1 | return $this->registeredValues[$id]; |
|
99 | } |
||
100 | |||
101 | 1 | public function getObjectHydrationStack(): array |
|
102 | { |
||
103 | 1 | return $this->hydrationStack; |
|
104 | } |
||
105 | |||
106 | 2 | public function pushOnObjectHydrationStack($value): void |
|
107 | { |
||
108 | 2 | $this->hydrationStack[] = $value; |
|
109 | } |
||
110 | |||
111 | 3 | public function popFromObjectHydrationStack() |
|
112 | { |
||
113 | 3 | Assert::greaterThan(count($this->hydrationStack), 1); |
|
114 | |||
115 | 2 | return array_pop($this->hydrationStack); |
|
116 | } |
||
117 | |||
118 | 2 | public function getEntityManager(): EntityManagerInterface |
|
119 | { |
||
120 | 2 | return $this->entityManager; |
|
121 | } |
||
122 | |||
123 | } |
||
124 |