1
|
|
|
<?php |
2
|
|
|
namespace AlgoWeb\PODataLaravel\Models\ObjectMap; |
3
|
|
|
|
4
|
|
|
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association; |
5
|
|
|
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic; |
6
|
|
|
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationPolymorphic; |
7
|
|
|
use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityGubbins; |
8
|
|
|
|
9
|
|
|
class Map |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var EntityGubbins[] |
13
|
|
|
*/ |
14
|
|
|
private $entities; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Association[] |
18
|
|
|
*/ |
19
|
|
|
private $associations; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param EntityGubbins $entity |
23
|
|
|
*/ |
24
|
|
|
public function addEntity(EntityGubbins $entity) |
25
|
|
|
{ |
26
|
|
|
if (!is_array($this->entities)) { |
27
|
|
|
$this->entities = []; |
28
|
|
|
} |
29
|
|
|
$this->entities[$entity->getClassName()] = $entity; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return EntityGubbins[] |
34
|
|
|
*/ |
35
|
|
|
public function getEntities() |
36
|
|
|
{ |
37
|
|
|
return $this->entities; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function resolveEntity($entityClassName) |
41
|
|
|
{ |
42
|
|
|
return array_key_exists($entityClassName, $this->entities) ? $this->entities[$entityClassName] : null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param EntityGubbins[] $entities |
47
|
|
|
*/ |
48
|
|
|
public function setEntities(array $entities) |
49
|
|
|
{ |
50
|
|
|
$this->entities = []; |
51
|
|
|
foreach ($entities as $entity) { |
52
|
|
|
if (!$entity instanceof EntityGubbins) { |
53
|
|
|
throw new \InvalidArgumentException('Entities array must contain only EntityGubbins objects'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
foreach ($entities as $entity) { |
57
|
|
|
$this->entities[$entity->getClassName()] = $entity; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Association[] $associations |
63
|
|
|
*/ |
64
|
|
|
public function setAssociations(array $associations) |
65
|
|
|
{ |
66
|
|
|
foreach ($associations as $association) { |
67
|
|
|
$this->addAssociation($association); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Association $association |
73
|
|
|
*/ |
74
|
|
|
public function addAssociation(Association $association) |
75
|
|
|
{ |
76
|
|
|
if (!is_array($this->associations)) { |
77
|
|
|
$this->associations = []; |
78
|
|
|
} |
79
|
|
|
if ($association instanceof AssociationMonomorphic) { |
80
|
|
|
$this->addAssociationMonomorphic($association); |
81
|
|
|
} elseif ($association instanceof AssociationPolymorphic) { |
82
|
|
|
$this->addAssociationPolymorphic($association); |
83
|
|
|
} else { |
84
|
|
|
throw new \InvalidArgumentException('Association type not yet handled'); |
85
|
|
|
} |
86
|
|
|
$this->associations[] = $association; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Association[] |
91
|
|
|
*/ |
92
|
|
|
public function getAssociations() |
93
|
|
|
{ |
94
|
|
|
return $this->associations; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function isOK() |
101
|
|
|
{ |
102
|
|
|
foreach ($this->entities as $entity) { |
103
|
|
|
if (!$entity->isOK()) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param AssociationMonomorphic $association |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
private function addAssociationMonomorphic(AssociationMonomorphic $association) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$firstClass = $this->entities[$association->getFirst()->getBaseType()]; |
116
|
|
|
$secondClass = $this->entities[$association->getLast()->getBaseType()]; |
117
|
|
|
$firstClass->addAssociation($association); |
118
|
|
|
$secondClass->addAssociation($association, false); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param AssociationPolymorphic $association |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
private function addAssociationPolymorphic(AssociationPolymorphic $association) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$firstClass = $this->entities[$association->getFirst()->getBaseType()]; |
127
|
|
|
$firstClass->addAssociation($association); |
128
|
|
|
foreach ($association->getLast() as $last) { |
129
|
|
|
$secondClass = $this->entities[$last->getBaseType()]; |
130
|
|
|
$secondClass->addAssociation($association, false); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.