1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Request; |
9
|
|
|
|
10
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Parameter; |
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author John Kleijn <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ParameterCoercer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Try to coerce a value into its defined type. |
20
|
|
|
* |
21
|
|
|
* If coersion is not possible, will return the original value, to be picked up by validation. |
22
|
|
|
* |
23
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
24
|
|
|
* |
25
|
|
|
* @param Parameter $parameter |
26
|
|
|
* @param mixed $value |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function coerce(Parameter $parameter, $value) |
31
|
|
|
{ |
32
|
|
|
$schema = $parameter->getSchema(); |
33
|
|
|
|
34
|
|
|
switch ($schema->getType()) { |
35
|
|
|
case Schema::TYPE_STRING: |
36
|
|
|
return (string)$value; |
37
|
|
|
case Schema::TYPE_BOOL: |
38
|
|
|
if (!is_scalar($value)) { |
39
|
|
|
return $value; |
40
|
|
|
} |
41
|
|
|
$bool = $this->coerceBooleanValue($value); |
42
|
|
|
|
43
|
|
|
return $bool === null ? $value : $bool; |
44
|
|
|
case Schema::TYPE_NUMBER: |
45
|
|
|
if (!is_numeric($value)) { |
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return ctype_digit($value) ? (int)$value : (float)$value; |
50
|
|
|
case Schema::TYPE_OBJECT: |
51
|
|
|
if (!is_array($value)) { |
52
|
|
|
return $value == '' ? null : $value; |
53
|
|
|
} |
54
|
|
|
if (count($value) && is_numeric(key($value))) { |
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return (object)$value; |
59
|
|
|
case Schema::TYPE_ARRAY: |
60
|
|
|
if (is_array($value) || !is_string($value)) { |
61
|
|
|
return $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->coerceArrayValue( |
65
|
|
|
$parameter->getCollectionFormat() ? $parameter->getCollectionFormat() : 'csv', |
66
|
|
|
$value |
67
|
|
|
); |
68
|
|
|
case Schema::TYPE_INT: |
69
|
|
|
if (!ctype_digit($value)) { |
70
|
|
|
return $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return (integer)$value; |
74
|
|
|
case Schema::TYPE_NULL: |
75
|
|
|
if ($value !== '') { |
76
|
|
|
return $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
default: |
81
|
|
|
return $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $format |
87
|
|
|
* @param string $value |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
* @throws \RuntimeException |
91
|
|
|
*/ |
92
|
|
|
protected function coerceArrayValue(string $format, $value): array |
93
|
|
|
{ |
94
|
|
|
switch ($format) { |
95
|
|
|
case 'csv': |
96
|
|
|
return explode(',', $value); |
97
|
|
|
case 'ssv': |
98
|
|
|
return explode(' ', $value); |
99
|
|
|
case 'tsv': |
100
|
|
|
return explode("\t", $value); |
101
|
|
|
case 'pipes': |
102
|
|
|
return explode('|', $value); |
103
|
|
|
default: |
104
|
|
|
throw new \RuntimeException( |
105
|
|
|
"Array 'collectionFormat' '$format' is not currently supported" |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $value |
112
|
|
|
* |
113
|
|
|
* @return bool|null |
114
|
|
|
*/ |
115
|
|
|
protected function coerceBooleanValue($value) |
116
|
|
|
{ |
117
|
|
|
switch ((string)$value) { |
118
|
|
|
case 'TRUE': |
119
|
|
|
case 'true': |
120
|
|
|
case '1': |
121
|
|
|
return true; |
122
|
|
|
case 'FALSE': |
123
|
|
|
case 'false': |
124
|
|
|
case '0': |
125
|
|
|
return false; |
126
|
|
|
default: |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|