1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\JWT; |
4
|
|
|
|
5
|
|
|
use Zenstruck\JWT\Validator\AudienceValidator; |
6
|
|
|
use Zenstruck\JWT\Validator\ChainValidator; |
7
|
|
|
use Zenstruck\JWT\Validator\ExpectedClaimValidator; |
8
|
|
|
use Zenstruck\JWT\Validator\ExpiresAtValidator; |
9
|
|
|
use Zenstruck\JWT\Validator\HasClaimValidator; |
10
|
|
|
use Zenstruck\JWT\Validator\IdValidator; |
11
|
|
|
use Zenstruck\JWT\Validator\IssuedAtValidator; |
12
|
|
|
use Zenstruck\JWT\Validator\IssuerValidator; |
13
|
|
|
use Zenstruck\JWT\Validator\NotBeforeValidator; |
14
|
|
|
use Zenstruck\JWT\Validator\NullValidator; |
15
|
|
|
use Zenstruck\JWT\Validator\SubjectValidator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Kevin Bond <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class ValidatorBuilder |
21
|
|
|
{ |
22
|
|
|
/** @var Validator[] */ |
23
|
|
|
private $validators = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return Validator |
27
|
|
|
*/ |
28
|
3 |
|
public function create() |
29
|
|
|
{ |
30
|
3 |
|
switch (count($this->validators)) { |
31
|
3 |
|
case 0: |
32
|
1 |
|
return new NullValidator(); |
33
|
|
|
|
34
|
2 |
|
case 1: |
35
|
1 |
|
return $this->validators[0]; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
return new ChainValidator($this->validators); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Validator $validator |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
2 |
|
public function add(Validator $validator) |
47
|
|
|
{ |
48
|
2 |
|
$this->validators[] = $validator; |
49
|
|
|
|
50
|
2 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $claim |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
1 |
|
public function has($claim) |
59
|
|
|
{ |
60
|
1 |
|
return $this->add(new HasClaimValidator($claim)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $claim |
65
|
|
|
* @param mixed $expected |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
1 |
|
public function expect($claim, $expected) |
70
|
|
|
{ |
71
|
1 |
|
return $this->add(new ExpectedClaimValidator($claim, $expected)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $expected |
76
|
|
|
* |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
1 |
|
public function issuedAt($expected) |
80
|
|
|
{ |
81
|
1 |
|
return $this->add(new IssuedAtValidator($expected)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $expected |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
1 |
|
public function subject($expected) |
90
|
|
|
{ |
91
|
1 |
|
return $this->add(new SubjectValidator($expected)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param mixed $expected |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
1 |
|
public function audience($expected) |
100
|
|
|
{ |
101
|
1 |
|
return $this->add(new AudienceValidator($expected)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param mixed $expected |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
1 |
|
public function issuer($expected) |
110
|
|
|
{ |
111
|
1 |
|
return $this->add(new IssuerValidator($expected)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $expected |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
1 |
|
public function id($expected) |
120
|
|
|
{ |
121
|
1 |
|
return $this->add(new IdValidator($expected)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param \DateTime|null $currentTime |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
2 |
|
public function expiresAt(\DateTime $currentTime = null) |
130
|
|
|
{ |
131
|
2 |
|
return $this->add(new ExpiresAtValidator($currentTime)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \DateTime|null $currentTime |
136
|
|
|
* |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
1 |
|
public function notBefore(\DateTime $currentTime = null) |
140
|
|
|
{ |
141
|
1 |
|
return $this->add(new NotBeforeValidator($currentTime)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|