1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata; |
4
|
|
|
|
5
|
|
|
use POData\Common\Messages; |
6
|
|
|
use POData\Providers\Metadata\Type\IType; |
7
|
|
|
use InvalidArgumentException; |
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 once 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)) { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Checks whether resource property kind is valid or not. |
192
|
|
|
* |
193
|
|
|
* @param ResourcePropertyKind $kind The kind to validate |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
private function _isValidResourcePropertyKind($kind) |
198
|
|
|
{ |
199
|
|
|
return |
200
|
|
|
!($kind != ResourcePropertyKind::RESOURCE_REFERENCE && |
201
|
|
|
$kind != ResourcePropertyKind::RESOURCESET_REFERENCE && |
202
|
|
|
$kind != ResourcePropertyKind::COMPLEX_TYPE && |
203
|
|
|
($kind != (ResourcePropertyKind::COMPLEX_TYPE | ResourcePropertyKind::BAG)) && |
204
|
|
|
$kind != ResourcePropertyKind::PRIMITIVE && |
205
|
|
|
($kind != (ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::BAG)) && |
206
|
|
|
($kind != (ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY)) && |
207
|
|
|
($kind != (ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::ETAG))); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Check the specified resource kind is valid resource kind for property kind. |
212
|
|
|
* |
213
|
|
|
* @param ResourcePropertyKind $pKind The kind of resource property |
214
|
|
|
* @param ResourceTypeKind $rKind The kind of resource type |
215
|
|
|
* |
216
|
|
|
* @return bool True if resource type kind and property kind matches |
217
|
|
|
* otherwise false |
218
|
|
|
*/ |
219
|
|
|
private function _isResourceKindValidForPropertyKind($pKind, $rKind) |
220
|
|
|
{ |
221
|
|
|
if (self::sIsKindOf($pKind, ResourcePropertyKind::PRIMITIVE) |
222
|
|
|
&& $rKind != ResourceTypeKind::PRIMITIVE |
223
|
|
|
) { |
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if (self::sIsKindOf($pKind, ResourcePropertyKind::COMPLEX_TYPE) |
228
|
|
|
&& $rKind != ResourceTypeKind::COMPLEX |
229
|
|
|
) { |
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if ((self::sIsKindOf($pKind, ResourcePropertyKind::RESOURCE_REFERENCE) |
234
|
|
|
|| self::sIsKindOf($pKind, ResourcePropertyKind::RESOURCESET_REFERENCE)) |
235
|
|
|
&& $rKind != ResourceTypeKind::ENTITY |
236
|
|
|
) { |
237
|
|
|
return false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|