Completed
Push — master ( e214c2...9b8774 )
by Kevin
02:27
created

ExpectedClaimValidator::claim()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Zenstruck\JWT\Validator;
4
5
use Zenstruck\JWT\Exception\Validation\ClaimMismatch;
6
use Zenstruck\JWT\Exception\Validation\MissingClaim;
7
use Zenstruck\JWT\Token;
8
use Zenstruck\JWT\Validator;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
abstract class ExpectedClaimValidator implements Validator
14
{
15
    private $expected;
16
17
    /**
18
     * @param mixed $expected
19
     */
20 20
    public function __construct($expected)
21
    {
22 20
        $this->expected = $expected;
23 20
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 19
    public function validate(Token $token)
29
    {
30 19
        $claim = $this->claim();
31 19
        $actual = $token->get($claim);
32
33 19
        if (null === $actual) {
34 7
            throw new MissingClaim($claim, $token);
35
        }
36
37 12
        if ($this->expected !== $actual) {
38 5
            throw new ClaimMismatch($claim, $actual, $this->expected, $token);
39
        }
40 7
    }
41
42
    /**
43
     * @return string
44
     */
45
    abstract protected function claim();
46
}
47