@@ -24,14 +24,17 @@ public function __construct(array $data)
2424 parent ::__construct ($ data );
2525
2626 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 );
3237 }
33- } else { // write
34- $ this ->_securityRequirements [$ index ] = $ value ;
3538 }
3639 }
3740
@@ -64,25 +67,51 @@ protected function performValidation()
6467 public function getSerializableData ()
6568 {
6669 $ data = [];
67- foreach ($ this ->_securityRequirements ?? [] as $ name => $ securityRequirement ) {
68- /** @var SecurityRequirement $securityRequirement */
6970
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 ;
7486 }
7587 }
7688 return $ data ;
7789 }
7890
7991 public function getRequirement (string $ name )
8092 {
81- return $ this ->_securityRequirements [ $ name] ?? null ;
93+ return static :: searchKey ( $ this ->_securityRequirements , $ name) ;
8294 }
8395
8496 public function getRequirements ()
8597 {
8698 return $ this ->_securityRequirements ;
8799 }
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+ }
88117}
0 commit comments