1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\DataProvider\StateProvider; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Doctrine\Orm\State\CollectionProvider; |
17
|
|
|
use ApiPlatform\Doctrine\Orm\State\ItemProvider; |
18
|
|
|
use ApiPlatform\Metadata\CollectionOperationInterface; |
19
|
|
|
use ApiPlatform\Metadata\Operation; |
20
|
|
|
use ApiPlatform\State\ProviderInterface; |
21
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RouteStateProvider implements ProviderInterface |
27
|
|
|
{ |
28
|
|
|
private RouteRepository $routeRepository; |
29
|
|
|
private ProviderInterface $defaultProvider; |
30
|
|
|
|
31
|
|
|
public function __construct(RouteRepository $routeRepository, ProviderInterface $defaultProvider) |
32
|
|
|
{ |
33
|
|
|
$this->routeRepository = $routeRepository; |
34
|
|
|
$this->defaultProvider = $defaultProvider; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
38
|
|
|
{ |
39
|
|
|
if ($operation instanceof CollectionOperationInterface) { |
40
|
|
|
return $this->defaultProvider->provide($operation->withProvider(CollectionProvider::class), $uriVariables, $context); |
41
|
|
|
} |
42
|
|
|
$id = $uriVariables['id']; |
43
|
|
|
if (!\is_string($id)) { |
44
|
|
|
return $this->defaultProvider->provide($operation->withProvider(ItemProvider::class), $uriVariables, $context); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->routeRepository->findOneByIdOrPath($id); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|