|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\Model; |
|
3
|
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
use Ajir\RabbitMqSqlBundle\DataMapper\DataMapper; |
|
6
|
|
|
use Ajir\RabbitMqSqlBundle\DataTransformer\DataTransformer; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Data container model for any data message type |
|
10
|
|
|
* |
|
11
|
|
|
* @author Florian Ajir <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Entity implements EntityInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Table name |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $table; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Data array only |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $data; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Associative array ( field => value ) |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $identifier; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Many to many relations array |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $manyToMany; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* One to many relations array |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $oneToMany; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Many to one relations array |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $manyToOne; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* One to one relations array |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $oneToOne; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor, set the attributes from array |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $data entity prepared data from DataTransformer |
|
68
|
|
|
*/ |
|
69
|
7 |
|
public function __construct(array $data) |
|
70
|
|
|
{ |
|
71
|
7 |
|
$this->identifier = null; |
|
|
|
|
|
|
72
|
7 |
|
if (array_key_exists(DataTransformer::IDENTIFIER_KEY, $data)) { |
|
73
|
5 |
|
$identifierKey = $data[DataTransformer::IDENTIFIER_KEY]; |
|
74
|
5 |
|
if (isset($data[$identifierKey])) { |
|
75
|
5 |
|
$this->identifier = array( |
|
76
|
5 |
|
$identifierKey => $data[$identifierKey] |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
5 |
|
unset($data[DataTransformer::IDENTIFIER_KEY]); |
|
80
|
|
|
} |
|
81
|
7 |
|
if (array_key_exists(DataTransformer::TABLE_KEY, $data)) { |
|
82
|
6 |
|
$this->table = $data[DataTransformer::TABLE_KEY]; |
|
83
|
6 |
|
unset($data[DataTransformer::TABLE_KEY]); |
|
84
|
|
|
} |
|
85
|
7 |
|
if (!isset($this->table)) { |
|
86
|
1 |
|
throw new InvalidArgumentException("Missing table or discriminator property in mapping"); |
|
87
|
|
|
} |
|
88
|
6 |
|
$this->oneToOne = array(); |
|
89
|
6 |
|
$this->manyToOne = array(); |
|
90
|
6 |
|
$this->oneToMany = array(); |
|
91
|
6 |
|
$this->manyToMany = array(); |
|
92
|
6 |
|
if (array_key_exists(DataTransformer::RELATED_KEY, $data)) { |
|
93
|
4 |
|
$this->initializeRelated($data[DataTransformer::RELATED_KEY]); |
|
94
|
4 |
|
unset($data[DataTransformer::RELATED_KEY]); |
|
95
|
|
|
} |
|
96
|
6 |
|
$this->data = $data; |
|
97
|
6 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param array $related |
|
101
|
|
|
*/ |
|
102
|
4 |
|
private function initializeRelated($related) |
|
103
|
|
|
{ |
|
104
|
4 |
|
if (isset($related[DataMapper::RELATION_ONE_TO_ONE])) { |
|
105
|
1 |
|
$this->oneToOne = $related[DataMapper::RELATION_ONE_TO_ONE]; |
|
106
|
|
|
} |
|
107
|
4 |
|
if (isset($related[DataMapper::RELATION_MANY_TO_ONE])) { |
|
108
|
4 |
|
$this->manyToOne = $related[DataMapper::RELATION_MANY_TO_ONE]; |
|
109
|
|
|
} |
|
110
|
4 |
|
if (isset($related[DataMapper::RELATION_ONE_TO_MANY])) { |
|
111
|
1 |
|
$this->oneToMany = $related[DataMapper::RELATION_ONE_TO_MANY]; |
|
112
|
|
|
} |
|
113
|
4 |
|
if (isset($related[DataMapper::RELATION_MANY_TO_MANY])) { |
|
114
|
1 |
|
$this->manyToMany = $related[DataMapper::RELATION_MANY_TO_MANY]; |
|
115
|
|
|
} |
|
116
|
4 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the table name |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function getTable() |
|
124
|
|
|
{ |
|
125
|
3 |
|
return $this->table; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the entity identifier => value array |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function getIdentifier() |
|
134
|
|
|
{ |
|
135
|
2 |
|
return $this->identifier; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set the entity identifier array |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $identifier entity identifier associative array (id => value) |
|
142
|
|
|
* |
|
143
|
|
|
* @return self |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function setIdentifier(array $identifier) |
|
146
|
|
|
{ |
|
147
|
1 |
|
$this->identifier = $identifier; |
|
148
|
|
|
|
|
149
|
1 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the entity data set |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function getData() |
|
158
|
|
|
{ |
|
159
|
1 |
|
return $this->data; |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get the entity manyToOne relations array |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function getManyToOneRelations() |
|
168
|
|
|
{ |
|
169
|
1 |
|
return $this->manyToOne; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get the entity manyToMany relations array |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public function getManyToManyRelations() |
|
178
|
|
|
{ |
|
179
|
1 |
|
return $this->manyToMany; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get the entity oneToMany relations array |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function getOneToManyRelations() |
|
188
|
|
|
{ |
|
189
|
1 |
|
return $this->oneToMany; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get the entity oneToOne relations array |
|
194
|
|
|
* |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public function getOneToOneRelations() |
|
198
|
|
|
{ |
|
199
|
1 |
|
return $this->oneToOne; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Merge new data on current entity data |
|
204
|
|
|
* |
|
205
|
|
|
* @param array $data data set to merge |
|
206
|
|
|
* |
|
207
|
|
|
* @return self |
|
208
|
|
|
*/ |
|
209
|
1 |
|
public function addDataSet(array $data) |
|
210
|
|
|
{ |
|
211
|
1 |
|
$this->data = array_merge($this->data, $data); |
|
212
|
|
|
|
|
213
|
1 |
|
return $this; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get an entity property from data |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $property data index to return |
|
220
|
|
|
* |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
2 |
|
public function getProperty($property) |
|
224
|
|
|
{ |
|
225
|
2 |
|
$value = null; |
|
226
|
2 |
|
if (array_key_exists($property, $this->data)) { |
|
227
|
2 |
|
$value = $this->data[$property]; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
2 |
|
return $value; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..