1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/doctrine-fixture-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\DoctrineObjectEngine; |
7
|
|
|
|
8
|
|
|
use Nnx\JmsSerializerModule\DataContainer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Metadata |
12
|
|
|
* |
13
|
|
|
* @package Nnx\JmsSerializerModule\DoctrineObjectEngine |
14
|
|
|
*/ |
15
|
|
|
class Metadata implements MetadataInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Ассоциция ведет только на одну сущность |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
const SINGLE_VALUE_ASSOCIATION = 'singleValuedAssociation'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Ассоциция ведет только на одну сущность |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const COLLECTION_VALUE_ASSOCIATION = 'collectionValuedAssociation'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Ключем является id контейнера с данными для фикстуры, а значением является имя класса сущности |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $dataContainerIdToEntityClassName = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* id контейнера с данными, для которого стоит искать уже созданную сущность в базе данных |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $candidatesForSearchInDb = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Ключем является id контейнера с данными, а значением массив со следующей структурой: |
47
|
|
|
* имя ассоциации => массив с id контейнеров, на которые ведет ассоциация |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $associationMap = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Добавляет элемент в карту ассоциаций |
55
|
|
|
* |
56
|
|
|
* @param DataContainer\EntityInterface $fromDataItem |
57
|
|
|
* @param DataContainer\EntityInterface $toDataItem |
58
|
|
|
* @param $association |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function addAssociationInfo(DataContainer\EntityInterface $fromDataItem, DataContainer\EntityInterface $toDataItem, DataContainer\Association $association) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$fromDataItemId = $fromDataItem->getId(); |
65
|
|
|
if (!array_key_exists($fromDataItemId, $this->associationMap)) { |
66
|
|
|
$this->associationMap[$fromDataItemId] = []; |
67
|
|
|
} |
68
|
|
|
$associationName = $association->getName(); |
69
|
|
|
if (!array_key_exists($associationName, $this->associationMap[$fromDataItemId])) { |
70
|
|
|
$this->associationMap[$fromDataItemId][$associationName] = []; |
71
|
|
|
} |
72
|
|
|
$toDataItemId = $toDataItem->getId(); |
73
|
|
|
$this->associationMap[$fromDataItemId][$associationName][$toDataItemId] = $toDataItemId; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Добавляет обратную связь |
80
|
|
|
* |
81
|
|
|
* @param DataContainer\EntityInterface $fromDataItem |
82
|
|
|
* @param DataContainer\EntityInterface $toDataItem |
83
|
|
|
* @param $associationName |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function addReverseAssociationInfo(DataContainer\EntityInterface $fromDataItem, DataContainer\EntityInterface $toDataItem, $associationName) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$fromDataItemId = $fromDataItem->getId(); |
90
|
|
|
if (!array_key_exists($fromDataItemId, $this->associationMap)) { |
91
|
|
|
$this->associationMap[$fromDataItemId] = []; |
92
|
|
|
} |
93
|
|
|
if (!array_key_exists($associationName, $this->associationMap[$fromDataItemId])) { |
94
|
|
|
$this->associationMap[$fromDataItemId][$associationName] = []; |
95
|
|
|
} |
96
|
|
|
$toDataItemId = $toDataItem->getId(); |
97
|
|
|
$this->associationMap[$fromDataItemId][$associationName][$toDataItemId] = $toDataItemId; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Связывает id контейнера с данными, с именем класса сущности |
104
|
|
|
* |
105
|
|
|
* @param DataContainer\EntityInterface $dataItem |
106
|
|
|
* @param string $entityClassName |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function linkDataContainerToEntityClassName(DataContainer\EntityInterface $dataItem, $entityClassName) |
111
|
|
|
{ |
112
|
|
|
$this->dataContainerIdToEntityClassName[$dataItem->getId()] = $entityClassName; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Возвращает имя класса сущности, который привязан к контейнеру |
121
|
|
|
* |
122
|
|
|
* @param DataContainer\EntityInterface $dataItem |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
* @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function getEntityClassNameByDataContainer(DataContainer\EntityInterface $dataItem) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$dataItemId = $dataItem->getId(); |
130
|
|
|
if (!array_key_exists($dataItemId, $this->dataContainerIdToEntityClassName)) { |
131
|
|
|
$errMsg = sprintf('Entity class name not found for data container:#id %s', $dataItemId); |
132
|
|
|
throw new Exception\RuntimeException($errMsg); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->dataContainerIdToEntityClassName[$dataItemId]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Возвращает данные о асоцицих, на основе контейнера |
141
|
|
|
* |
142
|
|
|
* @param DataContainer\EntityInterface $dataItem |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
* @throws \Nnx\JmsSerializerModule\DoctrineObjectEngine\Exception\RuntimeException |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function getAssociationsForEntity(DataContainer\EntityInterface $dataItem) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$dataItemId = $dataItem->getId(); |
150
|
|
|
if (!array_key_exists($dataItemId, $this->associationMap)) { |
151
|
|
|
$errMsg = sprintf('Associations for data container id: %s not found', $dataItemId); |
152
|
|
|
throw new Exception\RuntimeException($errMsg); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->associationMap[$dataItemId]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Проверяет есть ли для контейнера с данными связанная ассоциация |
160
|
|
|
* |
161
|
|
|
* @param DataContainer\EntityInterface $dataItem |
162
|
|
|
* |
163
|
|
|
* @return boolean |
164
|
|
|
*/ |
165
|
|
|
public function hasAssociationsForEntity(DataContainer\EntityInterface $dataItem) |
166
|
|
|
{ |
167
|
|
|
return array_key_exists($dataItem->getId(), $this->associationMap); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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.