1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata; |
4
|
|
|
|
5
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType; |
6
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType\EndAnonymousType; |
7
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\EntitySetAnonymousType; |
8
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationEndType; |
9
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TAssociationType; |
10
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TConstraintType; |
11
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TDocumentationType; |
12
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityPropertyType; |
13
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType; |
14
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TNavigationPropertyType; |
15
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyRefType; |
16
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\TReferentialConstraintRoleElementType; |
17
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edmx\Edmx; |
18
|
|
|
use JMS\Serializer\SerializerBuilder; |
19
|
|
|
|
20
|
|
|
class MetadataManager |
21
|
|
|
{ |
22
|
|
|
private $V3Edmx = null; |
23
|
|
|
private $oldEdmx = null; |
|
|
|
|
24
|
|
|
private $lastError = null; |
25
|
|
|
private $serializer = null; |
26
|
|
|
|
27
|
|
|
public function __construct($namespaceName = "Data", $containerName = "DefaultContainer") |
28
|
|
|
{ |
29
|
|
|
$this->V3Edmx = new Edmx($namespaceName, $containerName); |
30
|
|
|
if (!$this->V3Edmx->isOK($msg)) { |
31
|
|
|
throw new \Exception($msg); |
32
|
|
|
} |
33
|
|
|
$ymlDir = __DIR__ . DIRECTORY_SEPARATOR . "MetadataV3" . DIRECTORY_SEPARATOR . "JMSmetadata"; |
34
|
|
|
$this->serializer = |
35
|
|
|
SerializerBuilder::create() |
36
|
|
|
->addMetadataDir($ymlDir) |
37
|
|
|
->build(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getEdmx() |
41
|
|
|
{ |
42
|
|
|
$msg = null; |
43
|
|
|
assert($this->V3Edmx->isOk($msg), $msg); |
44
|
|
|
return $this->V3Edmx; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getEdmxXML() |
48
|
|
|
{ |
49
|
|
|
return $this->serializer->serialize($this->getEdmx(), "xml"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function addEntityType($name, $accessType = "Public", $summary = null, $longDescription = null) |
53
|
|
|
{ |
54
|
|
|
$this->startEdmxTransaction(); |
|
|
|
|
55
|
|
|
$NewEntity = new TEntityTypeType(); |
56
|
|
|
$NewEntity->setName($name); |
57
|
|
View Code Duplication |
if (null != $summary || null != $longDescription) { |
|
|
|
|
58
|
|
|
$documentation = new TDocumentationType(); |
59
|
|
|
$documentation->setSummary($summary); |
60
|
|
|
$documentation->setLongDescription($longDescription); |
61
|
|
|
$NewEntity->setDocumentation($documentation); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$entitySet = new EntitySetAnonymousType(); |
65
|
|
|
$entitySet->setName($this->pluralize(2, $NewEntity->getName())); |
66
|
|
|
$namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
67
|
|
View Code Duplication |
if (0 == strlen(trim($namespace))) { |
|
|
|
|
68
|
|
|
$entityTypeName = $NewEntity->getName(); |
69
|
|
|
} else { |
70
|
|
|
$entityTypeName = $namespace . "." . $NewEntity->getName(); |
71
|
|
|
} |
72
|
|
|
$entitySet->setEntityType($entityTypeName); |
73
|
|
|
$entitySet->setGetterAccess($accessType); |
74
|
|
|
|
75
|
|
|
$this->V3Edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity); |
76
|
|
|
$this->V3Edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet); |
77
|
|
|
if (!$this->V3Edmx->isok($this->lastError)) { |
78
|
|
|
$this->revertEdmxTransaction(); |
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
$this->commitEdmxTransaction(); |
|
|
|
|
82
|
|
|
return $NewEntity; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function startEdmxTransaction() |
86
|
|
|
{ |
87
|
|
|
//$this->oldEdmx = serialize($this->V3Edmx); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Pluralizes a word if quantity is not one. |
92
|
|
|
* |
93
|
|
|
* @param int $quantity Number of items |
94
|
|
|
* @param string $singular Singular form of word |
95
|
|
|
* @param string $plural Plural form of word; function will attempt to deduce plural |
96
|
|
|
* form from singular if not provided |
97
|
|
|
* @return string Pluralized word if quantity is not one, otherwise singular |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public static function pluralize($quantity, $singular, $plural = null) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if ($quantity == 1 || !strlen($singular)) { |
102
|
|
|
return $singular; |
103
|
|
|
} |
104
|
|
|
if ($plural !== null) { |
105
|
|
|
return $plural; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$last_letter = strtolower($singular[strlen($singular) - 1]); |
109
|
|
|
switch ($last_letter) { |
110
|
|
|
case 'y': |
111
|
|
|
return substr($singular, 0, -1) . 'ies'; |
112
|
|
|
case 's': |
113
|
|
|
return $singular . 'es'; |
114
|
|
|
default: |
115
|
|
|
return $singular . 's'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function revertEdmxTransaction() |
120
|
|
|
{ |
121
|
|
|
//$this->V3Edmx = unserialize($this->oldEdmx); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function commitEdmxTransaction() |
125
|
|
|
{ |
126
|
|
|
//$this->oldEdmx = null; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function addPropertyToEntityType( |
130
|
|
|
$entityType, |
131
|
|
|
$name, |
132
|
|
|
$type, |
133
|
|
|
$defaultValue = null, |
134
|
|
|
$nullable = false, |
135
|
|
|
$isKey = false, |
136
|
|
|
$storeGeneratedPattern = null, |
137
|
|
|
$summary = null, |
138
|
|
|
$longDescription = null |
139
|
|
|
) { |
140
|
|
|
$this->startEdmxTransaction(); |
|
|
|
|
141
|
|
|
$NewProperty = new TEntityPropertyType(); |
142
|
|
|
$NewProperty->setName($name); |
143
|
|
|
$NewProperty->setType($type); |
144
|
|
|
$NewProperty->setStoreGeneratedPattern($storeGeneratedPattern); |
145
|
|
|
$NewProperty->setNullable($nullable); |
146
|
|
View Code Duplication |
if (null != $summary || null != $longDescription) { |
|
|
|
|
147
|
|
|
$documentation = new TDocumentationType(); |
148
|
|
|
$documentation->setSummary($summary); |
149
|
|
|
$documentation->setLongDescription($longDescription); |
150
|
|
|
$NewProperty->addToDocumentation($documentation); |
151
|
|
|
} |
152
|
|
|
if (null != $defaultValue) { |
153
|
|
|
$NewProperty->setDefaultValue($defaultValue); |
154
|
|
|
} |
155
|
|
|
$entityType->addToProperty($NewProperty); |
156
|
|
|
if ($isKey) { |
157
|
|
|
$Key = new TPropertyRefType(); |
158
|
|
|
$Key->setName($name); |
159
|
|
|
$entityType->addToKey($Key); |
160
|
|
|
} |
161
|
|
|
if (!$this->V3Edmx->isok($this->lastError)) { |
162
|
|
|
$this->revertEdmxTransaction(); |
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
$this->commitEdmxTransaction(); |
|
|
|
|
166
|
|
|
return $NewProperty; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function addNavigationPropertyToEntityType( |
170
|
|
|
TEntityTypeType $principalType, |
171
|
|
|
$principalMultiplicity, |
172
|
|
|
$principalProperty, |
173
|
|
|
TEntityTypeType $dependentType, |
174
|
|
|
$dependentMultiplicity, |
175
|
|
|
$dependentProperty = "", |
176
|
|
|
array $principalConstraintProperty = null, |
177
|
|
|
array $dependentConstraintProperty = null, |
178
|
|
|
$principalGetterAccess = "Public", |
179
|
|
|
$principalSetterAccess = "Public", |
180
|
|
|
$dependentGetterAccess = "Public", |
181
|
|
|
$dependentSetterAccess = "Public", |
182
|
|
|
$principalSummery = null, |
183
|
|
|
$principalLongDescription = null, |
184
|
|
|
$dependentSummery = null, |
185
|
|
|
$dependentLongDescription = null |
186
|
|
|
) { |
187
|
|
|
$this->startEdmxTransaction(); |
|
|
|
|
188
|
|
|
$principalEntitySetName = $this->pluralize(2, $principalType->getName()); |
189
|
|
|
$dependentEntitySetName = $this->pluralize(2, $dependentType->getName()); |
190
|
|
|
$relationName = $principalType->getName() . "_" . $principalProperty . "_" |
191
|
|
|
. $dependentType->getName() . "_" . $dependentProperty; |
192
|
|
|
$relationName = trim($relationName, "_"); |
193
|
|
|
|
194
|
|
|
$namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
195
|
|
|
if (0 == strlen(trim($namespace))) { |
196
|
|
|
$relationFQName = $relationName; |
197
|
|
|
} else { |
198
|
|
|
$relationFQName = $namespace . "." . $relationName; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$principalNavigationProperty = new TNavigationPropertyType(); |
202
|
|
|
$principalNavigationProperty->setName($principalProperty); |
203
|
|
|
$principalNavigationProperty->setToRole(trim($dependentEntitySetName . "_" . $dependentProperty, "_")); |
204
|
|
|
$principalNavigationProperty->setFromRole($principalEntitySetName . "_" . $principalProperty); |
205
|
|
|
$principalNavigationProperty->setRelationship($relationFQName); |
206
|
|
|
$principalNavigationProperty->setGetterAccess($principalGetterAccess); |
207
|
|
|
$principalNavigationProperty->setSetterAccess($principalSetterAccess); |
208
|
|
|
if (null != $principalSummery || null != $principalLongDescription) { |
209
|
|
|
$principalDocumentation = new TDocumentationType(); |
210
|
|
|
$principalDocumentation->setSummary($principalSummery); |
211
|
|
|
$principalDocumentation->setLongDescription($principalLongDescription); |
212
|
|
|
$principalNavigationProperty->setDocumentation($principalDocumentation); |
213
|
|
|
} |
214
|
|
|
$principalType->addToNavigationProperty($principalNavigationProperty); |
215
|
|
|
$dependentNavigationProperty = null; |
216
|
|
|
if (!empty($dependentProperty)) { |
217
|
|
|
$dependentNavigationProperty = new TNavigationPropertyType(); |
218
|
|
|
$dependentNavigationProperty->setName($dependentProperty); |
219
|
|
|
$dependentNavigationProperty->setToRole($principalEntitySetName . "_" . $principalProperty); |
220
|
|
|
$dependentNavigationProperty->setFromRole($dependentEntitySetName . "_" . $dependentProperty); |
221
|
|
|
$dependentNavigationProperty->setRelationship($relationFQName); |
222
|
|
|
$dependentNavigationProperty->setGetterAccess($dependentGetterAccess); |
223
|
|
|
$dependentNavigationProperty->setSetterAccess($dependentSetterAccess); |
224
|
|
|
if (null != $dependentSummery || null != $dependentLongDescription) { |
225
|
|
|
$dependentDocumentation = new TDocumentationType(); |
226
|
|
|
$dependentDocumentation->setSummary($dependentSummery); |
227
|
|
|
$dependentDocumentation->setLongDescription($dependentLongDescription); |
228
|
|
|
$dependentNavigationProperty->setDocumentation($dependentDocumentation); |
229
|
|
|
} |
230
|
|
|
$dependentType->addToNavigationProperty($dependentNavigationProperty); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$assocation = $this->createAssocationFromNavigationProperty( |
234
|
|
|
$principalType, |
235
|
|
|
$dependentType, |
236
|
|
|
$principalNavigationProperty, |
237
|
|
|
$dependentNavigationProperty, |
238
|
|
|
$principalMultiplicity, |
239
|
|
|
$dependentMultiplicity, |
240
|
|
|
$principalConstraintProperty, |
241
|
|
|
$dependentConstraintProperty |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
$this->V3Edmx->getDataServiceType()->getSchema()[0]->addToAssociation($assocation); |
245
|
|
|
|
246
|
|
|
$associationSet = $this->createAssocationSetForAssocation( |
247
|
|
|
$assocation, |
248
|
|
|
$principalEntitySetName, |
249
|
|
|
$dependentEntitySetName |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
$this->V3Edmx->getDataServiceType()->getSchema()[0] |
253
|
|
|
->getEntityContainer()[0]->addToAssociationSet($associationSet); |
254
|
|
|
|
255
|
|
|
if (!$this->V3Edmx->isok($this->lastError)) { |
256
|
|
|
$this->revertEdmxTransaction(); |
|
|
|
|
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
$this->commitEdmxTransaction(); |
|
|
|
|
260
|
|
|
return [$principalNavigationProperty, $dependentNavigationProperty]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
protected function createAssocationFromNavigationProperty( |
264
|
|
|
TEntityTypeType $principalType, |
265
|
|
|
TEntityTypeType $dependentType, |
266
|
|
|
TNavigationPropertyType $principalNavigationProperty, |
267
|
|
|
TNavigationPropertyType $dependentNavigationProperty = null, |
268
|
|
|
$principalMultiplicity, |
269
|
|
|
$dependentMultiplicity, |
270
|
|
|
array $principalConstraintProperty = null, |
271
|
|
|
array $dependentConstraintProperty = null |
272
|
|
|
) { |
273
|
|
|
if (null != $dependentNavigationProperty) { |
274
|
|
|
if ($dependentNavigationProperty->getRelationship() != $principalNavigationProperty->getRelationship()) { |
275
|
|
|
$msg = "if you have both a dependent property and a principal property," |
276
|
|
|
." they should both have the same relationship"; |
277
|
|
|
throw new \Exception($msg); |
278
|
|
|
} |
279
|
|
|
if ($dependentNavigationProperty->getFromRole() != $principalNavigationProperty->getToRole() || |
280
|
|
|
$dependentNavigationProperty->getToRole() != $principalNavigationProperty->getFromRole() |
281
|
|
|
) { |
282
|
|
|
throw new \Exception("The from roles and two roles from matching properties should match"); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
$namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
286
|
|
|
|
287
|
|
|
if (0 == strlen(trim($namespace))) { |
288
|
|
|
$principalTypeFQName = $principalType->getName(); |
289
|
|
|
$dependentTypeFQName = $dependentType->getName(); |
290
|
|
|
} else { |
291
|
|
|
$principalTypeFQName = $namespace . "." . $principalType->getName(); |
292
|
|
|
$dependentTypeFQName = $namespace . "." . $dependentType->getName(); |
293
|
|
|
} |
294
|
|
|
$association = new TAssociationType(); |
295
|
|
|
$relationship = $principalNavigationProperty->getRelationship(); |
296
|
|
|
if (strpos($relationship, '.') !== false) { |
297
|
|
|
$relationship = substr($relationship, strpos($relationship, '.') + 1); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$association->setName($relationship); |
301
|
|
|
$principalEnd = new TAssociationEndType(); |
302
|
|
|
$principalEnd->setType($principalTypeFQName); |
303
|
|
|
$principalEnd->setRole($principalNavigationProperty->getFromRole()); |
304
|
|
|
$principalEnd->setMultiplicity($principalMultiplicity); |
305
|
|
|
$association->addToEnd($principalEnd); |
306
|
|
|
$dependentEnd = new TAssociationEndType(); |
307
|
|
|
$dependentEnd->setType($dependentTypeFQName); |
308
|
|
|
$dependentEnd->setMultiplicity($dependentMultiplicity); |
309
|
|
|
$association->addToEnd($dependentEnd); |
310
|
|
|
|
311
|
|
|
if (null != $dependentNavigationProperty) { |
312
|
|
|
$dependentEnd->setRole($dependentNavigationProperty->getFromRole()); |
313
|
|
|
} else { |
314
|
|
|
$dependentEnd->setRole($principalNavigationProperty->getToRole()); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$principalReferralConstraint = null; |
318
|
|
|
$dependentReferralConstraint = null; |
319
|
|
|
|
320
|
|
View Code Duplication |
if (null != $principalConstraintProperty && 0 < count($principalConstraintProperty)) { |
|
|
|
|
321
|
|
|
$principalReferralConstraint = new TReferentialConstraintRoleElementType(); |
322
|
|
|
$principalReferralConstraint->setRole($principalNavigationProperty->getFromRole()); |
323
|
|
|
foreach ($principalConstraintProperty as $propertyRef) { |
324
|
|
|
$TpropertyRef = new TPropertyRefType(); |
325
|
|
|
$TpropertyRef->setName($propertyRef); |
326
|
|
|
$principalReferralConstraint->addToPropertyRef($TpropertyRef); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
View Code Duplication |
if (null != $dependentConstraintProperty && 0 < count($dependentConstraintProperty)) { |
|
|
|
|
330
|
|
|
$dependentReferralConstraint = new TReferentialConstraintRoleElementType(); |
331
|
|
|
$dependentReferralConstraint->setRole($dependentNavigationProperty->getFromRole()); |
|
|
|
|
332
|
|
|
foreach ($dependentConstraintProperty as $propertyRef) { |
333
|
|
|
$TpropertyRef = new TPropertyRefType(); |
334
|
|
|
$TpropertyRef->setName($propertyRef); |
335
|
|
|
$dependentReferralConstraint->addToPropertyRef($TpropertyRef); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
if (null != $dependentReferralConstraint || null != $principalReferralConstraint) { |
340
|
|
|
$constraint = new TConstraintType(); |
341
|
|
|
$constraint->setPrincipal($principalReferralConstraint); |
|
|
|
|
342
|
|
|
$constraint->setDependent($dependentReferralConstraint); |
|
|
|
|
343
|
|
|
$association->setReferentialConstraint($constraint); |
344
|
|
|
} |
345
|
|
|
return $association; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
protected function createAssocationSetForAssocation( |
349
|
|
|
TAssociationType $association, |
350
|
|
|
$principalEntitySetName, |
351
|
|
|
$dependentEntitySetName |
352
|
|
|
) { |
353
|
|
|
$as = new AssociationSetAnonymousType(); |
354
|
|
|
$name = $association->getName(); |
355
|
|
|
$as->setName($name); |
356
|
|
|
$namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
357
|
|
View Code Duplication |
if (0 == strlen(trim($namespace))) { |
|
|
|
|
358
|
|
|
$associationSetName = $association->getName(); |
359
|
|
|
} else { |
360
|
|
|
$associationSetName = $namespace . "." . $association->getName(); |
361
|
|
|
} |
362
|
|
|
$as->setAssociation($associationSetName); |
363
|
|
|
$end1 = new EndAnonymousType(); |
364
|
|
|
$end1->setRole($association->getEnd()[0]->getRole()); |
365
|
|
|
$end1->setEntitySet($principalEntitySetName); |
366
|
|
|
$end2 = new EndAnonymousType(); |
367
|
|
|
$end2->setRole($association->getEnd()[1]->getRole()); |
368
|
|
|
$end2->setEntitySet($dependentEntitySetName); |
369
|
|
|
$as->addToEnd($end1); |
370
|
|
|
$as->addToEnd($end2); |
371
|
|
|
return $as; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function getLastError() |
375
|
|
|
{ |
376
|
|
|
return $this->lastError; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.