Skip to content

Commit f9815d9

Browse files
samdarkcebe
authored andcommitted
Introduce type constants (cebe#2)
1 parent d857320 commit f9815d9

16 files changed

Lines changed: 83 additions & 68 deletions

src/SpecBaseObject.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use cebe\openapi\exceptions\ReadonlyPropertyException;
66
use cebe\openapi\exceptions\UnknownPropertyException;
7+
use cebe\openapi\spec\Type;
78

89
/**
910
* Base class for all spec objects.
@@ -40,25 +41,25 @@ public function __construct(array $data)
4041
continue;
4142
}
4243

43-
if ($type === 'string' || $type === 'any') {
44+
if ($type === Type::STRING || $type === Type::ANY) {
4445
$this->_properties[$property] = $data[$property];
45-
} elseif ($type === 'boolean') {
46-
if (!is_bool($data[$property])) {
46+
} elseif ($type === Type::BOOLEAN) {
47+
if (!\is_bool($data[$property])) {
4748
$this->_errors[] = "property '$property' must be boolean, but " . gettype($data[$property]) . " given.";
4849
continue;
4950
}
5051
$this->_properties[$property] = (bool) $data[$property];
51-
} elseif (is_array($type)) {
52-
if (!is_array($data[$property])) {
52+
} elseif (\is_array($type)) {
53+
if (!\is_array($data[$property])) {
5354
$this->_errors[] = "property '$property' must be array, but " . gettype($data[$property]) . " given.";
5455
continue;
5556
}
56-
switch (count($type)) {
57+
switch (\count($type)) {
5758
case 1:
5859
// array
5960
$this->_properties[$property] = [];
6061
foreach($data[$property] as $item) {
61-
if ($type[0] === 'string') {
62+
if ($type[0] === Type::STRING) {
6263
if (!is_string($item)) {
6364
$this->_errors[] = "property '$property' must be array of strings, but array has " . gettype($item) . " element.";
6465
}
@@ -71,14 +72,14 @@ public function __construct(array $data)
7172
break;
7273
case 2:
7374
// map
74-
if ($type[0] !== 'string') {
75+
if ($type[0] !== Type::STRING) {
7576
throw new \Exception('Invalid map key type: ' . $type[0]);
7677
}
7778
$this->_properties[$property] = [];
7879
foreach($data[$property] as $key => $item) {
7980
if ($type[1] === 'string') {
8081
if (!is_string($item)) {
81-
$this->_errors[] = "property '$property' must be map<string, string>, but entry '$key' is of type " . gettype($item) . ".";
82+
$this->_errors[] = "property '$property' must be map<string, string>, but entry '$key' is of type " . \gettype($item) . '.';
8283
}
8384
$this->_properties[$property][$key] = $item;
8485
} else {
@@ -105,13 +106,13 @@ public function __construct(array $data)
105106
*/
106107
public function validate(): bool
107108
{
108-
foreach($this->_properties as $k => $v) {
109+
foreach($this->_properties as $v) {
109110
if ($v instanceof self) {
110111
$v->validate();
111112
}
112113
}
113114
$this->performValidation();
114-
return count($this->getErrors()) === 0;
115+
return \count($this->getErrors()) === 0;
115116
}
116117

117118
/**
@@ -121,7 +122,7 @@ public function validate(): bool
121122
public function getErrors(): array
122123
{
123124
$errors = [$this->_errors];
124-
foreach($this->_properties as $k => $v) {
125+
foreach($this->_properties as $v) {
125126
if ($v instanceof self) {
126127
$errors[] = $v->getErrors();
127128
}
@@ -173,12 +174,12 @@ public function __get($name)
173174
if (isset(static::attributes()[$name])) {
174175
return is_array(static::attributes()[$name]) ? [] : null;
175176
}
176-
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
177+
throw new UnknownPropertyException('Getting unknown property: ' . \get_class($this) . '::' . $name);
177178
}
178179

179180
public function __set($name, $value)
180181
{
181-
throw new ReadonlyPropertyException('Setting read-only property: ' . get_class($this) . '::' . $name);
182+
throw new ReadonlyPropertyException('Setting read-only property: ' . \get_class($this) . '::' . $name);
182183
}
183184

184185
public function __isset($name)
@@ -192,6 +193,6 @@ public function __isset($name)
192193

193194
public function __unset($name)
194195
{
195-
throw new ReadonlyPropertyException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
196+
throw new ReadonlyPropertyException('Unsetting read-only property: ' . \get_class($this) . '::' . $name);
196197
}
197198
}

src/spec/Contact.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class Contact extends SpecBaseObject
2323
protected function attributes(): array
2424
{
2525
return [
26-
'name' => 'string',
27-
'url' => 'string',
28-
'email' => 'string',
26+
'name' => Type::STRING,
27+
'url' => Type::STRING,
28+
'email' => Type::STRING,
2929
];
3030
}
3131

src/spec/Discriminator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#discriminatorObject
1111
*
1212
* @property-read string $propertyName
13-
* @property-read string[] $ mapping
13+
* @property-read string[] $mapping
1414
*
1515
* @author Carsten Brandt <mail@cebe.cc>
1616
*/
@@ -22,8 +22,8 @@ class Discriminator extends SpecBaseObject
2222
protected function attributes(): array
2323
{
2424
return [
25-
'propertyName' => 'string',
26-
' mapping' => ['string', 'string'],
25+
'propertyName' => Type::STRING,
26+
'mapping' => [Type::STRING, Type::STRING],
2727
];
2828
}
2929

src/spec/ExternalDocumentation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class ExternalDocumentation extends SpecBaseObject
2222
protected function attributes(): array
2323
{
2424
return [
25-
'description' => 'string',
26-
'url' => 'string',
25+
'description' => Type::STRING,
26+
'url' => Type::STRING,
2727
];
2828
}
2929

src/spec/Info.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class Info extends SpecBaseObject
2828
protected function attributes(): array
2929
{
3030
return [
31-
'title' => 'string',
32-
'description' => 'string',
33-
'termsOfService' => 'string',
31+
'title' => Type::STRING,
32+
'description' => Type::STRING,
33+
'termsOfService' => Type::STRING,
3434
'contact' => Contact::class,
3535
'license' => License::class,
36-
'version' => 'string',
36+
'version' => Type::STRING,
3737
];
3838
}
3939

src/spec/License.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class License extends SpecBaseObject
2222
protected function attributes(): array
2323
{
2424
return [
25-
'name' => 'string',
26-
'url' => 'string',
25+
'name' => Type::STRING,
26+
'url' => Type::STRING,
2727
];
2828
}
2929

src/spec/OpenApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class OpenApi extends SpecBaseObject
2828
protected function attributes(): array
2929
{
3030
return [
31-
'openapi' => 'string',
31+
'openapi' => Type::STRING,
3232
'info' => Info::class,
3333
'servers' => [Server::class],
3434
'paths' => Paths::class,

src/spec/Schema.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,26 @@ class Schema extends SpecBaseObject
6464
protected function attributes(): array
6565
{
6666
return [
67-
'type' => 'string',
67+
'type' => Type::STRING,
6868
'allOf' => [Schema::class], // TODO allow reference
6969
'oneOf' => [Schema::class],
7070
'anyOf' => [Schema::class],
7171
'not' => Schema::class,
7272
'items' => Schema::class,
73-
'properties' => ['string', Schema::class],
73+
'properties' => [Type::STRING, Schema::class],
7474
//'additionalProperties' => 'boolean' | ['string', Schema::class], // TODO can be bool?
75-
'description' => 'string',
76-
'format' => 'string',
77-
'default' => 'any',
75+
'description' => Type::STRING,
76+
'format' => Type::STRING,
77+
'default' => Type::ANY,
7878

79-
'nullable' => 'boolean',
79+
'nullable' => Type::BOOLEAN,
8080
'discriminator' => Discriminator::class,
81-
'readOnly' => 'boolean',
82-
'writeOnly' => 'boolean',
81+
'readOnly' => Type::BOOLEAN,
82+
'writeOnly' => Type::BOOLEAN,
8383
'xml' => Xml::class,
8484
'externalDocs' => ExternalDocumentation::class,
85-
'example' => 'any',
86-
'deprecated' => 'boolean',
85+
'example' => Type::ANY,
86+
'deprecated' => Type::BOOLEAN,
8787
];
8888
}
8989

src/spec/Server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class Server extends SpecBaseObject
2323
protected function attributes(): array
2424
{
2525
return [
26-
'url' => 'string',
27-
'description' => 'string',
28-
'variables' => ['string', ServerVariable::class],
26+
'url' => Type::STRING,
27+
'description' => Type::STRING,
28+
'variables' => [Type::STRING, ServerVariable::class],
2929
];
3030
}
3131

src/spec/ServerVariable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class ServerVariable extends SpecBaseObject
2323
protected function attributes(): array
2424
{
2525
return [
26-
'enum' => ['string'],
27-
'default' => 'string',
28-
'description' => 'string',
26+
'enum' => [Type::STRING],
27+
'default' => Type::STRING,
28+
'description' => Type::STRING,
2929
];
3030
}
3131

0 commit comments

Comments
 (0)