2121use Ixnode \PhpApiVersionBundle \Utils \TypeCasting \TypeCastingHelper ;
2222use Ixnode \PhpException \ArrayType \ArrayKeyNotFoundException ;
2323use Ixnode \PhpException \Case \CaseInvalidException ;
24+ use Ixnode \PhpException \Case \CaseUnsupportedException ;
2425use Ixnode \PhpException \Type \TypeInvalidException ;
2526use Symfony \Component \Console \Input \ArrayInput ;
2627use Symfony \Component \Console \Input \InputArgument ;
2728use Symfony \Component \Console \Input \InputDefinition ;
2829use Symfony \Component \Console \Input \InputInterface ;
2930use Symfony \Component \Console \Input \InputOption ;
3031use Symfony \Component \DependencyInjection \ParameterBag \ParameterBagInterface ;
32+ use Symfony \Component \HttpFoundation \HeaderBag ;
33+ use Symfony \Component \HttpFoundation \Request ;
34+ use Symfony \Component \HttpFoundation \RequestStack ;
3135
3236/**
3337 * Class BaseProvider
@@ -47,16 +51,17 @@ abstract class BaseProvider implements ProviderInterface, ProcessorInterface
4751 /** @var array<string, mixed> $inputArgumentValues */
4852 protected array $ inputArgumentValues = [];
4953
50- protected const NAME_KERNEL_PROJECT_DIR = 'kernel.project_dir ' ;
54+ protected ?string $ error = null ;
55+
56+ final public const NAME_KERNEL_PROJECT_DIR = 'kernel.project_dir ' ;
5157
5258 protected const TEXT_UNDEFINED_METHOD = 'Please overwrite the "%s" method in your provider to use this function. ' ;
5359
5460 /**
55- * BaseProvider constructor.
56- *
5761 * @param ParameterBagInterface $parameterBag
62+ * @param RequestStack $request
5863 */
59- public function __construct (protected ParameterBagInterface $ parameterBag )
64+ public function __construct (protected ParameterBagInterface $ parameterBag, protected RequestStack $ request )
6065 {
6166 $ this ->input = new ArrayInput ([]);
6267 }
@@ -65,19 +70,25 @@ public function __construct(protected ParameterBagInterface $parameterBag)
6570 * Do the provided job and returns the base resource.
6671 *
6772 * @return BasePublicResource|BasePublicResource[]
68- * @throws CaseInvalidException
73+ * @throws CaseUnsupportedException
6974 * @noRector
7075 */
71- abstract protected function doProvide (): BasePublicResource |array ;
76+ protected function doProvide (): BasePublicResource |array
77+ {
78+ throw new CaseUnsupportedException (sprintf (self ::TEXT_UNDEFINED_METHOD , __METHOD__ ));
79+ }
7280
7381 /**
7482 * Do the processed job and returns the resource wrapper.
7583 *
7684 * @return BasePublicResource
77- * @throws CaseInvalidException
85+ * @throws CaseUnsupportedException
7886 * @noRector
7987 */
80- abstract protected function doProcess (): BasePublicResource ;
88+ protected function doProcess (): BasePublicResource
89+ {
90+ throw new CaseUnsupportedException (sprintf (self ::TEXT_UNDEFINED_METHOD , __METHOD__ ));
91+ }
8192
8293 /**
8394 * Binds given input definitions.
@@ -141,6 +152,7 @@ protected function getRouteProperties(): array
141152 * Gets the project directory.
142153 *
143154 * @return string
155+ * @throws TypeInvalidException
144156 * @throws ArrayKeyNotFoundException
145157 * @throws TypeInvalidException
146158 */
@@ -152,4 +164,208 @@ protected function getProjectDir(): string
152164
153165 return (new TypeCastingHelper ($ this ->parameterBag ->get (self ::NAME_KERNEL_PROJECT_DIR )))->strval ();
154166 }
167+
168+ /**
169+ * Returns the current request.
170+ *
171+ * @return Request
172+ * @throws CaseUnsupportedException
173+ */
174+ protected function getCurrentRequest (): Request
175+ {
176+ $ currentRequest = $ this ->request ->getCurrentRequest ();
177+
178+ if (is_null ($ currentRequest )) {
179+ throw new CaseUnsupportedException ('Can \'t get the CurrentRequest class(<code>$this->getRequest()->getCurrentRequest();</code>). ' );
180+ }
181+
182+ return $ currentRequest ;
183+ }
184+
185+ /**
186+ * Returns the header bag.
187+ *
188+ * @return HeaderBag
189+ * @throws CaseUnsupportedException
190+ */
191+ protected function getHeaderBag (): HeaderBag
192+ {
193+ $ currentRequest = $ this ->getCurrentRequest ();
194+
195+ return $ currentRequest ->headers ;
196+ }
197+
198+ /**
199+ * Returns if the given name exists as a header request.
200+ *
201+ * @param string $name
202+ * @return bool
203+ * @throws CaseUnsupportedException
204+ */
205+ public function hasHeader (string $ name ): bool
206+ {
207+ $ headerBag = $ this ->getHeaderBag ();
208+
209+ return $ headerBag ->has ($ name );
210+ }
211+
212+ /**
213+ * Returns the header bag request.
214+ *
215+ * @param string $name
216+ * @return string|null
217+ * @throws ArrayKeyNotFoundException
218+ * @throws CaseUnsupportedException
219+ */
220+ public function getHeader (string $ name ): ?string
221+ {
222+ if (!$ this ->hasHeader ($ name )) {
223+ throw new ArrayKeyNotFoundException ($ name );
224+ }
225+
226+ return $ this ->getHeaderBag ()->get ($ name );
227+ }
228+
229+ /**
230+ * Returns the given name from header (as string).
231+ *
232+ * @param string $name
233+ * @return string|null
234+ * @throws CaseUnsupportedException
235+ */
236+ public function getHeaderAsStringOrNull (string $ name ): ?string
237+ {
238+ if (!$ this ->hasHeader ($ name )) {
239+ return null ;
240+ }
241+
242+ return strval ($ this ->getHeaderBag ()->get ($ name ));
243+ }
244+
245+ /**
246+ * Returns the given name from header (as string).
247+ *
248+ * @param string $name
249+ * @return string
250+ * @throws CaseUnsupportedException
251+ */
252+ public function getHeaderAsString (string $ name ): string
253+ {
254+ if (!$ this ->hasHeader ($ name )) {
255+ throw new CaseUnsupportedException (sprintf ('Header missing "%s" ' , $ name ));
256+ }
257+
258+ return strval ($ this ->getHeaderBag ()->get ($ name ));
259+ }
260+
261+ /**
262+ * Returns the given name from header (as float).
263+ *
264+ * @param string $name
265+ * @return float|null
266+ * @throws CaseUnsupportedException
267+ */
268+ public function getHeaderAsFloatOrNull (string $ name ): ?float
269+ {
270+ if (!$ this ->hasHeader ($ name )) {
271+ return null ;
272+ }
273+
274+ return floatval ($ this ->getHeaderBag ()->get ($ name ));
275+ }
276+
277+ /**
278+ * Returns the given name from header (as float).
279+ *
280+ * @param string $name
281+ * @return float
282+ * @throws CaseUnsupportedException
283+ */
284+ public function getHeaderAsFloat (string $ name ): float
285+ {
286+ if (!$ this ->hasHeader ($ name )) {
287+ throw new CaseUnsupportedException (sprintf ('Header missing "%s" ' , $ name ));
288+ }
289+
290+ return floatval ($ this ->getHeaderBag ()->get ($ name ));
291+ }
292+
293+ /**
294+ * Returns the given name from header (as integer).
295+ *
296+ * @param string $name
297+ * @return int|null
298+ * @throws CaseUnsupportedException
299+ */
300+ public function getHeaderAsIntOrNull (string $ name ): ?int
301+ {
302+ if (!$ this ->hasHeader ($ name )) {
303+ return null ;
304+ }
305+
306+ return intval ($ this ->getHeaderBag ()->get ($ name ));
307+ }
308+
309+ /**
310+ * Returns the given name from header (as integer).
311+ *
312+ * @param string $name
313+ * @return int
314+ * @throws CaseUnsupportedException
315+ */
316+ public function getHeaderAsInt (string $ name ): int
317+ {
318+ if (!$ this ->hasHeader ($ name )) {
319+ throw new CaseUnsupportedException (sprintf ('Header missing "%s" ' , $ name ));
320+ }
321+
322+ return intval ($ this ->getHeaderBag ()->get ($ name ));
323+ }
324+
325+ /**
326+ * Returns the given name from header (as bool).
327+ *
328+ * @param string $name
329+ * @return bool
330+ * @throws CaseInvalidException
331+ * @throws CaseUnsupportedException
332+ * @throws TypeInvalidException
333+ */
334+ public function isHeaderAsBoolean (string $ name ): bool
335+ {
336+ if (!$ this ->hasHeader ($ name )) {
337+ return false ;
338+ }
339+
340+ $ value = (new TypeCastingHelper ($ this ->getHeaderBag ()->get ($ name )))->strval ();
341+
342+ return match ($ value ) {
343+ 'true ' => true ,
344+ 'false ' => false ,
345+ default => throw new CaseInvalidException ($ value , ['true ' , 'false ' ]),
346+ };
347+ }
348+
349+ /**
350+ * Gets an error of this resource.
351+ *
352+ * @return string|null
353+ */
354+ protected function getError (): ?string
355+ {
356+ return $ this ->error ;
357+ }
358+
359+ /**
360+ * Sets an error of this resource.
361+ *
362+ * @param string|null $error
363+ * @return self
364+ */
365+ protected function setError (?string $ error ): self
366+ {
367+ $ this ->error = $ error ;
368+
369+ return $ this ;
370+ }
155371}
0 commit comments