-
Notifications
You must be signed in to change notification settings - Fork 13
/
SimpleMetadataProvider.php
1089 lines (1012 loc) · 41.7 KB
/
SimpleMetadataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace POData\Providers\Metadata;
use AlgoWeb\ODataMetadata\MetadataManager;
use AlgoWeb\ODataMetadata\MetadataV3\edm\TComplexTypeType;
use AlgoWeb\ODataMetadata\MetadataV3\edm\TEntityTypeType;
use ErrorException;
use Exception;
use InvalidArgumentException;
use POData\Common\InvalidOperationException;
use POData\Pluralizer;
use POData\Providers\Metadata\Type\EdmPrimitiveType;
use POData\Providers\Metadata\Type\IType;
use ReflectionClass;
use ReflectionException;
use stdClass;
/**
* Class SimpleMetadataProvider.
*/
class SimpleMetadataProvider implements IMetadataProvider
{
public $oDataEntityMap = [];
protected $resourceSets = [];
protected $resourceTypes = [];
protected $associationSets = [];
protected $containerName;
protected $namespaceName;
protected $singletons = [];
private $metadataManager;
private $typeSetMapping = [];
private $baseTypes = [];
/**
* @param string $containerName container name for the data source
* @param string $namespaceName namespace for the data source
*/
public function __construct(string $containerName, string $namespaceName)
{
$this->containerName = $containerName;
$this->namespaceName = $namespaceName;
$this->metadataManager = new MetadataManager($namespaceName, $containerName);
}
//Begin Implementation of IMetadataProvider
/**
* @throws Exception
* @return string|null
*/
public function getXML(): ?string
{
return $this->getMetadataManager()->getEdmxXML();
}
/**
* @return MetadataManager
*/
public function getMetadataManager(): MetadataManager
{
return $this->metadataManager;
}
/**
* get the Container name for the data source.
*
* @return string container name
*/
public function getContainerName(): string
{
return $this->containerName;
}
/**
* get Namespace name for the data source.
*
* @return string namespace
*/
public function getContainerNamespace(): string
{
return $this->namespaceName;
}
/**
* get all entity set information.
*
* @param null|mixed $params
*
* @throws ErrorException
* @return ResourceSet[]
*/
public function getResourceSets($params = null): array
{
$parameters = [];
if (is_string($params)) {
$parameters[] = $params;
} elseif (isset($params) && !is_array($params)) {
throw new ErrorException('Input parameter must be absent, null, string or array');
} else {
$parameters = $params;
}
if (!is_array($parameters) || 0 == count($parameters)) {
return array_values($this->resourceSets);
}
assert(is_array($parameters));
$return = [];
$counter = 0;
foreach ($this->resourceSets as $resource) {
$resName = $resource->getName();
if (in_array($resName, $parameters)) {
$return[] = $resource;
$counter++;
}
}
assert($counter == count($return));
return $return;
}
/**
* get all resource types in the data source.
*
* @return ResourceType[]
*/
public function getTypes(): array
{
return array_values($this->resourceTypes);
}
/**
* get a resource set based on the specified resource set name.
*
* @param string $name Name of the resource set
*
* @return ResourceSet|null resource set with the given name if found else NULL
*/
public function resolveResourceSet(string $name): ?ResourceSet
{
if (array_key_exists($name, $this->resourceSets)) {
return $this->resourceSets[$name];
}
return null;
}
/**
* get a resource type based on the resource type name.
*
* @param string $name Name of the resource type
*
* @return ResourceType|null resource type with the given resource type name if found else NULL
*/
public function resolveResourceType(string $name): ?ResourceType
{
if (array_key_exists($name, $this->resourceTypes)) {
return $this->resourceTypes[$name];
}
return null;
}
/**
* get a singleton based on the specified singleton name.
*
* @param string $name Name of the resource set
*
* @return ResourceFunctionType|null singleton with the given name if found else NULL
*/
public function resolveSingleton(string $name): ?ResourceFunctionType
{
if (array_key_exists($name, $this->singletons)) {
return $this->singletons[$name];
}
return null;
}
/**
* get a resource set based on the specified resource association set name.
*
* @param string $name Name of the resource association set
*
* @return ResourceAssociationSet|null resource association set with the given name if found else NULL
*/
public function resolveAssociationSet(string $name): ?ResourceAssociationSet
{
if (array_key_exists($name, $this->associationSets)) {
return $this->associationSets[$name];
}
return null;
}
/**
* Get number of association sets hooked up.
*
* @return int
*/
public function getAssociationCount(): int
{
return count($this->associationSets);
}
/**
* The method must return a collection of all the types derived from
* $resourceType The collection returned should NOT include the type
* passed in as a parameter.
*
* @param ResourceEntityType $resourceType Resource to get derived resource types from
*
* @return ResourceType[]
*/
public function getDerivedTypes(ResourceEntityType $resourceType): array
{
$ret = [];
foreach ($this->resourceTypes as $rType) {
if ($rType->getBaseType() == $resourceType) {
$ret[] = $rType;
}
}
return $ret;
}
/**
* @param ResourceEntityType $resourceType Resource to check for derived resource types
*
* @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false
*/
public function hasDerivedTypes(ResourceEntityType $resourceType): bool
{
if (in_array($resourceType, $this->baseTypes)) {
return true;
}
return false;
}
//End Implementation of IMetadataProvider
/**
* Gets the ResourceAssociationSet instance for the given source
* association end.
*
* @param ResourceSet $sourceResourceSet Resource set of the source association end
* @param ResourceEntityType $sourceResourceType Resource type of the source association end
* @param ResourceProperty $targetResourceProperty Resource property of the target association end
*
* @throws InvalidOperationException
* @return ResourceAssociationSet|null
*/
public function getResourceAssociationSet(
ResourceSet $sourceResourceSet,
ResourceEntityType $sourceResourceType,
ResourceProperty $targetResourceProperty
): ?ResourceAssociationSet {
//e.g.
//ResourceSet => Representing 'Customers' entity set
//ResourceType => Representing'Customer' entity type
//ResourceProperty => Representing 'Orders' property
//We have created ResourceAssociationSet while adding
//ResourceSetReference or ResourceReference
//and kept in $this->associationSets
//$metadata->addResourceSetReferenceProperty(
// $customersEntityType,
// 'Orders',
// $ordersResourceSet
// );
$targetResourceSet = $targetResourceProperty->getResourceType()->getCustomState();
if (null === $targetResourceSet) {
throw new InvalidOperationException(
'Failed to retrieve the custom state from ' . $targetResourceProperty->getResourceType()->getName()
);
}
assert($targetResourceSet instanceof ResourceSet);
//Customer_Orders_Orders, Order_Customer_Customers
$key = ResourceAssociationSet::keyName(
$sourceResourceType,
$targetResourceProperty->getName(),
$targetResourceSet
);
$associationSet = array_key_exists($key, $this->associationSets) ? $this->associationSets[$key] : null;
assert(
null == $associationSet || $associationSet instanceof ResourceAssociationSet,
'Retrieved resource association must be either null or an instance of ResourceAssociationSet'
);
return $associationSet;
}
/**
* Add an entity type.
*
* @param ReflectionClass $refClass reflection class of the entity
* @param string $name name of the entity
* @param null|string $pluralName Optional custom resource set name
* @param mixed $isAbstract
* @param null|mixed $baseType
* @throws InvalidOperationException when the name is already in use
* @return ResourceEntityType when the name is already in use
* @internal param string $namespace namespace of the data source
*/
public function addEntityType(
ReflectionClass $refClass,
string $name,
string $pluralName = null,
bool $isAbstract = false,
$baseType = null
): ResourceEntityType {
$result = $this->createResourceType(
$refClass,
$name,
ResourceTypeKind::ENTITY(),
$isAbstract,
$baseType,
$pluralName
);
assert($result instanceof ResourceEntityType);
return $result;
}
/**
* @param ReflectionClass $refClass
* @param string $name
* @param ResourceTypeKind $typeKind
* @param mixed $isAbstract
* @param null|mixed $baseType
* @param null|mixed $pluralName
* @throws InvalidOperationException
* @return ResourceEntityType|ResourceComplexType
* @internal param null|string $namespace
* @internal param null|ResourceType $baseResourceType
*/
private function createResourceType(
ReflectionClass $refClass,
string $name,
ResourceTypeKind $typeKind,
bool $isAbstract = false,
ResourceType $baseType = null,
string $pluralName = null
): ResourceType {
if (array_key_exists($name, $this->resourceTypes)) {
throw new InvalidOperationException('Type with same name already added');
}
if (null !== $baseType) {
$baseTEntityType = $this->oDataEntityMap[$baseType->getFullName()];
$this->baseTypes[] = $baseType;
} else {
$baseTEntityType = null;
}
$type = null;
if ($typeKind == ResourceTypeKind::ENTITY()) {
list($oet, $entitySet) = $this->getMetadataManager()
->addEntityType($name, $baseTEntityType, $isAbstract, 'Public', null, null, $pluralName);
assert($oet instanceof TEntityTypeType, 'Entity type ' . $name . ' not successfully added');
$type = new ResourceEntityType($refClass, $oet, $this);
$typeName = $type->getFullName();
$returnName = MetadataManager::getResourceSetNameFromResourceType($typeName);
$this->oDataEntityMap[$typeName] = $oet;
$this->typeSetMapping[$name] = $entitySet;
$this->typeSetMapping[$typeName] = $entitySet;
$this->typeSetMapping[$returnName] = $entitySet;
} elseif ($typeKind == ResourceTypeKind::COMPLEX()) {
$complex = new TComplexTypeType();
$complex->setName($name);
$type = new ResourceComplexType($refClass, $complex);
}
assert(null != $type, 'Type variable must not be null');
$this->resourceTypes[$name] = $type;
ksort($this->resourceTypes);
return $type;
}
/**
* Add a complex type.
*
* @param ReflectionClass $refClass reflection class of the complex entity type
* @param string $name name of the entity
* @throws InvalidOperationException when the name is already in use
* @return ResourceComplexType
*
* @internal param string $namespace namespace of the data source
* @internal param ResourceType $baseResourceType base resource type
*/
public function addComplexType(ReflectionClass $refClass, string $name): ResourceComplexType
{
$result = $this->createResourceType($refClass, $name, ResourceTypeKind::COMPLEX());
assert($result instanceof ResourceComplexType);
return $result;
}
/**
* @param string|null $name name of the resource set (now taken from resource type)
* @param ResourceEntityType $resourceType resource type
*
* @throws InvalidOperationException
* @return ResourceSet
*/
public function addResourceSet(?string $name, ResourceEntityType $resourceType): ResourceSet
{
$typeName = $resourceType->getFullName();
$pluralName = Pluralizer::plural($typeName);
if (null === $name) {
$returnName = $pluralName;
} else {
// handle case where $name is a fully-qualified PHP class name
$nameBits = explode('\\', $name);
$numBits = count($nameBits);
if ($typeName == $nameBits[$numBits - 1]) {
$returnName = $pluralName;
} else {
$returnName = $name;
}
}
if (array_key_exists($returnName, $this->resourceSets)) {
throw new InvalidOperationException('Resource Set already added');
}
$resourceType->validateType();
$this->resourceSets[$returnName] = new ResourceSet($returnName, $resourceType);
//No support for multiple ResourceSet with same EntityType
//So keeping reference to the 'ResourceSet' with the entity type
$resourceType->setCustomState($this->resourceSets[$returnName]);
ksort($this->resourceSets);
return $this->resourceSets[$returnName];
}
/**
* To add a Key-primitive property to a resource (Complex/Entity).
*
* @param ResourceType $resourceType resource type to which key property
* is to be added
* @param string $name name of the key property
* @param EdmPrimitiveType $typeCode type of the key property
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addKeyProperty(ResourceType $resourceType, string $name, EdmPrimitiveType $typeCode): void
{
$this->addPrimitivePropertyInternal($resourceType, $name, $typeCode, true);
}
/**
* To add a Key/NonKey-primitive property to a resource (complex/entity).
*
* @param ResourceType $resourceType Resource type
* @param string $name name of the property
* @param EdmPrimitiveType $typeCode type of property
* @param bool $isKey property is key or not
* @param bool $isBag property is bag or not
* @param bool $isETagProperty property is etag or not
* @param null|mixed $defaultValue
* @param bool $nullable
*
* @throws InvalidOperationException
* @throws ReflectionException
*/
private function addPrimitivePropertyInternal(
ResourceType $resourceType,
string $name,
EdmPrimitiveType $typeCode = null,
bool $isKey = false,
bool $isBag = false,
bool $isETagProperty = false,
$defaultValue = null,
bool $nullable = false
): void {
if ($isETagProperty && $isBag) {
throw new InvalidOperationException(
'Only primitive property can be etag property, bag property cannot be etag property.'
);
}
$this->checkInstanceProperty($name, $resourceType);
// check that property and resource name don't up and collide - would violate OData spec
if (strtolower($name) == strtolower($resourceType->getName())) {
throw new InvalidOperationException(
'Property name must be different from resource name.'
);
}
$primitiveResourceType = ResourceType::getPrimitiveResourceType($typeCode);
$kind = $isKey ? ResourcePropertyKind::PRIMITIVE()->setKEY(true) : ResourcePropertyKind::PRIMITIVE();
if ($isBag) {
$kind = $kind->setBAG(true);
}
if ($isETagProperty) {
$kind = $kind->setETAG(true);
}
$kind = new ResourcePropertyKind($kind);
$resourceProperty = new ResourceProperty(
$name,
null,
$kind,
$primitiveResourceType
);
$resourceType->addProperty($resourceProperty);
if (array_key_exists($resourceType->getFullName(), $this->oDataEntityMap)) {
$this->metadataManager->addPropertyToEntityType(
$this->oDataEntityMap[$resourceType->getFullName()],
$name,
$primitiveResourceType->getFullName(),
$defaultValue,
$nullable,
$isKey
);
}
}
/**
* @param string $name
* @param ResourceType $resourceType
*
* @throws InvalidOperationException
* @throws ReflectionException
*/
private function checkInstanceProperty(string $name, ResourceType $resourceType): void
{
$instance = $resourceType->getInstanceType();
$hasMagicGetter = $instance instanceof IType || $instance->hasMethod('__get');
if ($instance instanceof ReflectionClass) {
$hasMagicGetter |= $instance->isInstance(new stdClass());
}
if (!$hasMagicGetter) {
try {
if ($instance instanceof ReflectionClass) {
$instance->getProperty($name);
}
} catch (ReflectionException $exception) {
throw new InvalidOperationException(
'Can\'t add a property which does not exist on the instance type.'
);
}
}
}
/**
* To add a NonKey-primitive property (Complex/Entity).
*
* @param ResourceType $resourceType resource type to which key property
* is to be added
* @param string $name name of the key property
* @param EdmPrimitiveType $typeCode type of the key property
* @param bool $isBag property is bag or not
* @param null|mixed $defaultValue
* @param bool $nullable
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addPrimitiveProperty(
ResourceType $resourceType,
string $name,
EdmPrimitiveType $typeCode,
bool $isBag = false,
$defaultValue = null,
bool $nullable = false
): void {
$this->addPrimitivePropertyInternal(
$resourceType,
$name,
$typeCode,
false,
$isBag,
false,
$defaultValue,
$nullable
);
}
/**
* To add a non-key etag property.
*
* @param ResourceType $resourceType resource type to which key property
* is to be added
* @param string $name name of the property
* @param EdmPrimitiveType $typeCode type of the etag property
* @param null|mixed $defaultValue
* @param mixed $nullable
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addETagProperty(
ResourceType $resourceType,
string $name,
EdmPrimitiveType $typeCode,
$defaultValue = null,
bool $nullable = false
): void {
$this->addPrimitivePropertyInternal(
$resourceType,
$name,
$typeCode,
false,
false,
true,
$defaultValue,
$nullable
);
}
/**
* To add a resource reference property.
*
* @param ResourceEntityType $resourceType The resource type to add the resource
* reference property to
* @param string $name The name of the property to add
* @param ResourceSet $targetResourceSet The resource set the resource reference
* property points to
* @param bool $flip
* @param bool $many
* @param ResourceEntityType|null $concreteType Underlying concrete resource reference type, if set
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addResourceReferenceProperty(
ResourceEntityType $resourceType,
string $name,
ResourceSet $targetResourceSet,
bool $flip = false,
bool $many = false,
ResourceEntityType $concreteType = null
): void {
$this->addReferencePropertyInternal(
$resourceType,
$name,
$targetResourceSet,
$flip ? '0..1' : '1',
$many,
$concreteType
);
}
/**
* To add a navigation property (resource set or resource reference)
* to a resource type.
*
* @param ResourceEntityType $sourceResourceType The resource type to add the resource reference
* or resource reference set property to
* @param string $name The name of the property to add
* @param ResourceSet $targetResourceSet The resource set the
* resource reference or reference
* set property points to
* @param string $resourceMult The multiplicity of relation being added
* @param bool $many
*
* @param ResourceEntityType|null $concreteType
* @throws InvalidOperationException
* @throws ReflectionException
*/
private function addReferencePropertyInternal(
ResourceEntityType $sourceResourceType,
string $name,
ResourceSet $targetResourceSet,
string $resourceMult,
bool $many = false,
ResourceEntityType $concreteType = null
): void {
$allowedMult = ['*', '1', '0..1'];
$backMultArray = ['*' => '*', '1' => '0..1', '0..1' => '1'];
$this->checkInstanceProperty($name, $sourceResourceType);
// check that property and resource name don't up and collide - would violate OData spec
if (strtolower($name) == strtolower($sourceResourceType->getName())) {
throw new InvalidOperationException(
'Property name must be different from resource name.'
);
}
assert(in_array($resourceMult, $allowedMult), 'Supplied multiplicity ' . $resourceMult . ' not valid');
$resourcePropertyKind = ('*' == $resourceMult)
? ResourcePropertyKind::RESOURCESET_REFERENCE()
: ResourcePropertyKind::RESOURCE_REFERENCE();
$targetResourceType = $targetResourceSet->getResourceType();
$sourceResourceProperty = new ResourceProperty($name, null, $resourcePropertyKind, $targetResourceType);
$sourceResourceType->addProperty($sourceResourceProperty);
//Create instance of AssociationSet for this relationship
$sourceResourceSet = $sourceResourceType->getCustomState();
if (!$sourceResourceSet instanceof ResourceSet) {
throw new InvalidOperationException(
'Failed to retrieve the custom state from '
. $sourceResourceType->getName()
);
}
//Customer_Orders_Orders, Order_Customer_Customers
//(source type::name _ source property::name _ target set::name)
$setKey = ResourceAssociationSet::keyName($sourceResourceType, $name, $targetResourceSet);
//$setKey = $sourceResourceType->getName() . '_' . $name . '_' . $targetResourceType->getName();
$set = new ResourceAssociationSet(
$setKey,
new ResourceAssociationSetEnd(
$sourceResourceSet,
$sourceResourceType,
$sourceResourceProperty
),
new ResourceAssociationSetEnd($targetResourceSet, $targetResourceType, null, $concreteType)
);
$mult = $resourceMult;
$backMult = $many ? '*' : $backMultArray[$resourceMult];
if (false === $many && '*' == $backMult && '*' == $resourceMult) {
$backMult = '1';
}
$this->getMetadataManager()->addNavigationPropertyToEntityType(
$this->oDataEntityMap[$sourceResourceType->getFullName()],
$mult,
$name,
$this->oDataEntityMap[$targetResourceType->getFullName()],
$backMult
);
$this->associationSets[$setKey] = $set;
}
/**
* To add a 1:N resource reference property.
*
* @param ResourceEntityType $sourceResourceType The resource type to add the resource
* reference property from
* @param ResourceEntityType $targetResourceType The resource type to add the resource
* reference property to
* @param string $sourceProperty The name of the property to add, on source type
* @param string $targetProperty The name of the property to add, on target type
* @param bool $nullable Is singleton side of relation nullable?
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addResourceReferencePropertyBidirectional(
ResourceEntityType $sourceResourceType,
ResourceEntityType $targetResourceType,
string $sourceProperty,
string $targetProperty,
bool $nullable = false
): void {
$this->addReferencePropertyInternalBidirectional(
$sourceResourceType,
$targetResourceType,
$sourceProperty,
$targetProperty,
'*',
true === $nullable ? '0..1' : '1'
);
// verify resource property types are what we expect them to be
$sourceResourceKind = $sourceResourceType->resolveProperty($sourceProperty)->getKind();
assert(
ResourcePropertyKind::RESOURCE_REFERENCE() == $sourceResourceKind,
'1 side of 1:N relationship not pointing to resource reference'
);
$targetResourceKind = $targetResourceType->resolveProperty($targetProperty)->getKind();
assert(
ResourcePropertyKind::RESOURCESET_REFERENCE() == $targetResourceKind,
'N side of 1:N relationship not pointing to resource set reference'
);
}
/**
* To add a navigation property (resource set or resource reference)
* to a resource type.
*
* @param ResourceEntityType $sourceResourceType The source resource type to add
* the resource reference
* or resource reference set property to
* @param ResourceEntityType $targetResourceType The target resource type to add
* the resource reference
* or resource reference set property to
* @param string $sourceProperty The name of the
* property to add to source type
* @param string $targetProperty The name of the
* property to add to target type
* @param string $sourceMultiplicity The multiplicity at the source end of relation
* @param string $targetMultiplicity The multiplicity at the target end of relation
*
* @throws InvalidOperationException
* @throws ReflectionException
*/
private function addReferencePropertyInternalBidirectional(
ResourceEntityType $sourceResourceType,
ResourceEntityType $targetResourceType,
string $sourceProperty,
string $targetProperty,
string $sourceMultiplicity,
string $targetMultiplicity
): void {
$this->checkInstanceProperty($sourceProperty, $sourceResourceType);
$this->checkInstanceProperty($targetProperty, $targetResourceType);
// check that property and resource name don't up and collide - would violate OData spec
if (strtolower($sourceProperty) == strtolower($sourceResourceType->getName())) {
throw new InvalidOperationException(
'Source property name must be different from source resource name.'
);
}
if (strtolower($targetProperty) == strtolower($targetResourceType->getName())) {
throw new InvalidOperationException(
'Target property name must be different from target resource name.'
);
}
//Create instance of AssociationSet for this relationship
$sourceResourceSet = $sourceResourceType->getCustomState();
if (!$sourceResourceSet instanceof ResourceSet) {
throw new InvalidOperationException(
'Failed to retrieve the custom state from '
. $sourceResourceType->getName()
);
}
$targetResourceSet = $targetResourceType->getCustomState();
if (!$targetResourceSet instanceof ResourceSet) {
throw new InvalidOperationException(
'Failed to retrieve the custom state from '
. $targetResourceType->getName()
);
}
//Customer_Orders_Orders, Order_Customer_Customers
$fwdSetKey = ResourceAssociationSet::keyName($sourceResourceType, $sourceProperty, $targetResourceSet);
$revSetKey = ResourceAssociationSet::keyName($targetResourceType, $targetProperty, $sourceResourceSet);
if (isset($this->associationSets[$fwdSetKey]) && isset($this->associationSets[$revSetKey])) {
return;
}
$sourceKind = ('*' == $sourceMultiplicity)
? ResourcePropertyKind::RESOURCESET_REFERENCE()
: ResourcePropertyKind::RESOURCE_REFERENCE();
$targetKind = ('*' == $targetMultiplicity)
? ResourcePropertyKind::RESOURCESET_REFERENCE()
: ResourcePropertyKind::RESOURCE_REFERENCE();
$sourceResourceProperty = new ResourceProperty($sourceProperty, null, $targetKind, $targetResourceType);
assert(
$targetKind == $sourceResourceProperty->getKind(),
'Resource property kind mismatch between $targetKind and $sourceResourceProperty'
);
$sourceResourceType->addProperty($sourceResourceProperty, false);
$targetResourceProperty = new ResourceProperty($targetProperty, null, $sourceKind, $sourceResourceType);
assert(
$sourceKind == $targetResourceProperty->getKind(),
'Resource property kind mismatch between $sourceKind and $targetResourceProperty'
);
$targetResourceType->addProperty($targetResourceProperty, false);
//TODO: Audit this, figure out how it makes metadata go sproing
$fwdSet = new ResourceAssociationSet(
$fwdSetKey,
new ResourceAssociationSetEnd($sourceResourceSet, $sourceResourceType, $sourceResourceProperty),
new ResourceAssociationSetEnd($targetResourceSet, $targetResourceType, $targetResourceProperty)
);
$revSet = new ResourceAssociationSet(
$revSetKey,
new ResourceAssociationSetEnd($targetResourceSet, $targetResourceType, $targetResourceProperty),
new ResourceAssociationSetEnd($sourceResourceSet, $sourceResourceType, $sourceResourceProperty)
);
$sourceName = $sourceResourceType->getFullName();
$targetName = $targetResourceType->getFullName();
$this->getMetadataManager()->addNavigationPropertyToEntityType(
$this->oDataEntityMap[$sourceName],
$sourceMultiplicity,
$sourceProperty,
$this->oDataEntityMap[$targetName],
$targetMultiplicity,
$targetProperty
);
$this->associationSets[$fwdSetKey] = $fwdSet;
$this->associationSets[$revSetKey] = $revSet;
}
/**
* To add a resource set reference property.
*
* @param ResourceEntityType $resourceType The resource type to add the
* resource reference set property to
* @param string $name The name of the property to add
* @param ResourceSet $targetResourceSet The resource set the resource
* reference set property points to
* @param ResourceEntityType|null $concreteType Underlying concrete resource type, if set
* @param mixed $single
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addResourceSetReferenceProperty(
ResourceEntityType $resourceType,
string $name,
ResourceSet $targetResourceSet,
ResourceEntityType $concreteType = null,
bool $single = false
): void {
$this->addReferencePropertyInternal(
$resourceType,
$name,
$targetResourceSet,
'*',
(true === $single) ? false : true,
$concreteType
);
}
/**
* To add a M:N resource reference property.
*
* @param ResourceEntityType $sourceResourceType The resource type to add the resource
* reference property from
* @param ResourceEntityType $targetResourceType The resource type to add the resource
* reference property to
* @param string $sourceProperty The name of the property to add, on source type
* @param string $targetProperty The name of the property to add, on target type
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addResourceSetReferencePropertyBidirectional(
ResourceEntityType $sourceResourceType,
ResourceEntityType $targetResourceType,
string $sourceProperty,
string $targetProperty
): void {
$this->addReferencePropertyInternalBidirectional(
$sourceResourceType,
$targetResourceType,
$sourceProperty,
$targetProperty,
'*',
'*'
);
// verify resource property types are what we expect them to be
$sourceResourceKind = $sourceResourceType->resolveProperty($sourceProperty)->getKind();
assert(
ResourcePropertyKind::RESOURCESET_REFERENCE() == $sourceResourceKind,
'M side of M:N relationship not pointing to resource set reference'
);
$targetResourceKind = $targetResourceType->resolveProperty($targetProperty)->getKind();
assert(
ResourcePropertyKind::RESOURCESET_REFERENCE() == $targetResourceKind,
'N side of M:N relationship not pointing to resource set reference'
);
}
/**
* To add a 1-1 resource reference.
*
* @param ResourceEntityType $sourceResourceType The resource type to add the resource
* reference property from
* @param ResourceEntityType $targetResourceType The resource type to add the resource
* reference property to
* @param string $sourceProperty The name of the property to add, on source type
* @param string $targetProperty The name of the property to add, on target type
* @throws InvalidOperationException
* @throws ReflectionException
*/
public function addResourceReferenceSinglePropertyBidirectional(
ResourceEntityType $sourceResourceType,
ResourceEntityType $targetResourceType,
string $sourceProperty,
string $targetProperty
): void {
$this->addReferencePropertyInternalBidirectional(
$sourceResourceType,
$targetResourceType,
$sourceProperty,
$targetProperty,
'1',
'0..1'
);
// verify resource property types are what we expect them to be
$sourceResourceKind = $sourceResourceType->resolveProperty($sourceProperty)->getKind();
assert(
ResourcePropertyKind::RESOURCE_REFERENCE() == $sourceResourceKind,
'1 side of 1:1 relationship not pointing to resource reference'
);
$targetResourceKind = $targetResourceType->resolveProperty($targetProperty)->getKind();
assert(
ResourcePropertyKind::RESOURCE_REFERENCE() == $targetResourceKind,
'0..1 side of 1:1 relationship not pointing to resource reference'
);
}
/**
* To add a complex property to entity or complex type.
*
* @param ResourceType $targetResourceType The resource type to which the complex property needs to add
* @param string $name name of the complex property
* @param ResourceComplexType $complexResourceType complex resource type
* @param bool $isBag complex type is bag or not
*
* @throws ReflectionException
* @throws InvalidOperationException
* @return ResourceProperty
*/
public function addComplexProperty(
ResourceType $targetResourceType,
string $name,
ResourceComplexType $complexResourceType,