2020use yii \base \InvalidConfigException ;
2121use yii \di \Instance ;
2222
23+ use function array_keys ;
2324use function count ;
2425use function in_array ;
2526use function is_array ;
2627use function is_string ;
28+ use function reset ;
2729use function strpos ;
2830
2931/**
@@ -104,15 +106,16 @@ class Jwt extends Component
104106
105107 /**
106108 * @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.
109111 * @since 3.0.0
110112 */
111113 public $ signer ;
112114
113115 /**
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.
116119 * Since 3.0.0 configuration is done using arrays.
117120 * @since 2.0.0
118121 */
@@ -165,8 +168,8 @@ class Jwt extends Component
165168 public $ decoder ;
166169
167170 /**
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
170173 * `function(\bizley\jwt\Jwt $jwt)` where $jwt will be an instance of this component.
171174 * For the constraints you can use instances of Lcobucci\JWT\Validation\Constraint or configuration arrays to be
172175 * resolved as such.
@@ -211,6 +214,22 @@ public function init(): void
211214 }
212215 }
213216
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+
214233 /**
215234 * @throws InvalidConfigException
216235 * @since 3.0.0
@@ -380,7 +399,7 @@ private function prepareSigner($signer): Signer
380399 }
381400
382401 /** @var Signer $signerInstance */
383- $ signerInstance = Yii:: createObject (... $ this ->signers [$ signer ]);
402+ $ signerInstance = $ this -> buildObjectFromArray ( $ this ->signers [$ signer ]);
384403
385404 return $ signerInstance ;
386405 }
@@ -404,7 +423,7 @@ private function prepareValidationConstraints(): array
404423 $ constraints [] = $ constraint ;
405424 } else {
406425 /** @var Validation\Constraint $constraintInstance */
407- $ constraintInstance = Yii:: createObject (... $ constraint );
426+ $ constraintInstance = $ this -> buildObjectFromArray ( $ constraint );
408427 $ constraints [] = $ constraintInstance ;
409428 }
410429 }
0 commit comments