1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[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 ApiPlatform\Core\Serializer; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
18
|
|
|
use ApiPlatform\Core\Serializer\Filter\FilterInterface; |
19
|
|
|
use ApiPlatform\Core\Util\RequestAttributesExtractor; |
20
|
|
|
use Psr\Container\ContainerInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
* |
26
|
|
|
* @author Baptiste Meyer <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class SerializerFilterContextBuilder implements SerializerContextBuilderInterface |
29
|
|
|
{ |
30
|
|
|
private $decorated; |
31
|
|
|
private $filterLocator; |
32
|
|
|
private $resourceMetadataFactory; |
33
|
|
|
|
34
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ContainerInterface $filterLocator, SerializerContextBuilderInterface $decorated) |
35
|
|
|
{ |
36
|
|
|
$this->decorated = $decorated; |
37
|
|
|
$this->filterLocator = $filterLocator; |
38
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array |
45
|
|
|
{ |
46
|
|
View Code Duplication |
if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) { |
|
|
|
|
47
|
|
|
throw new RuntimeException('Request attributes are not valid.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $attributes); |
51
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
52
|
|
|
|
53
|
|
|
if (isset($attributes['collection_operation_name']) || isset($attributes['subresource_operation_name'])) { |
54
|
|
|
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute( |
55
|
|
|
$attributes['collection_operation_name'] ?? $attributes['subresource_operation_name'], |
56
|
|
|
'filters', |
57
|
|
|
[], |
58
|
|
|
true |
59
|
|
|
); |
60
|
|
|
} else { |
61
|
|
|
$resourceFilters = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], 'filters', [], true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!$resourceFilters) { |
65
|
|
|
return $context; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($resourceFilters as $filterId) { |
69
|
|
|
if ($this->filterLocator->has($filterId) && ($filter = $this->filterLocator->get($filterId)) instanceof FilterInterface) { |
70
|
|
|
$filter->apply($request, $normalization, $attributes, $context); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $context; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.