Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cdcc4e

Browse files
committedOct 11, 2022
Add convenience functions for extension properties
1 parent 020d72b commit 2cdcc4e

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed
 

Diff for: ‎src/SpecBaseObject.php

+33
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,37 @@ public function getExtensions(): array
525525
}
526526
return $extensions;
527527
}
528+
529+
/**
530+
* Returns extension property with `x-` prefix.
531+
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
532+
* @param string $key The property key, e.g. 'x-prop' or 'prop' (the 'x-' prefix is added automatically if needed).
533+
* @return mixed|null The property value, if present.
534+
* @since 1.8.0
535+
*/
536+
public function getExtension(string $key)
537+
{
538+
if (strpos($key, 'x-') !== 0) {
539+
$key = 'x-' . $key;
540+
}
541+
542+
return $this->_properties[$key] ?? null;
543+
}
544+
545+
/**
546+
* Set extension property with `x-` prefix.
547+
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
548+
* @param string $key The property key, e.g. 'x-prop' or 'prop' (the 'x-' prefix is added automatically if needed).
549+
* @param mixed $value The property value.
550+
* @return void
551+
* @since 1.8.0
552+
*/
553+
public function setExtension(string $key, $value): void
554+
{
555+
if (strpos($key, 'x-') !== 0) {
556+
$key = 'x-' . $key;
557+
}
558+
559+
$this->_properties[$key] = $value;
560+
}
528561
}

Diff for: ‎tests/WriterTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ public function testWriteJson()
3737
);
3838
}
3939

40+
public function testWriteJsonExtensions()
41+
{
42+
$openapi = $this->createOpenAPI();
43+
$openapi->{'x-extra-var'} = 'foo';
44+
$openapi->setExtension('x-another-var', 'bar');
45+
$openapi->setExtension('short-var', 'baz');
46+
47+
$json = \cebe\openapi\Writer::writeToJson($openapi);
48+
49+
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
50+
{
51+
"openapi": "3.0.0",
52+
"info": {
53+
"title": "Test API",
54+
"version": "1.0.0"
55+
},
56+
"paths": {},
57+
"x-extra-var": "foo",
58+
"x-another-var": "bar",
59+
"x-short-var": "baz"
60+
}
61+
JSON
62+
),
63+
$json
64+
);
65+
}
66+
4067
public function testWriteJsonMofify()
4168
{
4269
$openapi = $this->createOpenAPI();

Diff for: ‎tests/spec/InfoTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testRead()
2727
name: Apache 2.0
2828
url: https://www.apache.org/licenses/LICENSE-2.0.html
2929
version: 1.0.1
30+
x-extra-var: foo
3031
YAML
3132
, Info::class);
3233

@@ -46,6 +47,11 @@ public function testRead()
4647
$this->assertInstanceOf(License::class, $info->license);
4748
$this->assertEquals('Apache 2.0', $info->license->name);
4849
$this->assertEquals('https://www.apache.org/licenses/LICENSE-2.0.html', $info->license->url);
50+
51+
$this->assertEquals('foo', $info->getExtensions()['x-extra-var']);
52+
$this->assertEquals('foo', $info->getExtension('x-extra-var'));
53+
$this->assertEquals('foo', $info->getExtension('extra-var'));
54+
$this->assertNull($info->getExtension('another-var'));
4955
}
5056

5157
public function testReadInvalid()
@@ -117,4 +123,4 @@ public function testReadInvalidLicense()
117123
$this->assertNull($info->contact);
118124

119125
}
120-
}
126+
}

0 commit comments

Comments
 (0)
Please sign in to comment.