| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Checker; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use function call_user_func; |
| 9 |
use function is_callable; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
final class CallableChecker implements ClaimChecker, HeaderChecker |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function __construct( |
| 21 |
private readonly string $key, |
| 22 |
private $callable, |
| 23 |
private readonly bool $protectedHeaderOnly = true |
| 24 |
) { |
| 25 |
if (! is_callable($this->callable)) { |
| 26 |
throw new InvalidArgumentException('The $callable argument must be a callable.'); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public function checkClaim(mixed $value): void |
| 31 |
{ |
| 32 |
if (call_user_func($this->callable, $value) !== true) { |
| 33 |
throw new InvalidClaimException(sprintf('The "%s" claim is invalid.', $this->key), $this->key, $value); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function supportedClaim(): string |
| 38 |
{ |
| 39 |
return $this->key; |
| 40 |
} |
| 41 |
|
| 42 |
public function checkHeader(mixed $value): void |
| 43 |
{ |
| 44 |
if (call_user_func($this->callable, $value) !== true) { |
| 45 |
throw new InvalidHeaderException(sprintf('The "%s" header is invalid.', $this->key), $this->key, $value); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function supportedHeader(): string |
| 50 |
{ |
| 51 |
return $this->key; |
| 52 |
} |
| 53 |
|
| 54 |
public function protectedHeaderOnly(): bool |
| 55 |
{ |
| 56 |
return $this->protectedHeaderOnly; |
| 57 |
} |
| 58 |
} |