1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Describes a Swagger Schema object, defining a primitive type, object, array |
7
|
|
|
* or reference to a defined schema. |
8
|
|
|
* |
9
|
|
|
* @package SwaggerGen |
10
|
|
|
* @author Martijn van der Lee <[email protected]> |
11
|
|
|
* @copyright 2014-2015 Martijn van der Lee |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
13
|
|
|
*/ |
14
|
|
|
class Schema extends AbstractDocumentableObject implements IDefinition |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private static $classTypes = array( |
18
|
|
|
'integer' => 'Integer', |
19
|
|
|
'int' => 'Integer', |
20
|
|
|
'int32' => 'Integer', |
21
|
|
|
'int64' => 'Integer', |
22
|
|
|
'long' => 'Integer', |
23
|
|
|
'float' => 'Number', |
24
|
|
|
'double' => 'Number', |
25
|
|
|
'string' => 'String', |
26
|
|
|
'uuid' => 'StringUuid', |
27
|
|
|
'byte' => 'String', |
28
|
|
|
'binary' => 'String', |
29
|
|
|
'password' => 'String', |
30
|
|
|
'enum' => 'String', |
31
|
|
|
'boolean' => 'Boolean', |
32
|
|
|
'bool' => 'Boolean', |
33
|
|
|
'array' => 'Array', |
34
|
|
|
'csv' => 'Array', |
35
|
|
|
'ssv' => 'Array', |
36
|
|
|
'tsv' => 'Array', |
37
|
|
|
'pipes' => 'Array', |
38
|
|
|
'date' => 'Date', |
39
|
|
|
'datetime' => 'Date', |
40
|
|
|
'date-time' => 'Date', |
41
|
|
|
'object' => 'Object', |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $description = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $title = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
private $readOnly = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var \SwaggerGen\Swagger\Type\AbstractType |
61
|
|
|
*/ |
62
|
|
|
private $type; |
63
|
|
|
|
64
|
|
|
public function __construct(AbstractObject $parent, $definition = 'object', $description = null) |
65
|
|
|
{ |
66
|
|
|
parent::__construct($parent); |
67
|
|
|
|
68
|
|
|
// Check if definition set |
69
|
|
|
if ($this->getSwagger()->hasDefinition($definition)) { |
70
|
|
|
$this->type = new Type\ReferenceObjectType($this, $definition); |
71
|
|
|
} else { |
72
|
|
|
// Parse regex |
73
|
|
|
$match = array(); |
74
|
|
|
preg_match('/^([a-z]+)/i', $definition, $match); |
75
|
|
|
$format = strtolower($match[1]); |
76
|
|
|
|
77
|
|
|
// Internal type if type known and not overwritten by definition |
78
|
|
|
if (isset(self::$classTypes[$format])) { |
79
|
|
|
$type = self::$classTypes[$format]; |
80
|
|
|
$class = "SwaggerGen\\Swagger\\Type\\{$type}Type"; |
81
|
|
|
$this->type = new $class($this, $definition); |
82
|
|
|
} else { |
83
|
|
|
$this->type = new Type\ReferenceObjectType($this, $definition); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->description = $description; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $command |
92
|
|
|
* @param string $data |
93
|
|
|
* @return \SwaggerGen\Swagger\AbstractObject|boolean |
94
|
|
|
*/ |
95
|
|
|
public function handleCommand($command, $data = null) |
96
|
|
|
{ |
97
|
|
|
// Pass through to Type |
98
|
|
|
if ($this->type && $this->type->handleCommand($command, $data)) { |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// handle all the rest manually |
103
|
|
|
switch (strtolower($command)) { |
104
|
|
|
case 'description': |
105
|
|
|
$this->description = $data; |
106
|
|
|
return $this; |
107
|
|
|
|
108
|
|
|
case 'title': |
109
|
|
|
$this->title = $data; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return parent::handleCommand($command, $data); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function toArray() |
117
|
|
|
{ |
118
|
|
|
return self::arrayFilterNull(array_merge($this->type->toArray(), array( |
119
|
|
|
'title' => empty($this->title) ? null : $this->title, |
120
|
|
|
'description' => empty($this->description) ? null : $this->description, |
121
|
|
|
'readOnly' => $this->readOnly |
122
|
|
|
), parent::toArray())); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function __toString() |
126
|
|
|
{ |
127
|
|
|
return __CLASS__; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function setReadOnly() |
131
|
|
|
{ |
132
|
|
|
$this->readOnly = true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|