1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Describes a Swagger Parameter object of all varieties except "body". |
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 Parameter extends AbstractObject implements IParameter |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private static $classTypes = array( |
17
|
|
|
'integer' => 'Integer', |
18
|
|
|
'int' => 'Integer', |
19
|
|
|
'int32' => 'Integer', |
20
|
|
|
'int64' => 'Integer', |
21
|
|
|
'long' => 'Integer', |
22
|
|
|
'float' => 'Number', |
23
|
|
|
'double' => 'Number', |
24
|
|
|
'string' => 'String', |
25
|
|
|
'byte' => 'String', |
26
|
|
|
'binary' => 'String', |
27
|
|
|
'password' => 'String', |
28
|
|
|
'enum' => 'String', |
29
|
|
|
'boolean' => 'Boolean', |
30
|
|
|
'bool' => 'Boolean', |
31
|
|
|
'array' => 'Array', |
32
|
|
|
'csv' => 'Array', |
33
|
|
|
'ssv' => 'Array', |
34
|
|
|
'tsv' => 'Array', |
35
|
|
|
'pipes' => 'Array', |
36
|
|
|
'multi' => 'Array', |
37
|
|
|
'date' => 'Date', |
38
|
|
|
'datetime' => 'Date', |
39
|
|
|
'date-time' => 'Date', |
40
|
|
|
'file' => 'File', |
41
|
|
|
//'set' => 'EnumArray', |
|
|
|
|
42
|
|
|
); |
43
|
|
|
private $name = ''; |
44
|
|
|
private $in; |
45
|
|
|
private $description; |
46
|
|
|
private $required = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Type\AbstractType |
50
|
|
|
*/ |
51
|
|
|
private $Type; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns true if the "multi" array collectionFormat is allowed for this |
55
|
|
|
* parameter. |
56
|
|
|
* @return boolean |
57
|
|
|
*/ |
58
|
|
|
public function isMulti() |
59
|
|
|
{ |
60
|
|
|
return in_array($this->in, array('query', 'form')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return true if the parameter is of type 'formData' |
65
|
|
|
* @return type |
66
|
|
|
*/ |
67
|
|
|
public function isForm() |
68
|
|
|
{ |
69
|
|
|
return $this->in === 'form'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function __construct(AbstractObject $parent, $in, $data, $required = false) |
73
|
|
|
{ |
74
|
|
|
parent::__construct($parent); |
75
|
|
|
|
76
|
|
|
if ($in !== 'path' && $in !== 'form' && $in !== 'query' && $in !== 'header') { |
77
|
|
|
throw new \SwaggerGen\Exception("Invalid in for parameter: '{$in}'"); |
78
|
|
|
} |
79
|
|
|
$this->in = $in; |
80
|
|
|
|
81
|
|
|
$definition = self::wordShift($data); |
82
|
|
|
if (empty($definition)) { |
83
|
|
|
throw new \SwaggerGen\Exception('No type definition for parameter'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->name = self::wordShift($data); |
|
|
|
|
87
|
|
|
if (empty($this->name)) { |
88
|
|
|
throw new \SwaggerGen\Exception('No name for parameter'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->description = $data; |
92
|
|
|
$this->required = (bool) $required; |
93
|
|
|
|
94
|
|
|
// Parse regex |
95
|
|
|
$match = array(); |
96
|
|
|
$count = preg_match('/^([a-z]+)/i', $definition, $match); |
|
|
|
|
97
|
|
|
$format = strtolower($match[1]); |
98
|
|
View Code Duplication |
if (isset(self::$classTypes[$format])) { |
|
|
|
|
99
|
|
|
$type = self::$classTypes[$format]; |
100
|
|
|
$class = "SwaggerGen\\Swagger\\Type\\{$type}Type"; |
101
|
|
|
$this->Type = new $class($this, $definition); |
102
|
|
|
} else { |
103
|
|
|
throw new \SwaggerGen\Exception("Type format not recognized: '{$format}'"); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function handleCommand($command, $data = null) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
// Pass through to Type |
110
|
|
|
if ($this->Type && $this->Type->handleCommand($command, $data)) { |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return parent::handleCommand($command, $data); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function toArray() |
118
|
|
|
{ |
119
|
|
|
return self::arrayFilterNull(array_merge(array( |
120
|
|
|
'name' => $this->name, |
121
|
|
|
'in' => $this->in === 'form' ? 'formData' : $this->in, |
122
|
|
|
'description' => empty($this->description) ? null : $this->description, |
123
|
|
|
'required' => $this->in === 'path' || $this->required ? true : null, |
124
|
|
|
), $this->Type->toArray(), parent::toArray())); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function __toString() |
128
|
|
|
{ |
129
|
|
|
return __CLASS__ . " {$this->name} {$this->in}"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getName() |
133
|
|
|
{ |
134
|
|
|
return $this->name; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.