Skip to content

Commit 2cbe040

Browse files
committed
move SpecBaseObject and implement map type
1 parent 1820daf commit 2cbe040

14 files changed

Lines changed: 108 additions & 6 deletions

src/Reader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace cebe\openapi;
44

55
use cebe\openapi\spec\OpenApi;
6-
use cebe\openapi\spec\SpecBaseObject;
76
use Symfony\Component\Yaml\Yaml;
87

98
/**
Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3-
namespace cebe\openapi\spec;
3+
namespace cebe\openapi;
44

55
use cebe\openapi\exceptions\ReadonlyPropertyException;
66
use cebe\openapi\exceptions\UnknownPropertyException;
77

88
/**
9+
* Base class for all spec objects.
910
*
11+
* Implements property management and validation basics.
1012
*
1113
* @author Carsten Brandt <mail@cebe.cc>
1214
*/
@@ -45,9 +47,38 @@ public function __construct(array $data)
4547
$this->_errors[] = "property '$property' must be array, but " . gettype($data[$property]) . " given.";
4648
continue;
4749
}
48-
$this->_properties[$property] = [];
49-
foreach($data[$property] as $item) {
50-
$this->_properties[$property][] = new $type[0]($item);
50+
switch (count($type)) {
51+
case 1:
52+
// array
53+
$this->_properties[$property] = [];
54+
foreach($data[$property] as $item) {
55+
if ($type[0] === 'string') {
56+
if (!is_string($item)) {
57+
$this->_errors[] = "property '$property' must be array of strings, but array has " . gettype($item) . " element.";
58+
}
59+
$this->_properties[$property][] = $item;
60+
} else {
61+
$this->_properties[$property][] = new $type[0]($item);
62+
}
63+
}
64+
break;
65+
case 2:
66+
// map
67+
if ($type[0] !== 'string') {
68+
throw new \Exception('Invalid map key type: ' . $type[0]);
69+
}
70+
$this->_properties[$property] = [];
71+
foreach($data[$property] as $key => $item) {
72+
if ($type[1] === 'string') {
73+
if (!is_string($item)) {
74+
$this->_errors[] = "property '$property' must be map<string, string>, but entry '$key' is of type " . gettype($item) . ".";
75+
}
76+
$this->_properties[$property][$key] = $item;
77+
} else {
78+
$this->_properties[$property][$key] = new $type[1]($item);
79+
}
80+
}
81+
break;
5182
}
5283
} else {
5384
$this->_properties[$property] = new $type($data[$property]);
@@ -66,7 +97,6 @@ public function __construct(array $data)
6697
*/
6798
public function validate(): bool
6899
{
69-
$this->_errors = [];
70100
foreach($this->_properties as $k => $v) {
71101
if ($v instanceof self) {
72102
$v->performValidation();

src/spec/Components.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* Holds a set of reusable objects for different aspects of the OAS.
79
*

src/spec/Contact.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* Contact information for the exposed API.
79
*

src/spec/ExternalDocumentation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* Allows referencing an external resource for extended documentation.
79
*

src/spec/Info.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* The object provides metadata about the API.
79
*

src/spec/License.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* License information for the exposed API.
79
*

src/spec/OpenApi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* This is the root document object of the OpenAPI document.
79
*

src/spec/Paths.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* Holds the relative paths to the individual endpoints and their operations.
79
*

src/spec/SecurityRequirement.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace cebe\openapi\spec;
44

5+
use cebe\openapi\SpecBaseObject;
6+
57
/**
68
* Lists the required security schemes to execute this operation.
79
*

0 commit comments

Comments
 (0)