Skip to content

Commit ac9578b

Browse files
author
Bizley
authored
Merge pull request #8 from bizley/yii-configs-constraints
Allow Yii-style configs
2 parents 9117177 + bb26016 commit ac9578b

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

infection.json.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@default": true,
1515
"MethodCallRemoval": {
1616
"ignore": [
17-
"bizley\\jwt\\Jwt::init::184",
17+
"bizley\\jwt\\Jwt::init::187",
1818
"bizley\\jwt\\JwtHttpBearerAuth::init::68"
1919
]
2020
}

src/Jwt.php

+27-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
use yii\base\InvalidConfigException;
2121
use yii\di\Instance;
2222

23+
use function array_keys;
2324
use function count;
2425
use function in_array;
2526
use function is_array;
2627
use function is_string;
28+
use function reset;
2729
use 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
}

tests/ConstraintsConfigTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace bizley\tests;
66

77
use bizley\jwt\Jwt;
8+
use bizley\tests\stubs\YiiConstraint;
89
use Closure;
910
use Lcobucci\JWT\Token;
1011
use Lcobucci\JWT\Validation\Constraint;
@@ -53,6 +54,13 @@ public function testArrayConfigWithArray(): void
5354
self::assertTrue($jwt->validate($this->getToken($jwt)));
5455
}
5556

57+
public function testArrayConfigWithYiiArray(): void
58+
{
59+
$jwt = $this->getJwt([['class' => YiiConstraint::class, 'test' => 'yii']]);
60+
61+
self::assertTrue($jwt->validate($this->getToken($jwt)));
62+
}
63+
5664
public function testArrayConfigWithClosure(): void
5765
{
5866
$jwt = $this->getJwt(static function (Jwt $jwt) {

tests/stubs/YiiConstraint.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace bizley\tests\stubs;
6+
7+
use Lcobucci\JWT\Token;
8+
use Lcobucci\JWT\Validation\Constraint;
9+
use yii\base\BaseObject;
10+
11+
class YiiConstraint extends BaseObject implements Constraint
12+
{
13+
public string $test;
14+
15+
public function assert(Token $token): void
16+
{
17+
}
18+
}

0 commit comments

Comments
 (0)