@@ -24,14 +24,17 @@ public function __construct(array $data)
24
24
parent ::__construct ($ data );
25
25
26
26
foreach ($ data as $ index => $ value ) {
27
- if (is_numeric ($ index )) { // read
28
- if ($ value === []) { # empty Security Requirement Object (`{}`) = anonymous access https://github.com/cebe/php-openapi/issues/238
29
- $ this ->_securityRequirements [$ index ] = $ value ;
30
- } else {
31
- $ this ->_securityRequirements [array_keys ($ value )[0 ]] = new SecurityRequirement (array_values ($ value )[0 ]);
27
+ if (is_numeric ($ index ) && $ value === []) { # empty Security Requirement Object (`{}`) = anonymous access
28
+ $ this ->_securityRequirements [$ index ] = [];
29
+ continue ;
30
+ }
31
+
32
+ if (is_string ($ index )) {
33
+ $ this ->_securityRequirements [] = [$ index => $ value instanceof SecurityRequirement ? $ value : new SecurityRequirement ($ value )];
34
+ } elseif (is_numeric ($ index )) {
35
+ foreach ($ value as $ innerIndex => $ subValue ) {
36
+ $ this ->_securityRequirements [$ index ][$ innerIndex ] = $ subValue instanceof SecurityRequirement ? $ subValue : new SecurityRequirement ($ subValue );
32
37
}
33
- } else { // write
34
- $ this ->_securityRequirements [$ index ] = $ value ;
35
38
}
36
39
}
37
40
@@ -64,25 +67,51 @@ protected function performValidation()
64
67
public function getSerializableData ()
65
68
{
66
69
$ data = [];
67
- foreach ($ this ->_securityRequirements ?? [] as $ name => $ securityRequirement ) {
68
- /** @var SecurityRequirement $securityRequirement */
69
70
70
- if (is_numeric ($ name )) {
71
- $ data [$ name ] = (object ) $ securityRequirement ; # case https://github.com/cebe/php-openapi/issues/238
72
- } else {
73
- $ data [] = (object ) [$ name => $ securityRequirement ->getSerializableData ()];
71
+ foreach ($ this ->_securityRequirements ?? [] as $ outerIndex => $ content ) {
72
+
73
+ if (is_string ($ outerIndex )) {
74
+ $ data [] = [$ outerIndex => $ content ->getSerializableData ()];
75
+ } elseif (is_numeric ($ outerIndex )) {
76
+ if ($ content === []) {
77
+ $ data [$ outerIndex ] = (object )$ content ;
78
+ continue ;
79
+ }
80
+ $ innerResult = [];
81
+ foreach ($ content as $ innerIndex => $ innerContent ) {
82
+ $ result = is_object ($ innerContent ) && method_exists ($ innerContent , 'getSerializableData ' ) ? $ innerContent ->getSerializableData () : $ innerContent ;
83
+ $ innerResult [$ innerIndex ] = $ result ;
84
+ }
85
+ $ data [$ outerIndex ] = (object )$ innerResult ;
74
86
}
75
87
}
76
88
return $ data ;
77
89
}
78
90
79
91
public function getRequirement (string $ name )
80
92
{
81
- return $ this ->_securityRequirements [ $ name] ?? null ;
93
+ return static :: searchKey ( $ this ->_securityRequirements , $ name) ;
82
94
}
83
95
84
96
public function getRequirements ()
85
97
{
86
98
return $ this ->_securityRequirements ;
87
99
}
100
+
101
+ private static function searchKey (array $ array , string $ searchKey )
102
+ {
103
+ foreach ($ array as $ key => $ value ) {
104
+ if ($ key === $ searchKey ) {
105
+ return $ value ;
106
+ }
107
+ if (is_array ($ value )) {
108
+ $ mt = __METHOD__ ;
109
+ $ result = $ mt ($ value , $ searchKey );
110
+ if ($ result !== null ) {
111
+ return $ result ; // key found in deeply nested/associative array
112
+ }
113
+ }
114
+ }
115
+ return null ; // key not found
116
+ }
88
117
}
0 commit comments