1
|
|
|
<?php |
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\DataMapper; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class DataMapper |
8
|
|
|
* |
9
|
|
|
* @author Florian Ajir <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class DataMapper implements DataMapperInterface |
12
|
|
|
{ |
13
|
|
|
const MAPPING_KEY_TABLE = 'table'; |
14
|
|
|
const MAPPING_KEY_IDENTIFIER = 'identifier'; |
15
|
|
|
const MAPPING_KEY_LENGTH = 'length'; |
16
|
|
|
const MAPPING_KEY_COLUMN = 'column'; |
17
|
|
|
const MAPPING_KEY_FIELDS = 'fields'; |
18
|
|
|
const MAPPING_KEY_TYPE = 'type'; |
19
|
|
|
const MAPPING_KEY_NULLABLE = 'nullable'; |
20
|
|
|
const MAPPING_KEY_DISCRIMINATOR = 'discriminator'; |
21
|
|
|
|
22
|
|
|
const RELATION_ONE_TO_ONE = 'oneToOne'; |
23
|
|
|
const RELATION_ONE_TO_MANY = 'oneToMany'; |
24
|
|
|
const RELATION_MANY_TO_ONE = 'manyToOne'; |
25
|
|
|
const RELATION_MANY_TO_MANY = 'manyToMany'; |
26
|
|
|
|
27
|
|
|
const RELATION_KEY_TARGET_ENTITY = 'targetEntity'; |
28
|
|
|
const RELATION_KEY_JOIN_COLUMN = 'joinColumn'; |
29
|
|
|
const RELATION_KEY_JOIN_COLUMN_NAME = 'name'; |
30
|
|
|
const RELATION_KEY_JOIN_COLUMN_REFERENCED_COLUMN_NAME = 'referencedColumnName'; |
31
|
|
|
const RELATION_KEY_JOIN_TABLE = 'joinTable'; |
32
|
|
|
const RELATION_KEY_INVERSE_JOIN_COLUMN = 'inverseJoinColumn'; |
33
|
|
|
|
34
|
|
|
const REFERENCES_KEY = 'references'; |
35
|
|
|
const WHERE_KEY = 'where'; |
36
|
|
|
const REMOVE_REFERENCED_KEY = 'removeReferenced'; |
37
|
|
|
const FIXED_VALUE = 'value'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $mapping; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $mapping |
46
|
|
|
*/ |
47
|
26 |
|
public function __construct(array $mapping) |
48
|
|
|
{ |
49
|
26 |
|
$this->mapping = $mapping; |
50
|
26 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $entity |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
8 |
|
public function getFieldsName($entity) |
58
|
|
|
{ |
59
|
8 |
|
$fieldsName = array(); |
60
|
8 |
|
if (isset($this->mapping[$entity][self::MAPPING_KEY_FIELDS])) { |
61
|
8 |
|
$fieldsName = array_keys($this->mapping[$entity][self::MAPPING_KEY_FIELDS]); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
return $fieldsName; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $entity |
69
|
|
|
* @param string $field |
70
|
|
|
* |
71
|
|
|
* @return int|null |
72
|
|
|
*/ |
73
|
7 |
|
public function getFieldMaxLength($entity, $field) |
74
|
|
|
{ |
75
|
7 |
|
$maxLength = null; |
76
|
7 |
|
$mapping = $this->getFieldMapping($entity, $field); |
77
|
7 |
|
if (isset($mapping[self::MAPPING_KEY_LENGTH])) { |
78
|
6 |
|
if (!is_numeric($mapping[self::MAPPING_KEY_LENGTH])) { |
79
|
1 |
|
throw new InvalidArgumentException( |
80
|
|
|
"$entity.$field " . |
81
|
1 |
|
self::MAPPING_KEY_LENGTH . |
82
|
1 |
|
" mapping property must be a numeric value." |
83
|
|
|
); |
84
|
|
|
} |
85
|
5 |
|
$maxLength = intval($mapping[self::MAPPING_KEY_LENGTH]); |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
return $maxLength; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $entity |
93
|
|
|
* @param string $field |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
17 |
|
public function getFieldMapping($entity, $field) |
98
|
|
|
{ |
99
|
17 |
|
$mapping = array(); |
100
|
17 |
|
if (isset($this->mapping[$entity][self::MAPPING_KEY_FIELDS][$field])) { |
101
|
17 |
|
$mapping = $this->mapping[$entity][self::MAPPING_KEY_FIELDS][$field]; |
102
|
|
|
} |
103
|
|
|
|
104
|
17 |
|
return $mapping; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $entity |
109
|
|
|
* @param string $field |
110
|
|
|
* |
111
|
|
|
* @return string|null |
112
|
|
|
*/ |
113
|
4 |
View Code Duplication |
public function getFieldType($entity, $field) |
|
|
|
|
114
|
|
|
{ |
115
|
4 |
|
$type = null; |
116
|
4 |
|
$mapping = $this->getFieldMapping($entity, $field); |
117
|
4 |
|
if (isset($mapping[self::MAPPING_KEY_TYPE])) { |
118
|
4 |
|
$type = $mapping[self::MAPPING_KEY_TYPE]; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
return $type; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Is the field nullable from mapping (default:true) |
126
|
|
|
* |
127
|
|
|
* @param string $entity |
128
|
|
|
* @param string $field |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
3 |
|
public function isFieldNullable($entity, $field) |
133
|
|
|
{ |
134
|
3 |
|
$nullable = true; |
135
|
3 |
|
$mapping = $this->getFieldMapping($entity, $field); |
136
|
3 |
|
if (isset($mapping[self::MAPPING_KEY_NULLABLE])) { |
137
|
|
|
$nullable = |
138
|
3 |
|
$mapping[self::MAPPING_KEY_NULLABLE] !== false |
139
|
3 |
|
&& $mapping[self::MAPPING_KEY_NULLABLE] !== 'false'; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
return $nullable; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $container |
147
|
|
|
* @param string $entity |
148
|
|
|
* |
149
|
|
|
* @return string|null |
150
|
|
|
*/ |
151
|
4 |
|
public function getRelation($container, $entity) |
152
|
|
|
{ |
153
|
|
|
$relations = array( |
154
|
4 |
|
self::RELATION_MANY_TO_MANY, |
155
|
4 |
|
self::RELATION_MANY_TO_ONE, |
156
|
4 |
|
self::RELATION_ONE_TO_ONE, |
157
|
4 |
|
self::RELATION_ONE_TO_MANY, |
158
|
|
|
); |
159
|
|
|
|
160
|
4 |
|
foreach ($relations as $relation) { |
161
|
4 |
|
if (isset($this->mapping[$container][$relation][$entity])) { |
162
|
4 |
|
return $relation; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $entity |
171
|
|
|
* |
172
|
|
|
* @return string|null |
173
|
|
|
*/ |
174
|
11 |
View Code Duplication |
public function getIdentifier($entity) |
|
|
|
|
175
|
|
|
{ |
176
|
11 |
|
$identifier = null; |
177
|
11 |
|
if (isset($this->mapping[$entity][self::MAPPING_KEY_IDENTIFIER])) { |
178
|
8 |
|
$identifier = $this->mapping[$entity][self::MAPPING_KEY_IDENTIFIER]; |
179
|
|
|
} |
180
|
|
|
|
181
|
11 |
|
return $identifier; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $container |
186
|
|
|
* @param string $entity |
187
|
|
|
* @param string $relation |
188
|
|
|
* |
189
|
|
|
* @return string|null |
190
|
|
|
*/ |
191
|
3 |
View Code Duplication |
public function getTargetEntity($container, $entity, $relation) |
|
|
|
|
192
|
|
|
{ |
193
|
3 |
|
$targetEntity = null; |
194
|
3 |
|
$infos = $this->getRelationInfos($container, $entity, $relation); |
195
|
3 |
|
if (isset($infos[self::RELATION_KEY_TARGET_ENTITY])) { |
196
|
3 |
|
$targetEntity = $infos[self::RELATION_KEY_TARGET_ENTITY]; |
197
|
|
|
} |
198
|
|
|
|
199
|
3 |
|
return $targetEntity; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $container |
204
|
|
|
* @param string $entity |
205
|
|
|
* @param string $relation |
206
|
|
|
* |
207
|
|
|
* @return array|null |
208
|
|
|
*/ |
209
|
5 |
|
public function getRelationInfos($container, $entity, $relation) |
210
|
|
|
{ |
211
|
5 |
|
$details = null; |
212
|
5 |
|
if (isset($this->mapping[$container][$relation][$entity])) { |
213
|
5 |
|
$details = $this->mapping[$container][$relation][$entity]; |
214
|
5 |
|
$details[self::MAPPING_KEY_TABLE] = $this->getTableName($details[self::RELATION_KEY_TARGET_ENTITY]); |
215
|
|
|
} |
216
|
|
|
|
217
|
5 |
|
return $details; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $entity |
222
|
|
|
* |
223
|
|
|
* @return string|null |
224
|
|
|
*/ |
225
|
14 |
View Code Duplication |
public function getTableName($entity) |
|
|
|
|
226
|
|
|
{ |
227
|
14 |
|
$tableName = null; |
228
|
14 |
|
if (isset($this->mapping[$entity][self::MAPPING_KEY_TABLE])) { |
229
|
12 |
|
$tableName = $this->mapping[$entity][self::MAPPING_KEY_TABLE]; |
230
|
|
|
} |
231
|
|
|
|
232
|
14 |
|
return $tableName; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $entity |
237
|
|
|
* |
238
|
|
|
* @return string|null |
239
|
|
|
*/ |
240
|
11 |
View Code Duplication |
public function getDiscriminator($entity) |
|
|
|
|
241
|
|
|
{ |
242
|
11 |
|
$discr = null; |
243
|
11 |
|
if (isset($this->mapping[$entity][self::MAPPING_KEY_DISCRIMINATOR])) { |
244
|
2 |
|
$discr = $this->mapping[$entity][self::MAPPING_KEY_DISCRIMINATOR]; |
245
|
|
|
} |
246
|
|
|
|
247
|
11 |
|
return $discr; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $container |
252
|
|
|
* @param string $entity |
253
|
|
|
* @param string $relation |
254
|
|
|
* |
255
|
|
|
* @return string|null |
256
|
|
|
*/ |
257
|
1 |
View Code Duplication |
public function getJoinTable($container, $entity, $relation) |
|
|
|
|
258
|
|
|
{ |
259
|
1 |
|
$joinTable = null; |
260
|
1 |
|
$infos = $this->getRelationInfos($container, $entity, $relation); |
261
|
1 |
|
if (isset($infos[self::RELATION_KEY_JOIN_TABLE])) { |
262
|
1 |
|
$joinTable = $infos[self::RELATION_KEY_JOIN_TABLE]; |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
1 |
|
return $joinTable; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $relation |
270
|
|
|
* |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
3 |
|
public function isCollection($relation) |
274
|
|
|
{ |
275
|
3 |
|
$collections = array(self::RELATION_ONE_TO_MANY, self::RELATION_MANY_TO_MANY); |
276
|
|
|
|
277
|
3 |
|
return in_array($relation, $collections); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get the mapping column => fixed_value for a field |
282
|
|
|
* |
283
|
|
|
* @param string $entity |
284
|
|
|
* @param string $field |
285
|
|
|
* |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
7 |
|
public function getFixedFieldMapping($entity, $field) |
289
|
|
|
{ |
290
|
7 |
|
$mapping = array(); |
291
|
7 |
|
$fixed = $this->getFixedValue($entity, $field); |
292
|
7 |
|
if (isset($fixed)) { |
293
|
|
|
$mapping = array( |
294
|
2 |
|
$this->getFieldColumn($entity, $field) => $fixed |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
7 |
|
return $mapping; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get the fixed field value |
303
|
|
|
* |
304
|
|
|
* @param string $entity |
305
|
|
|
* @param string $field |
306
|
|
|
* |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
7 |
|
public function getFixedValue($entity, $field) |
310
|
|
|
{ |
311
|
7 |
|
$fixed = null; |
312
|
7 |
|
if (isset($this->mapping[$entity][self::MAPPING_KEY_FIELDS][$field][self::FIXED_VALUE])) { |
313
|
2 |
|
$fixed = $this->mapping[$entity][self::MAPPING_KEY_FIELDS][$field][self::FIXED_VALUE]; |
314
|
|
|
} |
315
|
|
|
|
316
|
7 |
|
return $fixed; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param string $entity |
321
|
|
|
* @param string $field |
322
|
|
|
* |
323
|
|
|
* @return string|null |
324
|
|
|
*/ |
325
|
10 |
View Code Duplication |
public function getFieldColumn($entity, $field) |
|
|
|
|
326
|
|
|
{ |
327
|
10 |
|
$column = $field; |
328
|
10 |
|
$mapping = $this->getFieldMapping($entity, $field); |
329
|
10 |
|
if (isset($mapping[self::MAPPING_KEY_COLUMN])) { |
330
|
10 |
|
$column = $mapping[self::MAPPING_KEY_COLUMN]; |
331
|
|
|
} |
332
|
|
|
|
333
|
10 |
|
return $column; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
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.