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