Skip to content

Commit b346ddd

Browse files
Add IsEqualChecker and CallableChecker (#462)
* Add IsEqualChecker * Add CallableChecker
1 parent aef1302 commit b346ddd

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
use function call_user_func;
8+
use InvalidArgumentException;
9+
use function is_callable;
10+
11+
/**
12+
* @see \Jose\Tests\Component\Checker\CallableCheckerTest
13+
*/
14+
final class CallableChecker implements ClaimChecker, HeaderChecker
15+
{
16+
/**
17+
* @param string $key The claim or header parameter name to check.
18+
* @param callable(mixed $value): bool $callable The callable function that will be invoked.
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)) { // @phpstan-ignore-line
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Component\Checker;
6+
7+
/**
8+
* @see \Jose\Tests\Component\Checker\IsEqualCheckerTest
9+
*/
10+
final class IsEqualChecker implements ClaimChecker, HeaderChecker
11+
{
12+
/**
13+
* @param string $key The claim or header parameter name to check.
14+
* @param mixed $value The expected value.
15+
* @param bool $protectedHeaderOnly [optional] Whether the header parameter MUST be protected.
16+
* This option has no effect for claim checkers.
17+
*/
18+
public function __construct(
19+
private readonly string $key,
20+
private readonly mixed $value,
21+
private readonly bool $protectedHeaderOnly = true
22+
) {
23+
}
24+
25+
public function checkClaim(mixed $value): void
26+
{
27+
if ($value !== $this->value) {
28+
throw new InvalidClaimException(sprintf('The "%s" claim is invalid.', $this->key), $this->key, $value);
29+
}
30+
}
31+
32+
public function supportedClaim(): string
33+
{
34+
return $this->key;
35+
}
36+
37+
public function checkHeader(mixed $value): void
38+
{
39+
if ($value !== $this->value) {
40+
throw new InvalidHeaderException(sprintf('The "%s" header is invalid.', $this->key), $this->key, $value);
41+
}
42+
}
43+
44+
public function supportedHeader(): string
45+
{
46+
return $this->key;
47+
}
48+
49+
public function protectedHeaderOnly(): bool
50+
{
51+
return $this->protectedHeaderOnly;
52+
}
53+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Tests\Component\Checker;
6+
7+
use InvalidArgumentException;
8+
use Jose\Component\Checker\CallableChecker;
9+
use Jose\Component\Checker\InvalidClaimException;
10+
use Jose\Component\Checker\InvalidHeaderException;
11+
use PHPUnit\Framework\TestCase;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class CallableCheckerTest extends TestCase
17+
{
18+
/**
19+
* @test
20+
*/
21+
public function theCallableIsCallable(): void
22+
{
23+
$this->expectException(InvalidArgumentException::class);
24+
$this->expectExceptionMessage('The $callable argument must be a callable.');
25+
26+
new CallableChecker('foo', 'not_a_callable');
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function theCallableDoesNotReturnABoolean(): void
33+
{
34+
$this->expectException(InvalidClaimException::class);
35+
36+
$checker = new CallableChecker('foo', fn (mixed $value) => 1);
37+
$checker->checkClaim('baz');
38+
39+
$this->expectException(InvalidHeaderException::class);
40+
41+
$checker = new CallableChecker('foo', fn (mixed $value) => 0);
42+
$checker->checkHeader('baz');
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function theClaimIsInvalid(): void
49+
{
50+
$this->expectException(InvalidClaimException::class);
51+
52+
$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
53+
$checker->checkClaim('baz');
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function theHeaderIsInvalid(): void
60+
{
61+
$this->expectException(InvalidHeaderException::class);
62+
63+
$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
64+
$checker->checkHeader('baz');
65+
}
66+
67+
/**
68+
* @test
69+
*/
70+
public function theClaimIsSupported(): void
71+
{
72+
$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
73+
$checker->checkClaim('bar');
74+
75+
static::assertSame('foo', $checker->supportedClaim());
76+
}
77+
78+
/**
79+
* @test
80+
*/
81+
public function theHeaderIsSupported(): void
82+
{
83+
$checker = new CallableChecker('foo', fn (mixed $value) => $value === 'bar');
84+
$checker->checkHeader('bar');
85+
86+
static::assertSame('foo', $checker->supportedHeader());
87+
}
88+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Tests\Component\Checker;
6+
7+
use Jose\Component\Checker\InvalidClaimException;
8+
use Jose\Component\Checker\InvalidHeaderException;
9+
use Jose\Component\Checker\IsEqualChecker;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* @internal
14+
*/
15+
final class IsEqualCheckerTest extends TestCase
16+
{
17+
/**
18+
* @test
19+
*/
20+
public function theClaimIsInvalid(): void
21+
{
22+
$this->expectException(InvalidClaimException::class);
23+
24+
$checker = new IsEqualChecker('foo', 'bar');
25+
$checker->checkClaim('baz');
26+
}
27+
28+
/**
29+
* @test
30+
*/
31+
public function theHeaderIsInvalid(): void
32+
{
33+
$this->expectException(InvalidHeaderException::class);
34+
35+
$checker = new IsEqualChecker('foo', 'bar');
36+
$checker->checkHeader('baz');
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function theClaimIsSupported(): void
43+
{
44+
$checker = new IsEqualChecker('foo', 'bar');
45+
$checker->checkClaim('bar');
46+
static::assertSame('foo', $checker->supportedClaim());
47+
}
48+
49+
/**
50+
* @test
51+
*/
52+
public function theHeaderIsSupported(): void
53+
{
54+
$checker = new IsEqualChecker('foo', 'bar');
55+
$checker->checkHeader('bar');
56+
static::assertSame('foo', $checker->supportedHeader());
57+
}
58+
}

0 commit comments

Comments
 (0)