1
|
|
|
<?php |
2
|
|
|
namespace T4webInfrastructure; |
3
|
|
|
|
4
|
|
|
class Config |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @var array |
8
|
|
|
*/ |
9
|
|
|
protected $entityMap = []; |
10
|
|
|
|
11
|
|
|
public function __construct(array $entityMap) |
12
|
|
|
{ |
13
|
|
|
$this->entityMap = $entityMap; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $entityName |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function getTable($entityName) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if (!isset($this->entityMap[$entityName]['table'])) { |
24
|
|
|
throw new ConfigException(sprintf("entity_map not configured for %s", $entityName)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->entityMap[$entityName]['table']; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $entityName |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function getEntityClass($entityName) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
if (!isset($this->entityMap[$entityName]['entityClass'])) { |
37
|
|
|
throw new ConfigException(sprintf("entity_map[entityClass] not configured for %s", $entityName)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->entityMap[$entityName]['entityClass']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $entityName |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getCollectionClass($entityName) |
48
|
|
|
{ |
49
|
|
|
if (!isset($this->entityMap[$entityName]['collectionClass'])) { |
50
|
|
|
return 'ArrayObject'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->entityMap[$entityName]['collectionClass']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $entityName |
58
|
|
|
* @param string $attribute |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getFiled($entityName, $attribute) |
62
|
|
|
{ |
63
|
|
View Code Duplication |
if (!isset($this->entityMap[$entityName]['columnsAsAttributesMap'])) { |
|
|
|
|
64
|
|
|
throw new ConfigException(sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$field = array_search($attribute, $this->entityMap[$entityName]['columnsAsAttributesMap']); |
68
|
|
|
|
69
|
|
|
if (!$field) { |
70
|
|
|
throw new ConfigException( |
71
|
|
|
sprintf("attributes %s not exists in entity_map[columnsAsAttributesMap] config", $attribute) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $field; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $entityName |
80
|
|
|
* @param string $joinEntityName |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getRelationExpression($entityName, $joinEntityName) |
85
|
|
|
{ |
86
|
|
View Code Duplication |
if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName])) { |
|
|
|
|
87
|
|
|
throw new ConfigException( |
88
|
|
|
sprintf( |
89
|
|
|
"entity_map for %s not configured with relation %s", |
90
|
|
|
$entityName, |
91
|
|
|
$joinEntityName |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0]) |
97
|
|
|
|| !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])) { |
98
|
|
|
throw new ConfigException( |
99
|
|
|
sprintf( |
100
|
|
|
"entity_map for %s with relation %s must be array [field, joined-filed]", |
101
|
|
|
$entityName, |
102
|
|
|
$joinEntityName |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->entityMap[$entityName]['relations'][$joinEntityName][0].' = ' |
108
|
|
|
. $this->entityMap[$entityName]['relations'][$joinEntityName][1]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $entityName |
113
|
|
|
* @param string $joinEntityName |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isRelationManyToMany($entityName, $joinEntityName) |
117
|
|
|
{ |
118
|
|
View Code Duplication |
if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName])) { |
|
|
|
|
119
|
|
|
throw new ConfigException( |
120
|
|
|
sprintf( |
121
|
|
|
"entity_map for %s not configured with relation %s", |
122
|
|
|
$entityName, |
123
|
|
|
$joinEntityName |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0]) |
129
|
|
|
|| !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1]) |
130
|
|
|
|| !isset($this->entityMap[$entityName]['relations'][$joinEntityName][2])) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $entityName |
139
|
|
|
* @param string $joinEntityName |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function getRelationManyToMany($entityName, $joinEntityName) |
143
|
|
|
{ |
144
|
|
|
if (!$this->isRelationManyToMany($entityName, $joinEntityName)) { |
145
|
|
|
throw new ConfigException( |
146
|
|
|
sprintf( |
147
|
|
|
"entity_map for %s with relation %s must be array [link-table, field, joined-filed]", |
148
|
|
|
$entityName, |
149
|
|
|
$joinEntityName |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return [ |
155
|
|
|
$this->entityMap[$entityName]['relations'][$joinEntityName][0], |
156
|
|
|
$this->entityMap[$entityName]['relations'][$joinEntityName][1], |
157
|
|
|
$this->entityMap[$entityName]['relations'][$joinEntityName][2], |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $entityName |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getColumnsAsAttributesMap($entityName) |
167
|
|
|
{ |
168
|
|
View Code Duplication |
if (!isset($this->entityMap[$entityName]['columnsAsAttributesMap'])) { |
|
|
|
|
169
|
|
|
throw new ConfigException( |
170
|
|
|
sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
if (!is_array($this->entityMap[$entityName]['columnsAsAttributesMap'])) { |
|
|
|
|
175
|
|
|
throw new ConfigException( |
176
|
|
|
sprintf("entity_map[columnsAsAttributesMap] for %s must be array", $entityName) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->entityMap[$entityName]['columnsAsAttributesMap']; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $entityName |
185
|
|
|
* @return string|null |
186
|
|
|
*/ |
187
|
|
|
public function getPrimaryKey($entityName) |
188
|
|
|
{ |
189
|
|
|
if (isset($this->entityMap[$entityName]['primaryKey'])) { |
190
|
|
|
return $this->entityMap[$entityName]['primaryKey']; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $entityName |
196
|
|
|
* @return string|null |
197
|
|
|
*/ |
198
|
|
|
public function getSequence($entityName) |
199
|
|
|
{ |
200
|
|
|
if (isset($this->entityMap[$entityName]['sequence'])) { |
201
|
|
|
return $this->entityMap[$entityName]['sequence']; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $entityName |
207
|
|
|
* @return string|null |
208
|
|
|
*/ |
209
|
|
|
public function getCriteriaMap($entityName) |
210
|
|
|
{ |
211
|
|
|
if (isset($this->entityMap[$entityName]['criteriaMap'])) { |
212
|
|
|
return $this->entityMap[$entityName]['criteriaMap']; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $entityName |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function getNamespace($entityName) |
221
|
|
|
{ |
222
|
|
|
if (isset($this->entityMap[$entityName]['namespace'])) { |
223
|
|
|
return $this->entityMap[$entityName]['namespace']; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return "{$entityName}s\\$entityName"; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function getCustomCriteriaClass($entityName, $criteriaName) |
230
|
|
|
{ |
231
|
|
|
if (class_exists($criteriaName)) { |
232
|
|
|
return $criteriaName; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$entityNamespace = $this->getNamespace($entityName); |
236
|
|
|
$className = "$entityNamespace\\Infrastructure\\Criteria\\$criteriaName"; |
237
|
|
|
|
238
|
|
|
return $className; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.