Skip to content

Commit d451bb2

Browse files
committed
Add ApiPlatform State Helper
1 parent 7e73479 commit d451bb2

File tree

8 files changed

+1149
-23
lines changed

8 files changed

+1149
-23
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"ixnode/php-array-to-object": "^0.1.1",
2626
"ixnode/php-checker": "^0.1.9",
2727
"ixnode/php-container": "^0.1.8",
28-
"ixnode/php-exception": "^0.1.19",
28+
"ixnode/php-exception": "^0.1.20",
2929
"ixnode/php-json-schema-validator": "^0.1.2",
3030
"ixnode/php-naming-conventions": "^0.1.1",
3131
"nelmio/cors-bundle": "^2.2",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ApiPlatform/Route/Base/BaseRoute.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class BaseRoute
3030

3131
final public const KEY_TYPE = 'type';
3232

33+
final public const TYPE_BOOLEAN = 'boolean';
34+
3335
final public const TYPE_STRING = 'string';
3436

3537
final public const TYPE_ENUM_STRING = 'string';

src/ApiPlatform/State/Base/BaseProvider.php

Lines changed: 224 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
use Ixnode\PhpApiVersionBundle\Utils\TypeCasting\TypeCastingHelper;
2222
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
2323
use Ixnode\PhpException\Case\CaseInvalidException;
24+
use Ixnode\PhpException\Case\CaseUnsupportedException;
2425
use Ixnode\PhpException\Type\TypeInvalidException;
2526
use Symfony\Component\Console\Input\ArrayInput;
2627
use Symfony\Component\Console\Input\InputArgument;
2728
use Symfony\Component\Console\Input\InputDefinition;
2829
use Symfony\Component\Console\Input\InputInterface;
2930
use Symfony\Component\Console\Input\InputOption;
3031
use 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
}

src/ApiPlatform/State/Base/Raw/BaseRawProvider.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
use ApiPlatform\Metadata\Operation;
1717
use Ixnode\PhpApiVersionBundle\ApiPlatform\Resource\Base\BasePublicResource;
1818
use Ixnode\PhpApiVersionBundle\ApiPlatform\State\Base\BaseProvider;
19-
use Ixnode\PhpException\Case\CaseInvalidException;
19+
use Ixnode\PhpException\Case\CaseUnsupportedException;
2020
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
21+
use Symfony\Component\HttpFoundation\RequestStack;
2122

2223
/**
2324
* Class BaseRawProvider
@@ -40,17 +41,17 @@ abstract class BaseRawProvider extends BaseProvider
4041
/**
4142
* BaseDirectProvider constructor.
4243
*/
43-
public function __construct(protected ParameterBagInterface $parameterBag)
44+
public function __construct(protected ParameterBagInterface $parameterBag, protected RequestStack $request)
4445
{
45-
parent::__construct($this->parameterBag);
46+
parent::__construct($this->parameterBag, $this->request);
4647
}
4748

4849
/**
4950
* @param Operation $operation
5051
* @param array<string, mixed> $uriVariables
5152
* @param array<int|string, mixed> $context
5253
* @return BasePublicResource|BasePublicResource[]
53-
* @throws CaseInvalidException
54+
* @throws CaseUnsupportedException
5455
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5556
*/
5657
public function provide(Operation $operation, array $uriVariables = [], array $context = []): BasePublicResource|array
@@ -64,7 +65,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6465
* @param array<string, mixed> $uriVariables
6566
* @param array<int|string, mixed> $context
6667
* @return BasePublicResource
67-
* @throws CaseInvalidException
68+
* @throws CaseUnsupportedException
6869
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6970
*/
7071
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): BasePublicResource

0 commit comments

Comments
 (0)