77use CasParser \Core \Attributes \Api as Property ;
88use CasParser \Core \Concerns \SdkModel ;
99use CasParser \Core \Contracts \BaseModel ;
10- use CasParser \Core \Implementation \Omittable ;
10+ use CasParser \Core \Implementation \Omit ;
1111use Psr \Http \Client \ClientInterface ;
1212use Psr \Http \Message \RequestFactoryInterface ;
1313use Psr \Http \Message \StreamFactoryInterface ;
@@ -48,15 +48,15 @@ final class RequestOptions implements BaseModel
4848 #[Property]
4949 public float $ maxRetryDelay = 8.0 ;
5050
51- /** @var array<string, string|int|list<string|int>|null> $extraHeaders */
52- #[Property]
53- public array $ extraHeaders = [] ;
51+ /** @var array<string, string|int|list<string|int>|null>|null $extraHeaders */
52+ #[Property(optional: true ) ]
53+ public ? array $ extraHeaders ;
5454
55- /** @var array<string, mixed> $extraQueryParams */
56- #[Property]
57- public array $ extraQueryParams = [] ;
55+ /** @var array<string, mixed>|null $extraQueryParams */
56+ #[Property(optional: true ) ]
57+ public ? array $ extraQueryParams ;
5858
59- #[Property]
59+ #[Property(optional: true ) ]
6060 public mixed $ extraBodyParams ;
6161
6262 #[Property(optional: true )]
@@ -71,12 +71,27 @@ final class RequestOptions implements BaseModel
7171 #[Property(optional: true )]
7272 public ?RequestFactoryInterface $ requestFactory ;
7373
74+ public function __construct ()
75+ {
76+ $ this ->initialize ();
77+ }
78+
79+ /**
80+ * @param request_opts|null $options
81+ */
82+ public static function parse (RequestOptions |array |null ...$ options ): self
83+ {
84+ $ parsed = array_map (static fn ($ o ) => $ o instanceof self ? $ o ->toArray () : $ o ?? [], array: $ options );
85+
86+ return self ::with (...array_merge (...$ parsed )); // @phpstan-ignore-line
87+ }
88+
7489 /**
7590 * @param array<string, string|int|list<string|int>|null>|null $extraHeaders
7691 * @param array<string, mixed>|null $extraQueryParams
77- * @param mixed|Omittable $extraBodyParams
92+ * @param mixed|Omit $extraBodyParams
7893 */
79- public function __construct (
94+ public static function with (
8095 ?float $ timeout = null ,
8196 ?int $ maxRetries = null ,
8297 ?float $ initialRetryDelay = null ,
@@ -88,40 +103,117 @@ public function __construct(
88103 ?UriFactoryInterface $ uriFactory = null ,
89104 ?StreamFactoryInterface $ streamFactory = null ,
90105 ?RequestFactoryInterface $ requestFactory = null ,
91- ) {
92- $ this ->initialize ();
106+ ): self {
107+ $ obj = new self ;
108+
109+ null !== $ timeout && $ obj ->timeout = $ timeout ;
110+ null !== $ maxRetries && $ obj ->maxRetries = $ maxRetries ;
111+ null !== $ initialRetryDelay && $ obj ->initialRetryDelay = $ initialRetryDelay ;
112+ null !== $ maxRetryDelay && $ obj ->maxRetryDelay = $ maxRetryDelay ;
113+ null !== $ extraHeaders && $ obj ->extraHeaders = $ extraHeaders ;
114+ null !== $ extraQueryParams && $ obj ->extraQueryParams = $ extraQueryParams ;
115+ omit !== $ extraBodyParams && $ obj ->extraBodyParams = $ extraBodyParams ;
116+ null !== $ transporter && $ obj ->transporter = $ transporter ;
117+ null !== $ uriFactory && $ obj ->uriFactory = $ uriFactory ;
118+ null !== $ streamFactory && $ obj ->streamFactory = $ streamFactory ;
119+ null !== $ requestFactory && $ obj ->requestFactory = $ requestFactory ;
120+
121+ return $ obj ;
122+ }
123+
124+ public function withTimeout (float $ timeout ): self
125+ {
126+ $ obj = clone $ this ;
127+ $ obj ->timeout = $ timeout ;
128+
129+ return $ obj ;
130+ }
131+
132+ public function withMaxRetries (int $ maxRetries ): self
133+ {
134+ $ obj = clone $ this ;
135+ $ obj ->maxRetries = $ maxRetries ;
136+
137+ return $ obj ;
138+ }
139+
140+ public function withInitialRetryDelay (float $ initialRetryDelay ): self
141+ {
142+ $ obj = clone $ this ;
143+ $ obj ->initialRetryDelay = $ initialRetryDelay ;
93144
94- null !== $ timeout && $ this ->timeout = $ timeout ;
95- null !== $ maxRetries && $ this ->maxRetries = $ maxRetries ;
96- null !== $ initialRetryDelay && $ this
97- ->initialRetryDelay = $ initialRetryDelay
98- ;
99- null !== $ maxRetryDelay && $ this ->maxRetryDelay = $ maxRetryDelay ;
100- null !== $ extraHeaders && $ this ->extraHeaders = $ extraHeaders ;
101- null !== $ extraQueryParams && $ this ->extraQueryParams = $ extraQueryParams ;
102- omit !== $ extraBodyParams && $ this ->extraBodyParams = $ extraBodyParams ;
103- null !== $ transporter && $ this ->transporter = $ transporter ;
104- null !== $ uriFactory && $ this ->uriFactory = $ uriFactory ;
105- null !== $ streamFactory && $ this ->streamFactory = $ streamFactory ;
106- null !== $ requestFactory && $ this ->requestFactory = $ requestFactory ;
145+ return $ obj ;
146+ }
147+
148+ public function withMaxRetryDelay (float $ maxRetryDelay ): self
149+ {
150+ $ obj = clone $ this ;
151+ $ obj ->maxRetryDelay = $ maxRetryDelay ;
152+
153+ return $ obj ;
107154 }
108155
109156 /**
110- * @param request_opts|null $options
157+ * @param array<string, string|int|list<string|int>|null> $extraHeaders
158+ */
159+ public function withExtraHeaders (array $ extraHeaders ): self
160+ {
161+ $ obj = clone $ this ;
162+ $ obj ->extraHeaders = $ extraHeaders ;
163+
164+ return $ obj ;
165+ }
166+
167+ /**
168+ * @param array<string, mixed> $extraQueryParams
111169 */
112- public static function parse (RequestOptions |array |null $ options ): self
170+ public function withExtraQueryParams (array $ extraQueryParams ): self
171+ {
172+ $ obj = clone $ this ;
173+ $ obj ->extraQueryParams = $ extraQueryParams ;
174+
175+ return $ obj ;
176+ }
177+
178+ public function withExtraBodyParams (mixed $ extraBodyParams ): self
179+ {
180+ $ obj = clone $ this ;
181+ $ obj ->extraBodyParams = $ extraBodyParams ;
182+
183+ return $ obj ;
184+ }
185+
186+ public function withTransporter (ClientInterface $ transporter ): self
187+ {
188+ $ obj = clone $ this ;
189+ $ obj ->transporter = $ transporter ;
190+
191+ return $ obj ;
192+ }
193+
194+ public function withUriFactory (UriFactoryInterface $ uriFactory ): self
113195 {
114- if (is_null ($ options )) {
115- return new self ;
116- }
196+ $ obj = clone $ this ;
197+ $ obj ->uriFactory = $ uriFactory ;
117198
118- if ($ options instanceof self) {
119- return $ options ;
120- }
199+ return $ obj ;
200+ }
201+
202+ public function withStreamFactory (
203+ StreamFactoryInterface $ streamFactory
204+ ): self {
205+ $ obj = clone $ this ;
206+ $ obj ->streamFactory = $ streamFactory ;
207+
208+ return $ obj ;
209+ }
121210
122- $ opts = new self ;
123- $ opts ->__unserialize ($ options );
211+ public function withRequestFactory (
212+ RequestFactoryInterface $ requestFactory
213+ ): self {
214+ $ obj = clone $ this ;
215+ $ obj ->requestFactory = $ requestFactory ;
124216
125- return $ opts ;
217+ return $ obj ;
126218 }
127219}
0 commit comments