|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger\Type; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Basic decimal-point numeric type definition. |
|
7
|
|
|
* |
|
8
|
|
|
* @package SwaggerGen |
|
9
|
|
|
* @author Martijn van der Lee <[email protected]> |
|
10
|
|
|
* @copyright 2014-2015 Martijn van der Lee |
|
11
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
class NumberType extends AbstractType |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
const REGEX_RANGE = '(?:([[<])(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?,(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*))?([\\]>]))?'; |
|
17
|
|
|
const REGEX_DEFAULT = '(?:=(-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)))?'; |
|
18
|
|
|
|
|
19
|
|
|
private static $formats = array( |
|
20
|
|
|
'float' => 'float', |
|
21
|
|
|
'double' => 'double', |
|
22
|
|
|
); |
|
23
|
|
|
private $format; |
|
24
|
|
|
private $default = null; |
|
25
|
|
|
private $maximum = null; |
|
26
|
|
|
private $exclusiveMaximum = null; |
|
27
|
|
|
private $minimum = null; |
|
28
|
|
|
private $exclusiveMinimum = null; |
|
29
|
|
|
private $enum = array(); |
|
30
|
|
|
private $multipleOf = null; |
|
31
|
|
|
|
|
32
|
|
|
protected function parseDefinition($definition) |
|
33
|
|
|
{ |
|
34
|
|
|
$match = array(); |
|
35
|
|
|
if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) { |
|
36
|
|
|
throw new \SwaggerGen\Exception("Unparseable number definition: '{$definition}'"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->parseFormat($definition, $match); |
|
40
|
|
|
$this->parseRange($definition, $match); |
|
41
|
|
|
$this->parseDefault($definition, $match); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function parseFormat($definition, $match) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!isset(self::$formats[strtolower($match[1])])) { |
|
47
|
|
|
throw new \SwaggerGen\Exception("Not a number: '{$definition}'"); |
|
48
|
|
|
} |
|
49
|
|
|
$this->format = self::$formats[strtolower($match[1])]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function parseRange($definition, $match) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!empty($match[2])) { |
|
55
|
|
|
if ($match[3] === '' && $match[4] === '') { |
|
56
|
|
|
throw new \SwaggerGen\Exception("Empty number range: '{$definition}'"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->exclusiveMinimum = isset($match[2]) ? ($match[2] == '<') : null; |
|
60
|
|
|
$this->minimum = $match[3] === '' ? null : doubleval($match[3]); |
|
61
|
|
|
$this->maximum = $match[4] === '' ? null : doubleval($match[4]); |
|
62
|
|
|
$this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
|
63
|
|
|
if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
|
64
|
|
|
self::swap($this->minimum, $this->maximum); |
|
65
|
|
|
self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function parseDefault($definition, $match) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $command The comment command |
|
77
|
|
|
* @param string $data Any data added after the command |
|
78
|
|
|
* @return \SwaggerGen\Swagger\Type\AbstractType|boolean |
|
79
|
|
|
*/ |
|
80
|
|
|
public function handleCommand($command, $data = null) |
|
81
|
|
|
{ |
|
82
|
|
|
switch (strtolower($command)) { |
|
83
|
|
|
case 'default': |
|
84
|
|
|
$this->default = $this->validateDefault($data); |
|
85
|
|
|
return $this; |
|
86
|
|
|
|
|
87
|
|
|
case 'enum': |
|
88
|
|
|
$words = self::wordSplit($data); |
|
89
|
|
|
foreach ($words as &$word) { |
|
90
|
|
|
$word = $this->validateDefault($word); |
|
91
|
|
|
} |
|
92
|
|
|
$this->enum = array_merge($this->enum, $words); |
|
93
|
|
|
return $this; |
|
94
|
|
|
|
|
95
|
|
|
case 'step': |
|
96
|
|
|
if (($step = doubleval($data)) > 0) { |
|
97
|
|
|
$this->multipleOf = $step; |
|
98
|
|
|
} |
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return parent::handleCommand($command, $data); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function toArray() |
|
106
|
|
|
{ |
|
107
|
|
|
return self::arrayFilterNull(array_merge(array( |
|
108
|
|
|
'type' => 'number', |
|
109
|
|
|
'format' => $this->format, |
|
110
|
|
|
'default' => $this->default, |
|
111
|
|
|
'minimum' => $this->minimum, |
|
112
|
|
|
'exclusiveMinimum' => ($this->exclusiveMinimum && !is_null($this->minimum)) ? true : null, |
|
113
|
|
|
'maximum' => $this->maximum, |
|
114
|
|
|
'exclusiveMaximum' => ($this->exclusiveMaximum && !is_null($this->maximum)) ? true : null, |
|
115
|
|
|
'enum' => $this->enum, |
|
116
|
|
|
'multipleOf' => $this->multipleOf, |
|
117
|
|
|
), parent::toArray())); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function __toString() |
|
121
|
|
|
{ |
|
122
|
|
|
return __CLASS__; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function validateDefault($value) |
|
126
|
|
|
{ |
|
127
|
|
|
if (preg_match('~^-?(?:\\d*\\.?\\d+|\\d+\\.\\d*)$~', $value) !== 1) { |
|
128
|
|
|
throw new \SwaggerGen\Exception("Invalid number default: '{$value}'"); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if ($this->maximum) { |
|
132
|
|
|
if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
|
133
|
|
|
throw new \SwaggerGen\Exception("Default number beyond maximum: '{$value}'"); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
if ($this->minimum) { |
|
137
|
|
|
if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
|
138
|
|
|
throw new \SwaggerGen\Exception("Default number beyond minimum: '{$value}'"); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return doubleval($value); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.