1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata; |
4
|
|
|
|
5
|
|
|
use POData\Common\Messages; |
6
|
|
|
use POData\Common\ODataConstants; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ResourceAssociationTypeEnd represents association (relationship) end. |
10
|
|
|
* |
11
|
|
|
* Entities (described using ResourceType) can have relationship between them. |
12
|
|
|
* A relationship (described using ResourceAssociationType) composed of two ends |
13
|
|
|
* (described using ResourceAssociationTypeEnd). |
14
|
|
|
* s |
15
|
|
|
*/ |
16
|
|
|
class ResourceAssociationTypeEnd |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the association end. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Type of the entity in the relationship end. |
27
|
|
|
* |
28
|
|
|
* @var ResourceEntityType |
29
|
|
|
*/ |
30
|
|
|
private $resourceType; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Entity property involved in the relationship end. |
34
|
|
|
* |
35
|
|
|
* @var ResourceProperty |
36
|
|
|
*/ |
37
|
|
|
private $resourceProperty; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Property of the entity involved in the relationship end points to this end. |
41
|
|
|
* The multiplicity of this end is determined from the fromProperty. |
42
|
|
|
* |
43
|
|
|
* @var ResourceProperty |
44
|
|
|
*/ |
45
|
|
|
private $fromProperty; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Construct new instance of ResourceAssociationTypeEnd. |
49
|
|
|
* |
50
|
|
|
* @param string $name name of the end |
51
|
|
|
* @param ResourceEntityType $resourceType resource type that the end |
52
|
|
|
* refers to |
53
|
|
|
* @param ResourceProperty|null $resourceProperty property of the end, can be |
54
|
|
|
* NULL if relationship is |
55
|
|
|
* uni-directional |
56
|
|
|
* @param ResourceProperty|null $fromProperty Property on the related end |
57
|
|
|
* that points to this end, can |
58
|
|
|
* be NULL if relationship is |
59
|
|
|
* uni-directional |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
$name, |
63
|
|
|
ResourceEntityType $resourceType, |
64
|
|
|
$resourceProperty, |
65
|
|
|
$fromProperty |
66
|
|
|
) { |
67
|
|
|
if (is_null($resourceProperty) && is_null($fromProperty)) { |
68
|
|
|
throw new \InvalidArgumentException( |
69
|
|
|
Messages::resourceAssociationTypeEndBothPropertyCannotBeNull() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
if (!is_null($fromProperty) |
|
|
|
|
74
|
|
|
&& !($fromProperty instanceof ResourceProperty) |
75
|
|
|
) { |
76
|
|
|
throw new \InvalidArgumentException( |
77
|
|
|
Messages::resourceAssociationTypeEndPropertyMustBeNullOrInstanceofResourceProperty( |
78
|
|
|
'$fromProperty' |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if (!is_null($resourceProperty) |
|
|
|
|
84
|
|
|
&& !($resourceProperty instanceof ResourceProperty) |
85
|
|
|
) { |
86
|
|
|
throw new \InvalidArgumentException( |
87
|
|
|
Messages::resourceAssociationTypeEndPropertyMustBeNullOrInstanceofResourceProperty( |
88
|
|
|
'$resourceProperty' |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->name = $name; |
94
|
|
|
$this->resourceType = $resourceType; |
95
|
|
|
$this->resourceProperty = $resourceProperty; |
96
|
|
|
$this->fromProperty = $fromProperty; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* To check this relationship belongs to a specfic entity property. |
101
|
|
|
* |
102
|
|
|
* @param ResourceEntityType $resourceType The type of the entity |
103
|
|
|
* @param ResourceProperty|null $resourceProperty The property in the entity |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isBelongsTo(ResourceEntityType $resourceType, $resourceProperty) |
108
|
|
|
{ |
109
|
|
|
$flag1 = is_null($resourceProperty); |
110
|
|
|
$flag2 = is_null($this->resourceProperty); |
111
|
|
|
if ($flag1 != $flag2) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$typeNameMatch = 0 == strcmp($resourceType->getFullName(), $this->resourceType->getFullName()); |
116
|
|
|
|
117
|
|
|
if (true === $flag1) { |
118
|
|
|
return $typeNameMatch; |
119
|
|
|
} |
120
|
|
|
assert(isset($resourceProperty)); |
121
|
|
|
$propertyNameMatch = 0 == strcmp($resourceProperty->getName(), $this->resourceProperty->getName()); |
122
|
|
|
|
123
|
|
|
return $typeNameMatch && $propertyNameMatch; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the name of the end. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getName() |
132
|
|
|
{ |
133
|
|
|
return $this->name; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the resource type that the end refers to. |
138
|
|
|
* |
139
|
|
|
* @return ResourceEntityType |
140
|
|
|
*/ |
141
|
|
|
public function getResourceType() |
142
|
|
|
{ |
143
|
|
|
return $this->resourceType; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the property of the end. |
148
|
|
|
* |
149
|
|
|
* @return ResourceProperty |
150
|
|
|
*/ |
151
|
|
|
public function getResourceProperty() |
152
|
|
|
{ |
153
|
|
|
return $this->resourceProperty; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the Multiplicity of the relationship end. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getMultiplicity() |
162
|
|
|
{ |
163
|
|
|
if (!is_null($this->fromProperty) |
164
|
|
|
&& $this->fromProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE |
165
|
|
|
) { |
166
|
|
|
return ODataConstants::ZERO_OR_ONE; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return ODataConstants::MANY; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
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.