1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Entity\Definition\Repository; |
4
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Entity\Definition; |
6
|
|
|
use Isolate\UnitOfWork\Entity\Definition\Property; |
7
|
|
|
use Isolate\UnitOfWork\Entity\Definition\Repository; |
8
|
|
|
use Isolate\UnitOfWork\Exception\InvalidArgumentException; |
9
|
|
|
use Isolate\UnitOfWork\Exception\RuntimeException; |
10
|
|
|
|
11
|
|
|
final class InMemory implements Repository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array|Definition[] |
15
|
|
|
*/ |
16
|
|
|
private $entityDefinitions; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $entityDefinitions |
20
|
|
|
* @throws InvalidArgumentException |
21
|
|
|
*/ |
22
|
|
|
public function __construct($entityDefinitions = []) |
23
|
|
|
{ |
24
|
|
|
$this->entityDefinitions = []; |
25
|
|
|
|
26
|
|
|
if (!is_array($entityDefinitions) && !$entityDefinitions instanceof \Traversable) { |
27
|
|
|
throw new InvalidArgumentException("Entity definition repository require array od traversable collection of entity definitions."); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
foreach ($entityDefinitions as $definition) { |
31
|
|
|
if (!$definition instanceof Definition) { |
32
|
|
|
throw new InvalidArgumentException("Each element in collection needs to be an instance of Isolate\\UnitOfWork\\Entity\\Definition"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->addDefinition($definition); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->validateAssociations(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Definition $entityDefinition |
43
|
|
|
*/ |
44
|
|
|
public function addDefinition(Definition $entityDefinition) |
45
|
|
|
{ |
46
|
|
|
$this->entityDefinitions[(string) $entityDefinition->getClassName()] = $entityDefinition; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $entity |
51
|
|
|
* @return bool |
52
|
|
|
* @throws InvalidArgumentException |
53
|
|
|
*/ |
54
|
|
|
public function hasDefinition($entity) |
55
|
|
|
{ |
56
|
|
|
if (!is_object($entity)) { |
57
|
|
|
throw new InvalidArgumentException("Entity definition repository require objects as arguments for methods."); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return array_key_exists(get_class($entity), $this->entityDefinitions); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $entity |
65
|
|
|
* @return Definition |
66
|
|
|
* @throws RuntimeException |
67
|
|
|
*/ |
68
|
|
|
public function getDefinition($entity) |
69
|
|
|
{ |
70
|
|
|
if (!$this->hasDefinition($entity)) { |
71
|
|
|
throw new RuntimeException(sprintf("Entity definition for \"%s\" does not exists.", get_class($entity))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->entityDefinitions[get_class($entity)]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
private function validateAssociations() |
81
|
|
|
{ |
82
|
|
|
foreach ($this->entityDefinitions as $definition) { |
83
|
|
|
foreach ($definition->getObservedProperties() as $property) { |
84
|
|
|
$this->validateAssociation($definition, $property); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Definition $definition |
91
|
|
|
* @param Property $property |
92
|
|
|
* @throws InvalidArgumentException |
93
|
|
|
*/ |
94
|
|
|
private function validateAssociation(Definition $definition, Property $property) |
95
|
|
|
{ |
96
|
|
|
if ($property->isAssociated()) { |
97
|
|
|
$targetClass = (string) $property->getAssociation()->getTargetClassName(); |
98
|
|
|
if (!array_key_exists($targetClass, $this->entityDefinitions)) { |
99
|
|
|
throw new InvalidArgumentException( |
100
|
|
|
sprintf( |
101
|
|
|
"Entity class \"%s\" used in association of \"%s\" entity does not have definition.", |
102
|
|
|
$targetClass, |
103
|
|
|
(string)$definition->getClassName() |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|