Skip to content

Commit 29f5bbb

Browse files
committed
Change design of SecurityScheme
1 parent b7c0a6d commit 29f5bbb

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

src/spec/SecurityScheme.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,22 @@
2525
*/
2626
class SecurityScheme extends SpecBaseObject
2727
{
28+
public $name;
29+
public $scheme;
2830
private $knownTypes = [
2931
"apiKey",
3032
"http",
3133
"oauth2",
3234
"openIdConnect"
3335
];
3436

37+
public function __construct(array $data)
38+
{
39+
parent::__construct($data);
40+
$this->name = array_keys($data)[0];
41+
$this->scheme = array_values($data)[0];
42+
}
43+
3544
/**
3645
* @return array array of attributes available in this object.
3746
*/

src/spec/SecuritySchemes.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
5+
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
6+
*/
7+
8+
namespace cebe\openapi\spec;
9+
10+
use cebe\openapi\SpecBaseObject;
11+
12+
class SecuritySchemes extends SpecBaseObject
13+
{
14+
private $_securitySchemes = [];
15+
16+
public function __construct(array $data)
17+
{
18+
parent::__construct($data);
19+
$this->_securitySchemes = $data;
20+
}
21+
22+
protected function attributes(): array
23+
{
24+
return [];
25+
}
26+
27+
public function performValidation()
28+
{
29+
}
30+
31+
/**
32+
* @return mixed returns the serializable data of this object for converting it
33+
* to JSON or YAML.
34+
*/
35+
public function getSerializableData()
36+
{
37+
$data = [];
38+
foreach ($this->_securitySchemes as $securityScheme) {
39+
/** @var $securityScheme SecurityScheme */
40+
$data[$securityScheme->name] = $securityScheme->scheme;
41+
}
42+
return (object) $data;
43+
}
44+
}

tests/WriterTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?php
22

33
use cebe\openapi\spec\Components;
4-
use cebe\openapi\spec\OpenApi;
54
use cebe\openapi\spec\Operation;
65
use cebe\openapi\spec\PathItem;
76
use cebe\openapi\spec\Response;
87
use cebe\openapi\spec\Responses;
98
use cebe\openapi\spec\SecurityRequirement;
10-
use cebe\openapi\spec\SecurityRequirements;
119
use cebe\openapi\spec\SecurityScheme;
10+
use cebe\openapi\spec\SecuritySchemes;
1211

1312
class WriterTest extends \PHPUnit\Framework\TestCase
1413
{
@@ -200,13 +199,15 @@ public function testSecurityAtPathOperationLevel()
200199
{
201200
$openapi = $this->createOpenAPI([
202201
'components' => new Components([
203-
'securitySchemes' => [
204-
'BearerAuth' => new SecurityScheme([
205-
'type' => 'http',
206-
'scheme' => 'bearer',
207-
'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes
202+
'securitySchemes' => new SecuritySchemes([
203+
new SecurityScheme([
204+
'BearerAuth' => [
205+
'type' => 'http',
206+
'scheme' => 'bearer',
207+
'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes
208+
]
208209
])
209-
],
210+
]),
210211
]),
211212
'paths' => [
212213
'/test' => new PathItem([

0 commit comments

Comments
 (0)