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