Skip to content

Commit 6973170

Browse files
committed
Allow passing prepared objects instead of array definition
1 parent 3867754 commit 6973170

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/SpecBaseObject.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,14 @@ public function __construct(array $data)
121121
*/
122122
private function instantiate($type, $data)
123123
{
124-
if (isset($data['$ref'])) {
124+
if ($data instanceof $type) {
125+
return $data;
126+
}
127+
128+
if (is_array($data) && isset($data['$ref'])) {
125129
return new Reference($data, $type);
126130
}
131+
127132
try {
128133
return new $type($data);
129134
} catch (\TypeError $e) {

src/spec/Paths.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,24 @@ class Paths implements SpecObjectInterface, ArrayAccess, Countable, IteratorAggr
3939

4040
/**
4141
* Create an object from spec data.
42-
* @param array $data spec data read from YAML or JSON
42+
* @param PathItem[]|array[] $data spec data read from YAML or JSON
4343
* @throws TypeErrorException in case invalid data is supplied.
4444
*/
4545
public function __construct(array $data)
4646
{
4747
foreach ($data as $path => $object) {
4848
if ($object === null) {
4949
$this->_paths[$path] = null;
50-
} else {
50+
} elseif (is_array($object)) {
5151
$this->_paths[$path] = new PathItem($object);
52+
} elseif ($object instanceof PathItem) {
53+
$this->_paths[$path] = $object;
54+
} else {
55+
$givenType = gettype($object);
56+
if ($givenType === 'object') {
57+
$givenType = get_class($object);
58+
}
59+
throw new \TypeError(sprintf('Path MUST be either array or PathItem object, "%s" given', $givenType));
5260
}
5361
}
5462
}

src/spec/Responses.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function __construct(array $data)
4242
// From Spec: This field MUST be enclosed in quotation marks (for example, "200") for compatibility between JSON and YAML.
4343
$statusCode = (string) $statusCode;
4444
if (preg_match('~^(?:default|[1-5](?:[0-9][0-9]|XX))$~', $statusCode)) {
45-
if (isset($response['$ref'])) {
45+
if ($response instanceof Response || $response instanceof Reference) {
46+
$this->_responses[$statusCode] = $response;
47+
} elseif (isset($response['$ref'])) {
4648
$this->_responses[$statusCode] = new Reference($response, Response::class);
4749
} else {
4850
$this->_responses[$statusCode] = new Response($response);

0 commit comments

Comments
 (0)