This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Parameter.php
380 lines (341 loc) · 8.9 KB
/
Parameter.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
<?php
namespace RREST;
use RREST\Exception\InvalidParameterException;
class Parameter
{
const TYPE_NUMBER = 'number';
const TYPE_BOOLEAN = 'boolean';
const TYPE_STRING = 'string';
const TYPE_DATE_ONLY = 'date-only';
const TYPE_TIME_ONLY = 'time-only';
const TYPE_DATETIME_ONLY = 'datetime-only';
const TYPE_DATETIME = 'datetime';
const TYPE_FILE = 'file';
const TYPE_INTEGER = 'integer';
/**
* @var string[]
*/
protected $validTypes = [
self::TYPE_NUMBER,
self::TYPE_STRING,
self::TYPE_BOOLEAN,
self::TYPE_DATE_ONLY,
self::TYPE_TIME_ONLY,
self::TYPE_DATETIME_ONLY,
self::TYPE_DATETIME,
self::TYPE_FILE,
self::TYPE_INTEGER,
];
/**
* Define types that can have a maximum or a minimum.
*
* @var string[]
*/
protected $minmaxTypes = [
self::TYPE_STRING,
self::TYPE_NUMBER,
self::TYPE_INTEGER,
];
/**
* The name of the parameter.
*
* @var string
*/
private $name;
/**
* The primitive type of the parameter.
*
* @var string
*/
private $type;
/**
* If the parameter is required.
*
* @var bool
*/
protected $required;
/**
* List of valid values for the parameter (optional).
*
* @var string[]
*/
private $enum;
/**
* A regular expression pattern for the string to match against (optional).
*
* @var string
*/
private $validationPattern;
/**
* The minimum for a string length, integer or number (optional).
*
* @var int|null
*/
private $minimum;
/**
* The maximum for a string length, a integer or number (optional).
*
* @var int|null
*/
private $maximum;
/**
* A valid DateTime format
* Default RFC2616.
*
* @var string
*/
private $dateFormat = 'D, d M Y H:i:s T';
/**
* @param string $name
* @param string $type
* @param bool $required
*/
public function __construct($name, $type, $required)
{
$this->setName($name);
$this->setType($type);
$this->setRequired($required);
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $type
*
* @throws \RuntimeException
*/
public function setType($type)
{
if (!in_array($type, $this->validTypes)) {
throw new \RuntimeException(
$type.' is not a valid type ('.implode(',', $this->validTypes).')'
);
}
$this->type = $type;
}
/**
* @return string[]
*/
public function getEnum()
{
return $this->enum;
}
/**
* @param array string[]
*/
public function setEnum(array $enum)
{
$this->enum = $enum;
}
/**
* @return string
*/
public function getValidationPattern()
{
return $this->validationPattern;
}
/**
* @param string $validationPattern
*/
public function setValidationPattern($validationPattern)
{
$this->validationPattern = $validationPattern;
}
/**
* @return int
*/
public function getMinimum()
{
return $this->minimum;
}
/**
* @param int $minimum
*
* @throws \Exception
*/
public function setMinimum($minimum)
{
$this->minimum = \CastToType::cast($minimum, 'integer', false, true);
}
/**
* @return int
*/
public function getMaximum()
{
return $this->maximum;
}
/**
* Set maximum.
*/
public function setMaximum($maximum)
{
$this->maximum = \CastToType::cast($maximum, 'integer', false, true);
}
/**
* @return bool
*/
public function getRequired()
{
return $this->required;
}
/**
* @param bool $required
*/
public function setRequired($required)
{
$this->required = (bool) $required;
}
/**
* @return string
*/
public function getDateFormat()
{
return $this->dateFormat;
}
/**
* @param string $type
*/
public function setDateFormat($dateFormat)
{
$this->dateFormat = $dateFormat;
}
/**
* @param mixed $value The value of the paramater to validate
*
* @throws \RREST\Exception\InvalidParameterException
*/
public function assertValue($value)
{
// required?
if (!$this->hasValue($value)) {
if ($this->getRequired()) {
$this->throwInvalidParameter($this->getName().' is required');
}
//no need to continue, the value is empty & not required
return;
}
// good type?
switch ($this->getType()) {
case static::TYPE_BOOLEAN:
if (!is_bool($value)) {
$this->throwInvalidParameter($this->getName().' is not a boolean');
}
break;
case static::TYPE_DATETIME:
if ($value instanceof \DateTime === false) {
$this->throwInvalidParameter($this->getName().' is not a valid date');
}
break;
case static::TYPE_DATE_ONLY:
case static::TYPE_TIME_ONLY:
case static::TYPE_DATETIME_ONLY:
$this->throwInvalidParameter(
$this->getType().' is not supported yet in RREST. Use datetime or feel free to contribute it'
);
break;
case static::TYPE_STRING:
if (!is_string($value)) {
$this->throwInvalidParameter($this->getName().' is not a string');
}
break;
case static::TYPE_INTEGER:
if (!is_int($value)) {
$this->throwInvalidParameter($this->getName().' is not an integer');
}
break;
case static::TYPE_NUMBER:
if (!is_numeric($value)) {
$this->throwInvalidParameter($this->getName().' is not a number');
}
break;
default:
throw new \RuntimeException();
break;
}
//min & max can only be apply to $this->minmaxTypes because make sense :)
if (in_array($this->getType(), $this->minmaxTypes)) {
$isNumeric = (
$this->getType() === self::TYPE_NUMBER ||
$this->getType() === self::TYPE_INTEGER
);
$isString = $this->getType() === self::TYPE_STRING;
$min = $this->getMinimum();
if (empty($min) === false) {
if (
($isNumeric && $min > $value) ||
($isString && $min > strlen($value))
) {
$this->throwInvalidParameter($this->getName().' minimum size is '.$min);
}
}
$max = $this->getMaximum();
if (empty($max) === false) {
if (
($isNumeric && $max < $value) ||
($isString && $max < strlen($value))
) {
$this->throwInvalidParameter($this->getName().' maximum size is '.$max);
}
}
}
//valid with a pattern?
$validationPattern = $this->getValidationPattern();
if (!empty($validationPattern) &&
preg_match('|'.$validationPattern.'|', $value) !== 1
) {
$this->throwInvalidParameter($this->getName().' does not match the specified pattern: '.$validationPattern);
}
//in enum list?
$enum = $this->getEnum();
if (
empty($enum) === false &&
is_array($enum) &&
in_array($value, $enum, true) === false
) {
$this->throwInvalidParameter($this->getName().' must be one of the following: '.implode(', ', $enum));
}
}
/**
* @param string $message
*
* @throws InvalidParameterException
*/
protected function throwInvalidParameter($message)
{
throw new InvalidParameterException([
new Error($message, 'parameter-invalid'),
]);
}
/**
* @param mixed $value
*/
private function hasValue($value): bool
{
return !empty($value) || $this->isBooleanWithValueFalse($value);
}
/**
* @param mixed $value
*/
private function isBooleanWithValueFalse($value): bool
{
return $this->getType() === self::TYPE_BOOLEAN && is_bool($value) && $value === false;
}
}