Skip to content

Commit

Permalink
Merge pull request #8 from bizley/yii-configs-constraints
Browse files Browse the repository at this point in the history
Allow Yii-style configs
  • Loading branch information
Bizley authored May 5, 2021
2 parents 9117177 + bb26016 commit ac9578b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@default": true,
"MethodCallRemoval": {
"ignore": [
"bizley\\jwt\\Jwt::init::184",
"bizley\\jwt\\Jwt::init::187",
"bizley\\jwt\\JwtHttpBearerAuth::init::68"
]
}
Expand Down
35 changes: 27 additions & 8 deletions src/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
use yii\base\InvalidConfigException;
use yii\di\Instance;

use function array_keys;
use function count;
use function in_array;
use function is_array;
use function is_string;
use function reset;
use function strpos;

/**
Expand Down Expand Up @@ -104,15 +106,16 @@ class Jwt extends Component

/**
* @var string|Signer|null Signer ID or Signer instance to be used for signing/verifying.
* See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you want
* to do some testing but it's NOT recommended for production environments.
* See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you
* want to do some testing but it's NOT recommended for production environments.
* @since 3.0.0
*/
public $signer;

/**
* @var array<string, array<mixed>> Default signers configuration. When instantiated it will use selected array to spread into
* `Yii::createObject($type, array $params = [])` method so the first array element is $type, and the second is $params.
* @var array<string, array<mixed>> Default signers configuration. When instantiated it will use selected array to
* spread into `Yii::createObject($type, array $params = [])` method so the first array element is $type, and
* the second is $params.
* Since 3.0.0 configuration is done using arrays.
* @since 2.0.0
*/
Expand Down Expand Up @@ -165,8 +168,8 @@ class Jwt extends Component
public $decoder;

/**
* @var array<array<mixed>>|Validation\Constraint[]|Closure|null List of constraints that will be used to validate against or
* an anonymous function that can be resolved as such list. The signature of the function should be
* @var array<array<mixed>>|Validation\Constraint[]|Closure|null List of constraints that will be used to validate
* against or an anonymous function that can be resolved as such list. The signature of the function should be
* `function(\bizley\jwt\Jwt $jwt)` where $jwt will be an instance of this component.
* For the constraints you can use instances of Lcobucci\JWT\Validation\Constraint or configuration arrays to be
* resolved as such.
Expand Down Expand Up @@ -211,6 +214,22 @@ public function init(): void
}
}

/**
* @param array<mixed> $config
* @return object
* @throws InvalidConfigException
*/
private function buildObjectFromArray(array $config): object
{
$keys = array_keys($config);
if (is_string(reset($keys))) {
// most probably Yii-style config
return Yii::createObject($config);
}

return Yii::createObject(...$config);
}

/**
* @throws InvalidConfigException
* @since 3.0.0
Expand Down Expand Up @@ -380,7 +399,7 @@ private function prepareSigner($signer): Signer
}

/** @var Signer $signerInstance */
$signerInstance = Yii::createObject(...$this->signers[$signer]);
$signerInstance = $this->buildObjectFromArray($this->signers[$signer]);

return $signerInstance;
}
Expand All @@ -404,7 +423,7 @@ private function prepareValidationConstraints(): array
$constraints[] = $constraint;
} else {
/** @var Validation\Constraint $constraintInstance */
$constraintInstance = Yii::createObject(...$constraint);
$constraintInstance = $this->buildObjectFromArray($constraint);
$constraints[] = $constraintInstance;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ConstraintsConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace bizley\tests;

use bizley\jwt\Jwt;
use bizley\tests\stubs\YiiConstraint;
use Closure;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
Expand Down Expand Up @@ -53,6 +54,13 @@ public function testArrayConfigWithArray(): void
self::assertTrue($jwt->validate($this->getToken($jwt)));
}

public function testArrayConfigWithYiiArray(): void
{
$jwt = $this->getJwt([['class' => YiiConstraint::class, 'test' => 'yii']]);

self::assertTrue($jwt->validate($this->getToken($jwt)));
}

public function testArrayConfigWithClosure(): void
{
$jwt = $this->getJwt(static function (Jwt $jwt) {
Expand Down
18 changes: 18 additions & 0 deletions tests/stubs/YiiConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace bizley\tests\stubs;

use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint;
use yii\base\BaseObject;

class YiiConstraint extends BaseObject implements Constraint
{
public string $test;

public function assert(Token $token): void
{
}
}

0 comments on commit ac9578b

Please sign in to comment.