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