|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Doctrine\ORM\PersistentCollection; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
9
|
|
|
use Symfony\Component\Validator\ObjectInitializerInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Initialiser implements ObjectInitializerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var EntityManagerInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $entityManager; |
|
17
|
|
|
|
|
18
|
|
|
private $visited = []; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var EntityFactoryInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $entityFactory; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(EntityManagerInterface $entityManager, EntityFactoryInterface $entityFactory) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->entityManager = $entityManager; |
|
27
|
|
|
$this->entityFactory = $entityFactory; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initializes an object just before validation. |
|
32
|
|
|
* |
|
33
|
|
|
* @param object $object The object to validate |
|
34
|
|
|
*/ |
|
35
|
|
|
public function initialize($object): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->initialise($object); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function initialise(object $object): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->visited = []; |
|
43
|
|
|
$this->initialiseObject($object); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function initialiseObject(object $object): void |
|
47
|
|
|
{ |
|
48
|
|
|
if (true === $this->isVisited($object)) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
$this->setAsVisited($object); |
|
52
|
|
|
$this->entityManager->initializeObject($object); |
|
53
|
|
|
if ($object instanceof EntityInterface) { |
|
54
|
|
|
$this->entityFactory->initialiseEntity($object); |
|
55
|
|
|
$this->initialiseProperties($object); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function isVisited(object $object): bool |
|
60
|
|
|
{ |
|
61
|
|
|
return isset($this->visited[spl_object_hash($object)]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function setAsVisited(object $object): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->visited[spl_object_hash($object)] = true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function initialiseProperties(EntityInterface $entity): void |
|
70
|
|
|
{ |
|
71
|
|
|
$getters = $entity::getDoctrineStaticMeta()->getGetters(); |
|
72
|
|
|
foreach ($getters as $getter) { |
|
73
|
|
|
try { |
|
74
|
|
|
$got = $entity->$getter(); |
|
75
|
|
|
} catch (\TypeError $e) { |
|
76
|
|
|
//getters for things that have not yet been set will return null |
|
77
|
|
|
//but they might be required. This should be caught by the validation, not cause a type error here |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
if (false === is_object($got)) { |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
if ($got instanceof \Doctrine\ORM\Proxy\Proxy) { |
|
84
|
|
|
$this->initialiseObject($got); |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
if ($got instanceof EntityInterface) { |
|
88
|
|
|
$this->initialiseObject($got); |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
if ($got instanceof PersistentCollection) { |
|
92
|
|
|
$this->initialiseObject($got); |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|