1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the tmilos/scim-schema package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tmilos\ScimSchema\Model\Schema; |
13
|
|
|
|
14
|
|
|
use Tmilos\ScimSchema\Model\SerializableInterface; |
15
|
|
|
use Tmilos\ScimSchema\ScimConstants; |
16
|
|
|
|
17
|
|
|
class Attribute implements SerializableInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** @var bool */ |
26
|
|
|
protected $multiValued = false; |
27
|
|
|
|
28
|
|
|
/** @var bool */ |
29
|
|
|
protected $required = false; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $description; |
33
|
|
|
|
34
|
|
|
/** @var Attribute[] */ |
35
|
|
|
protected $subAttributes = []; |
36
|
|
|
|
37
|
|
|
/** @var string[] */ |
38
|
|
|
protected $canonicalValues = []; |
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
protected $caseExact = false; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
protected $mutability = ScimConstants::MUTABILITY_READ_WRITE; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
protected $returned = ScimConstants::RETURNED_ALWAYS; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
protected $uniqueness = ScimConstants::UNIQUENESS_NONE; |
51
|
|
|
|
52
|
|
|
/** @var string[] */ |
53
|
|
|
protected $referenceTypes = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* @param string $type |
58
|
|
|
* @param bool $multiValued |
59
|
|
|
* @param bool $required |
60
|
|
|
* @param string $description |
61
|
|
|
* @param Attribute[] $subAttributes |
62
|
|
|
* @param \string[] $canonicalValues |
63
|
|
|
* @param bool $caseExact |
64
|
|
|
* @param string $mutability |
65
|
|
|
* @param string $returned |
66
|
|
|
* @param string $uniqueness |
67
|
|
|
* @param \string[] $referenceTypes |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
$name, |
71
|
|
|
$type, |
72
|
|
|
$multiValued, |
73
|
|
|
$required, |
74
|
|
|
$description, |
75
|
|
|
array $subAttributes, |
76
|
|
|
array $canonicalValues, |
77
|
|
|
$caseExact, |
78
|
|
|
$mutability, |
79
|
|
|
$returned, |
80
|
|
|
$uniqueness, |
81
|
|
|
array $referenceTypes |
82
|
|
|
) { |
83
|
|
|
$this->name = $name; |
84
|
|
|
$this->type = $type; |
85
|
|
|
$this->multiValued = $multiValued; |
86
|
|
|
$this->required = $required; |
87
|
|
|
$this->description = $description; |
88
|
|
|
$this->subAttributes = $subAttributes; |
89
|
|
|
$this->canonicalValues = $canonicalValues; |
90
|
|
|
$this->caseExact = $caseExact; |
91
|
|
|
$this->mutability = $mutability; |
92
|
|
|
$this->returned = $returned; |
93
|
|
|
$this->uniqueness = $uniqueness; |
94
|
|
|
$this->referenceTypes = $referenceTypes === null ? [] : $referenceTypes; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getName() |
101
|
|
|
{ |
102
|
|
|
return $this->name; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getType() |
109
|
|
|
{ |
110
|
|
|
return $this->type; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isMultiValued() |
117
|
|
|
{ |
118
|
|
|
return $this->multiValued; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isRequired() |
125
|
|
|
{ |
126
|
|
|
return $this->required; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getDescription() |
133
|
|
|
{ |
134
|
|
|
return $this->description; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return Attribute[] |
139
|
|
|
*/ |
140
|
|
|
public function getSubAttributes() |
141
|
|
|
{ |
142
|
|
|
return $this->subAttributes; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return \string[] |
147
|
|
|
*/ |
148
|
|
|
public function getCanonicalValues() |
149
|
|
|
{ |
150
|
|
|
return $this->canonicalValues; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function isCaseExact() |
157
|
|
|
{ |
158
|
|
|
return $this->caseExact; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getMutability() |
165
|
|
|
{ |
166
|
|
|
return $this->mutability; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getReturned() |
173
|
|
|
{ |
174
|
|
|
return $this->returned; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getUniqueness() |
181
|
|
|
{ |
182
|
|
|
return $this->uniqueness; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return \string[] |
187
|
|
|
*/ |
188
|
|
|
public function getReferenceTypes() |
189
|
|
|
{ |
190
|
|
|
return $this->referenceTypes; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $name |
195
|
|
|
* |
196
|
|
|
* @return null|Attribute |
197
|
|
|
*/ |
198
|
|
|
public function findAttribute($name) |
199
|
|
|
{ |
200
|
|
|
foreach ($this->subAttributes as $attribute) { |
201
|
|
|
if ($attribute->getName() === $name) { |
202
|
|
|
return $attribute; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function isValueValid($value) |
210
|
|
|
{ |
211
|
|
|
switch ($this->type) { |
212
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_STRING: return is_string($value); |
213
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_BOOLEAN: return is_bool($value); |
214
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_DECIMAL: return is_float($value) || is_int($value); |
215
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_INTEGER: return is_int($value); |
216
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_DATETIME: |
217
|
|
|
return $value instanceof \DateTime || false !== \DateTime::createFromFormat(\DateTime::ATOM, $value); |
218
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_BINARY: return true; |
219
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_REFERENCE: return is_string($value); // improve this |
220
|
|
|
case ScimConstants::ATTRIBUTE_TYPE_COMPLEX: return is_array($value) || is_object($value); |
221
|
|
|
default: throw new \LogicException('Unrecognized attribute type: '.$this->type); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
public function serializeObject() |
229
|
|
|
{ |
230
|
|
|
$result = [ |
231
|
|
|
'name' => $this->name, |
232
|
|
|
]; |
233
|
|
|
if ($this->description) { |
234
|
|
|
$result['description'] = $this->description; |
235
|
|
|
} |
236
|
|
|
$result['type'] = $this->type; |
237
|
|
|
$result['mutability'] = $this->mutability; |
238
|
|
|
$result['returned'] = $this->returned; |
239
|
|
|
|
240
|
|
|
if ($this->type !== ScimConstants::ATTRIBUTE_TYPE_BOOLEAN) { |
241
|
|
|
$result['uniqueness'] = $this->uniqueness; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$result['required'] = $this->required; |
245
|
|
|
$result['multiValued'] = $this->multiValued; |
246
|
|
|
$result['caseExact'] = $this->caseExact; |
247
|
|
|
|
248
|
|
|
if ($this->subAttributes) { |
|
|
|
|
249
|
|
|
$result['subAttributes'] = []; |
250
|
|
|
foreach ($this->subAttributes as $subAttribute) { |
251
|
|
|
$result['subAttributes'][] = $subAttribute->serializeObject(); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
if ($this->canonicalValues) { |
|
|
|
|
255
|
|
|
$result['canonicalValues'] = $this->canonicalValues; |
256
|
|
|
} |
257
|
|
|
if ($this->referenceTypes) { |
|
|
|
|
258
|
|
|
$result['referenceTypes'] = $this->referenceTypes; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $result; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param array $data |
266
|
|
|
* |
267
|
|
|
* @return Attribute |
268
|
|
|
*/ |
269
|
|
|
public static function deserializeObject(array $data) |
270
|
|
|
{ |
271
|
|
|
$subAttributes = []; |
272
|
|
|
if (isset($data['subAttributes'])) { |
273
|
|
|
foreach ($data['subAttributes'] as $subAttribute) { |
274
|
|
|
$subAttributes[] = static::deserializeObject($subAttribute); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
$result = new static( |
278
|
|
|
$data['name'], |
279
|
|
|
$data['type'], |
280
|
|
|
$data['multiValued'], |
281
|
|
|
$data['required'], |
282
|
|
|
isset($data['description']) ? $data['description'] : null, |
283
|
|
|
$subAttributes, |
284
|
|
|
isset($data['canonicalValues']) ? $data['canonicalValues'] : [], |
285
|
|
|
isset($data['caseExact']) ? $data['caseExact'] : false, |
286
|
|
|
$data['mutability'], |
287
|
|
|
$data['returned'], |
288
|
|
|
isset($data['uniqueness']) ? $data['uniqueness'] : null, |
289
|
|
|
isset($data['referenceTypes']) ? $data['referenceTypes'] : [] |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
return $result; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|