@@ -24,21 +24,29 @@ final class Configuration
2424 private Closure $ builderFactory ;
2525
2626 /** @var Constraint[] */
27- private array $ validationConstraints = [] ;
27+ private array $ validationConstraints ;
2828
29+ /** @param Closure(ClaimsFormatter $claimFormatter): Builder|null $builderFactory */
2930 private function __construct (
3031 private readonly Signer $ signer ,
3132 private readonly Key $ signingKey ,
3233 private readonly Key $ verificationKey ,
33- Encoder $ encoder ,
34- Decoder $ decoder ,
34+ private readonly Encoder $ encoder ,
35+ private readonly Decoder $ decoder ,
36+ ?Parser $ parser ,
37+ ?Validator $ validator ,
38+ ?Closure $ builderFactory ,
39+ Constraint ...$ validationConstraints ,
3540 ) {
36- $ this ->parser = new Token \Parser ($ decoder );
37- $ this ->validator = new Validation \Validator ();
41+ $ this ->parser = $ parser ?? new Token \Parser ($ decoder );
42+ $ this ->validator = $ validator ?? new Validation \Validator ();
3843
39- $ this ->builderFactory = static function (ClaimsFormatter $ claimFormatter ) use ($ encoder ): Builder {
40- return new Token \Builder ($ encoder , $ claimFormatter );
41- };
44+ $ this ->builderFactory = $ builderFactory
45+ ?? static function (ClaimsFormatter $ claimFormatter ) use ($ encoder ): Builder {
46+ return Token \Builder::new ($ encoder , $ claimFormatter );
47+ };
48+
49+ $ this ->validationConstraints = $ validationConstraints ;
4250 }
4351
4452 public static function forAsymmetricSigner (
@@ -54,6 +62,9 @@ public static function forAsymmetricSigner(
5462 $ verificationKey ,
5563 $ encoder ,
5664 $ decoder ,
65+ null ,
66+ null ,
67+ null ,
5768 );
5869 }
5970
@@ -69,15 +80,38 @@ public static function forSymmetricSigner(
6980 $ key ,
7081 $ encoder ,
7182 $ decoder ,
83+ null ,
84+ null ,
85+ null ,
7286 );
7387 }
7488
75- /** @param callable(ClaimsFormatter): Builder $builderFactory */
89+ /**
90+ * @deprecated Deprecated since v5.5, please use {@see self::withBuilderFactory()} instead
91+ *
92+ * @param callable(ClaimsFormatter): Builder $builderFactory
93+ */
7694 public function setBuilderFactory (callable $ builderFactory ): void
7795 {
7896 $ this ->builderFactory = $ builderFactory (...);
7997 }
8098
99+ /** @param callable(ClaimsFormatter): Builder $builderFactory */
100+ public function withBuilderFactory (callable $ builderFactory ): self
101+ {
102+ return new self (
103+ $ this ->signer ,
104+ $ this ->signingKey ,
105+ $ this ->verificationKey ,
106+ $ this ->encoder ,
107+ $ this ->decoder ,
108+ $ this ->parser ,
109+ $ this ->validator ,
110+ $ builderFactory (...),
111+ ...$ this ->validationConstraints ,
112+ );
113+ }
114+
81115 public function builder (?ClaimsFormatter $ claimFormatter = null ): Builder
82116 {
83117 return ($ this ->builderFactory )($ claimFormatter ?? ChainedFormatter::default ());
@@ -88,11 +122,27 @@ public function parser(): Parser
88122 return $ this ->parser ;
89123 }
90124
125+ /** @deprecated Deprecated since v5.5, please use {@see self::withParser()} instead */
91126 public function setParser (Parser $ parser ): void
92127 {
93128 $ this ->parser = $ parser ;
94129 }
95130
131+ public function withParser (Parser $ parser ): self
132+ {
133+ return new self (
134+ $ this ->signer ,
135+ $ this ->signingKey ,
136+ $ this ->verificationKey ,
137+ $ this ->encoder ,
138+ $ this ->decoder ,
139+ $ parser ,
140+ $ this ->validator ,
141+ $ this ->builderFactory ,
142+ ...$ this ->validationConstraints ,
143+ );
144+ }
145+
96146 public function signer (): Signer
97147 {
98148 return $ this ->signer ;
@@ -113,19 +163,51 @@ public function validator(): Validator
113163 return $ this ->validator ;
114164 }
115165
166+ /** @deprecated Deprecated since v5.5, please use {@see self::withValidator()} instead */
116167 public function setValidator (Validator $ validator ): void
117168 {
118169 $ this ->validator = $ validator ;
119170 }
120171
172+ public function withValidator (Validator $ validator ): self
173+ {
174+ return new self (
175+ $ this ->signer ,
176+ $ this ->signingKey ,
177+ $ this ->verificationKey ,
178+ $ this ->encoder ,
179+ $ this ->decoder ,
180+ $ this ->parser ,
181+ $ validator ,
182+ $ this ->builderFactory ,
183+ ...$ this ->validationConstraints ,
184+ );
185+ }
186+
121187 /** @return Constraint[] */
122188 public function validationConstraints (): array
123189 {
124190 return $ this ->validationConstraints ;
125191 }
126192
193+ /** @deprecated Deprecated since v5.5, please use {@see self::withValidationConstraints()} instead */
127194 public function setValidationConstraints (Constraint ...$ validationConstraints ): void
128195 {
129196 $ this ->validationConstraints = $ validationConstraints ;
130197 }
198+
199+ public function withValidationConstraints (Constraint ...$ validationConstraints ): self
200+ {
201+ return new self (
202+ $ this ->signer ,
203+ $ this ->signingKey ,
204+ $ this ->verificationKey ,
205+ $ this ->encoder ,
206+ $ this ->decoder ,
207+ $ this ->parser ,
208+ $ this ->validator ,
209+ $ this ->builderFactory ,
210+ ...$ validationConstraints ,
211+ );
212+ }
131213}
0 commit comments