1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Factory; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\NotifyPropertyChanged; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\Mapping\GenericFactoryInterface; |
10
|
|
|
|
11
|
|
|
class EntityFactory implements GenericFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EntityValidatorFactory |
15
|
|
|
*/ |
16
|
|
|
private $entityValidatorFactory; |
17
|
|
|
/** |
18
|
|
|
* @var EntityManagerInterface |
19
|
|
|
*/ |
20
|
|
|
private $entityManager; |
21
|
|
|
|
22
|
145 |
|
public function __construct(EntityValidatorFactory $entityValidatorFactory) |
23
|
|
|
{ |
24
|
145 |
|
$this->entityValidatorFactory = $entityValidatorFactory; |
25
|
145 |
|
} |
26
|
|
|
|
27
|
145 |
|
public function setEntityManager(EntityManagerInterface $entityManager) |
28
|
|
|
{ |
29
|
145 |
|
$this->entityManager = $entityManager; |
30
|
145 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Build a new entity with the validator factory preloaded |
34
|
|
|
* |
35
|
|
|
* Optionally pass in an array of property=>value |
36
|
|
|
* |
37
|
|
|
* @param string $entityFqn |
38
|
|
|
* |
39
|
|
|
* @param array $values |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
17 |
|
public function create(string $entityFqn, array $values = []) |
44
|
|
|
{ |
45
|
17 |
|
if (!$this->entityManager instanceof EntityManagerInterface) { |
|
|
|
|
46
|
|
|
throw new \RuntimeException( |
47
|
|
|
'No EntityManager set, this must be set first using setEntityManager()' |
48
|
|
|
); |
49
|
|
|
} |
50
|
17 |
|
$entity = $this->createEntity($entityFqn); |
51
|
17 |
|
$entity->ensureMetaDataIsSet($this->entityManager); |
52
|
17 |
|
$this->addListenerToEntityIfRequired($entity); |
53
|
17 |
|
$this->setEntityValues($entity, $values); |
54
|
|
|
|
55
|
16 |
|
return $entity; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create the Entity |
60
|
|
|
* |
61
|
|
|
* @param string $entityFqn |
62
|
|
|
* |
63
|
|
|
* @return EntityInterface |
64
|
|
|
*/ |
65
|
17 |
|
private function createEntity(string $entityFqn): EntityInterface |
66
|
|
|
{ |
67
|
17 |
|
return new $entityFqn($this->entityValidatorFactory); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generally DSM Entities are using the Notify change tracking policy. |
72
|
|
|
* This ensures that they are fully set up for that |
73
|
|
|
* |
74
|
|
|
* @param EntityInterface $entity |
75
|
|
|
*/ |
76
|
17 |
|
private function addListenerToEntityIfRequired(EntityInterface $entity): void |
77
|
|
|
{ |
78
|
17 |
|
if (!$entity instanceof NotifyPropertyChanged) { |
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
17 |
|
$listener = $this->entityManager->getUnitOfWork(); |
82
|
17 |
|
$entity->addPropertyChangedListener($listener); |
83
|
17 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set all the values, if there are any |
87
|
|
|
* |
88
|
|
|
* @param EntityInterface $entity |
89
|
|
|
* @param array $values |
90
|
|
|
*/ |
91
|
17 |
|
private function setEntityValues(EntityInterface $entity, array $values): void |
92
|
|
|
{ |
93
|
17 |
|
if ([] === $values) { |
94
|
16 |
|
return; |
95
|
|
|
} |
96
|
1 |
|
foreach ($values as $property => $value) { |
97
|
1 |
|
$setter = 'set' . $property; |
98
|
1 |
|
if (!method_exists($entity, $setter)) { |
99
|
1 |
|
throw new \InvalidArgumentException( |
100
|
1 |
|
'The entity ' . \get_class($entity) . ' does not have the setter method ' . $setter |
101
|
1 |
|
. "\n\nmethods: " . \print_r(get_class_methods($entity), true) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
$entity->$setter($value); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getEntity(string $className) |
109
|
|
|
{ |
110
|
|
|
return $this->create($className); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|