|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use POData\Common\Messages; |
|
7
|
|
|
use POData\Providers\Metadata\Type\IType; |
|
8
|
|
|
|
|
9
|
|
|
class ResourceProperty |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Property name. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $name; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Property MIME type. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $mimeType; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Property Kind, the possible values are: |
|
27
|
|
|
* ResourceReference |
|
28
|
|
|
* ResourceSetReference |
|
29
|
|
|
* ComplexType |
|
30
|
|
|
* ComplexType + Bag |
|
31
|
|
|
* PrimitiveType |
|
32
|
|
|
* PrimitiveType + Bag |
|
33
|
|
|
* PrimitiveType + Key |
|
34
|
|
|
* PrimitiveType + ETag. |
|
35
|
|
|
* |
|
36
|
|
|
* @var ResourcePropertyKind |
|
37
|
|
|
*/ |
|
38
|
|
|
private $kind; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ResourceType describes this property. |
|
42
|
|
|
* |
|
43
|
|
|
* @var ResourceType |
|
44
|
|
|
*/ |
|
45
|
|
|
private $propertyResourceType; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $name Name of the property |
|
49
|
|
|
* @param string $mimeType Mime type of the property |
|
50
|
|
|
* @param ResourcePropertyKind $kind The kind of property |
|
51
|
|
|
* @param ResourceType $propertyResourceType ResourceType of the property |
|
52
|
|
|
* |
|
53
|
|
|
* @throws InvalidArgumentException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($name, $mimeType, $kind, ResourceType $propertyResourceType) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->isValidPropertyName($name)) { |
|
58
|
|
|
throw new InvalidArgumentException( |
|
59
|
|
|
'Property name violates OData specification.' |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!$this->isValidResourcePropertyKind($kind)) { |
|
64
|
|
|
throw new InvalidArgumentException( |
|
65
|
|
|
Messages::resourcePropertyInvalidKindParameter('$kind') |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!$this->isResourceKindValidForPropertyKind($kind, $propertyResourceType->getResourceTypeKind())) { |
|
70
|
|
|
throw new InvalidArgumentException( |
|
71
|
|
|
Messages::resourcePropertyPropertyKindAndResourceTypeKindMismatch( |
|
72
|
|
|
'$kind', |
|
73
|
|
|
'$propertyResourceType' |
|
74
|
|
|
) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->name = $name; |
|
79
|
|
|
$this->mimeType = $mimeType; |
|
80
|
|
|
$this->kind = $kind; |
|
81
|
|
|
$this->propertyResourceType = $propertyResourceType; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Check whether current property is of kind specified by the parameter. |
|
86
|
|
|
* |
|
87
|
|
|
* @param ResourcePropertyKind $kind kind to check |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function isKindOf($kind) |
|
92
|
|
|
{ |
|
93
|
|
|
return ($this->kind & $kind) == $kind; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the property name. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getName() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->name; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get property MIME type. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getMIMEType() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->mimeType; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get property kind. |
|
118
|
|
|
* |
|
119
|
|
|
* @return ResourcePropertyKind |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getKind() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->kind; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get the resource type for this property. |
|
128
|
|
|
* |
|
129
|
|
|
* @return ResourceType |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getResourceType() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->propertyResourceType; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the kind of resource type. |
|
138
|
|
|
* |
|
139
|
|
|
* @return ResourceTypeKind |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getTypeKind() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->propertyResourceType->getResourceTypeKind(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the instance type. If the property is of kind 'Complex', |
|
148
|
|
|
* 'ResourceReference' or 'ResourceSetReference' then this function returns |
|
149
|
|
|
* refernece to ReflectionClass instance for the type. If the property of |
|
150
|
|
|
* kind 'Primitive' then this function returns ITYpe instance for the type. |
|
151
|
|
|
* |
|
152
|
|
|
* @return \ReflectionClass|IType |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getInstanceType() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->propertyResourceType->getInstanceType(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Check one kind is of another kind. |
|
161
|
|
|
* |
|
162
|
|
|
* @param ResourcePropertyKind $kind1 First kind |
|
163
|
|
|
* @param ResourcePropertyKind $kind2 second kind |
|
164
|
|
|
* |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public static function sIsKindOf($kind1, $kind2) |
|
168
|
|
|
{ |
|
169
|
|
|
return ($kind1 & $kind2) == $kind2; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Checks whether supplied name meets OData specification. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $name Field name to be validated |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
|
|
private function isValidPropertyName($name) |
|
180
|
|
|
{ |
|
181
|
|
|
if (!isset($name) || !is_string($name) || empty($name)) { |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
if ('_' == substr($name, 0, 1)) { |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Checks whether resource property kind is valid or not. |
|
193
|
|
|
* |
|
194
|
|
|
* @param ResourcePropertyKind $kind The kind to validate |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
|
|
private function isValidResourcePropertyKind($kind) |
|
199
|
|
|
{ |
|
200
|
|
|
return |
|
201
|
|
|
!($kind != ResourcePropertyKind::RESOURCE_REFERENCE && |
|
202
|
|
|
$kind != ResourcePropertyKind::RESOURCESET_REFERENCE && |
|
203
|
|
|
$kind != ResourcePropertyKind::COMPLEX_TYPE && |
|
204
|
|
|
($kind != (ResourcePropertyKind::COMPLEX_TYPE | ResourcePropertyKind::BAG)) && |
|
205
|
|
|
$kind != ResourcePropertyKind::PRIMITIVE && |
|
206
|
|
|
($kind != (ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::BAG)) && |
|
207
|
|
|
($kind != (ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY)) && |
|
208
|
|
|
($kind != (ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::ETAG))); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Check the specified resource kind is valid resource kind for property kind. |
|
213
|
|
|
* |
|
214
|
|
|
* @param ResourcePropertyKind $pKind The kind of resource property |
|
215
|
|
|
* @param ResourceTypeKind $rKind The kind of resource type |
|
216
|
|
|
* |
|
217
|
|
|
* @return bool True if resource type kind and property kind matches |
|
218
|
|
|
* otherwise false |
|
219
|
|
|
*/ |
|
220
|
|
|
private function isResourceKindValidForPropertyKind($pKind, $rKind) |
|
221
|
|
|
{ |
|
222
|
|
|
if (self::sIsKindOf($pKind, ResourcePropertyKind::PRIMITIVE) |
|
223
|
|
|
&& $rKind != ResourceTypeKind::PRIMITIVE |
|
224
|
|
|
) { |
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
if (self::sIsKindOf($pKind, ResourcePropertyKind::COMPLEX_TYPE) |
|
229
|
|
|
&& $rKind != ResourceTypeKind::COMPLEX |
|
230
|
|
|
) { |
|
231
|
|
|
return false; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if ((self::sIsKindOf($pKind, ResourcePropertyKind::RESOURCE_REFERENCE) |
|
235
|
|
|
|| self::sIsKindOf($pKind, ResourcePropertyKind::RESOURCESET_REFERENCE)) |
|
236
|
|
|
&& $rKind != ResourceTypeKind::ENTITY |
|
237
|
|
|
) { |
|
238
|
|
|
return false; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return true; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|