-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathSecurityRequirements.php
More file actions
116 lines (101 loc) · 3.53 KB
/
SecurityRequirements.php
File metadata and controls
116 lines (101 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
*/
namespace cebe\openapi\spec;
use cebe\openapi\SpecBaseObject;
/**
* Lists the required security schemes to execute this operation.
*
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#securityRequirementObject
*
*/
class SecurityRequirements extends SpecBaseObject
{
private $_securityRequirements;
public function __construct(array $data)
{
parent::__construct($data);
foreach ($data as $index => $value) {
if (is_numeric($index) && $value === []) { # empty Security Requirement Object (`{}`) = anonymous access
$this->_securityRequirements[$index] = [];
continue;
}
if (is_string($index)) {
$this->_securityRequirements[] = [$index => $value instanceof SecurityRequirement ? $value : new SecurityRequirement($value)];
} elseif (is_numeric($index)) {
foreach ($value as $innerIndex => $subValue) {
$this->_securityRequirements[$index][$innerIndex] = $subValue instanceof SecurityRequirement ? $subValue : new SecurityRequirement($subValue);
}
}
}
if ($data === []) {
$this->_securityRequirements = [];
}
}
/**
* @return array array of attributes available in this object.
*/
protected function attributes(): array
{
// this object does not have a fixed set of attribute names
return [];
}
/**
* Perform validation on this object, check data against OpenAPI Specification rules.
*
* Call `addError()` in case of validation errors.
*/
protected function performValidation()
{
}
/**
* {@inheritDoc}
*/
public function getSerializableData()
{
$data = [];
foreach ($this->_securityRequirements ?? [] as $outerIndex => $content) {
if (is_string($outerIndex)) {
$data[] = [$outerIndex => $content->getSerializableData()];
} elseif (is_numeric($outerIndex)) {
if ($content === []) {
$data[$outerIndex] = (object)$content;
continue;
}
$innerResult = [];
foreach ($content as $innerIndex => $innerContent) {
$result = is_object($innerContent) && method_exists($innerContent, 'getSerializableData') ? $innerContent->getSerializableData() : $innerContent;
$innerResult[$innerIndex] = $result;
}
$data[$outerIndex] = (object)$innerResult;
}
}
return $data;
}
public function getRequirement(string $name)
{
return static::searchKey($this->_securityRequirements, $name);
}
public function getRequirements()
{
return $this->_securityRequirements;
}
private static function searchKey(array $array, string $searchKey)
{
foreach ($array as $key => $value) {
if ($key === $searchKey) {
return $value;
}
if (is_array($value)) {
$mt = __METHOD__;
$result = $mt($value, $searchKey);
if ($result !== null) {
return $result; // key found in deeply nested/associative array
}
}
}
return null; // key not found
}
}