20
20
use yii \base \InvalidConfigException ;
21
21
use yii \di \Instance ;
22
22
23
+ use function array_keys ;
23
24
use function count ;
24
25
use function in_array ;
25
26
use function is_array ;
26
27
use function is_string ;
28
+ use function reset ;
27
29
use function strpos ;
28
30
29
31
/**
@@ -104,15 +106,16 @@ class Jwt extends Component
104
106
105
107
/**
106
108
* @var string|Signer|null Signer ID or Signer instance to be used for signing/verifying.
107
- * See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you want
108
- * to do some testing but it's NOT recommended for production environments.
109
+ * See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you
110
+ * want to do some testing but it's NOT recommended for production environments.
109
111
* @since 3.0.0
110
112
*/
111
113
public $ signer ;
112
114
113
115
/**
114
- * @var array<string, array<mixed>> Default signers configuration. When instantiated it will use selected array to spread into
115
- * `Yii::createObject($type, array $params = [])` method so the first array element is $type, and the second is $params.
116
+ * @var array<string, array<mixed>> Default signers configuration. When instantiated it will use selected array to
117
+ * spread into `Yii::createObject($type, array $params = [])` method so the first array element is $type, and
118
+ * the second is $params.
116
119
* Since 3.0.0 configuration is done using arrays.
117
120
* @since 2.0.0
118
121
*/
@@ -165,8 +168,8 @@ class Jwt extends Component
165
168
public $ decoder ;
166
169
167
170
/**
168
- * @var array<array<mixed>>|Validation\Constraint[]|Closure|null List of constraints that will be used to validate against or
169
- * an anonymous function that can be resolved as such list. The signature of the function should be
171
+ * @var array<array<mixed>>|Validation\Constraint[]|Closure|null List of constraints that will be used to validate
172
+ * against or an anonymous function that can be resolved as such list. The signature of the function should be
170
173
* `function(\bizley\jwt\Jwt $jwt)` where $jwt will be an instance of this component.
171
174
* For the constraints you can use instances of Lcobucci\JWT\Validation\Constraint or configuration arrays to be
172
175
* resolved as such.
@@ -211,6 +214,22 @@ public function init(): void
211
214
}
212
215
}
213
216
217
+ /**
218
+ * @param array<mixed> $config
219
+ * @return object
220
+ * @throws InvalidConfigException
221
+ */
222
+ private function buildObjectFromArray (array $ config ): object
223
+ {
224
+ $ keys = array_keys ($ config );
225
+ if (is_string (reset ($ keys ))) {
226
+ // most probably Yii-style config
227
+ return Yii::createObject ($ config );
228
+ }
229
+
230
+ return Yii::createObject (...$ config );
231
+ }
232
+
214
233
/**
215
234
* @throws InvalidConfigException
216
235
* @since 3.0.0
@@ -380,7 +399,7 @@ private function prepareSigner($signer): Signer
380
399
}
381
400
382
401
/** @var Signer $signerInstance */
383
- $ signerInstance = Yii:: createObject (... $ this ->signers [$ signer ]);
402
+ $ signerInstance = $ this -> buildObjectFromArray ( $ this ->signers [$ signer ]);
384
403
385
404
return $ signerInstance ;
386
405
}
@@ -404,7 +423,7 @@ private function prepareValidationConstraints(): array
404
423
$ constraints [] = $ constraint ;
405
424
} else {
406
425
/** @var Validation\Constraint $constraintInstance */
407
- $ constraintInstance = Yii:: createObject (... $ constraint );
426
+ $ constraintInstance = $ this -> buildObjectFromArray ( $ constraint );
408
427
$ constraints [] = $ constraintInstance ;
409
428
}
410
429
}
0 commit comments