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
|
|
View Code Duplication |
private function parseFormat($definition, $match) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$type = strtolower($match[1]); |
52
|
|
|
if (!isset(self::$formats[$type])) { |
53
|
|
|
throw new \SwaggerGen\Exception("Not an integer: '{$definition}'"); |
54
|
|
|
} |
55
|
|
|
$this->format = self::$formats[$type]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
private function parseRange($definition, $match) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (!empty($match[2])) { |
61
|
|
|
if ($match[3] === '' && $match[4] === '') { |
62
|
|
|
throw new \SwaggerGen\Exception("Empty integer range: '{$definition}'"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->exclusiveMinimum = isset($match[2]) ? ($match[2] == '<') : null; |
66
|
|
|
$this->minimum = $match[3] === '' ? null : intval($match[3]); |
67
|
|
|
$this->maximum = $match[4] === '' ? null : intval($match[4]); |
68
|
|
|
$this->exclusiveMaximum = isset($match[5]) ? ($match[5] == '>') : null; |
69
|
|
|
if ($this->minimum && $this->maximum && $this->minimum > $this->maximum) { |
70
|
|
|
self::swap($this->minimum, $this->maximum); |
71
|
|
|
self::swap($this->exclusiveMinimum, $this->exclusiveMaximum); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function parseDefault($definition, $match) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->default = isset($match[6]) && $match[6] !== '' ? $this->validateDefault($match[6]) : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $command The comment command |
83
|
|
|
* @param string $data Any data added after the command |
84
|
|
|
* @return \SwaggerGen\Swagger\Type\AbstractType|boolean |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function handleCommand($command, $data = null) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
switch (strtolower($command)) { |
89
|
|
|
case 'default': |
90
|
|
|
$this->default = $this->validateDefault($data); |
91
|
|
|
return $this; |
92
|
|
|
|
93
|
|
|
case 'enum': |
94
|
|
|
$words = self::wordSplit($data); |
95
|
|
|
foreach ($words as &$word) { |
96
|
|
|
$word = $this->validateDefault($word); |
97
|
|
|
} |
98
|
|
|
$this->enum = array_merge($this->enum, $words); |
99
|
|
|
return $this; |
100
|
|
|
|
101
|
|
|
case 'step': |
102
|
|
|
if (($step = intval($data)) > 0) { |
103
|
|
|
$this->multipleOf = $step; |
104
|
|
|
} |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return parent::handleCommand($command, $data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function toArray() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
return self::arrayFilterNull(array( |
114
|
|
|
'type' => 'integer', |
115
|
|
|
'format' => $this->format, |
116
|
|
|
'default' => $this->default, |
117
|
|
|
'minimum' => $this->minimum, |
118
|
|
|
'exclusiveMinimum' => $this->exclusiveMinimum ? true : null, |
119
|
|
|
'maximum' => $this->maximum, |
120
|
|
|
'exclusiveMaximum' => $this->exclusiveMaximum ? true : null, |
121
|
|
|
'enum' => $this->enum, |
122
|
|
|
'multipleOf' => $this->multipleOf, |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function __toString() |
127
|
|
|
{ |
128
|
|
|
return __CLASS__; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
private function validateDefault($value) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
if (preg_match('~^-?\d+$~', $value) !== 1) { |
134
|
|
|
throw new \SwaggerGen\Exception("Invalid integer default: '{$value}'"); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($this->maximum) { |
138
|
|
|
if (($value > $this->maximum) || ($this->exclusiveMaximum && $value == $this->maximum)) { |
139
|
|
|
throw new \SwaggerGen\Exception("Default integer beyond maximum: '{$value}'"); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
if ($this->minimum) { |
143
|
|
|
if (($value < $this->minimum) || ($this->exclusiveMinimum && $value == $this->minimum)) { |
144
|
|
|
throw new \SwaggerGen\Exception("Default integer beyond minimum: '{$value}'"); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return intval($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.