1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
6
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\ComponentLocation; |
9
|
|
|
use Silverback\ApiComponentBundle\Entity\Layout\Layout; |
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Navigation\AbstractNavigation; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Navigation\AbstractNavigationItem; |
12
|
|
|
use Silverback\ApiComponentBundle\Entity\Navigation\Route\Route; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
class ApiContextBuilder implements SerializerContextBuilderInterface |
16
|
|
|
{ |
17
|
|
|
private $decorated; |
18
|
|
|
|
19
|
|
|
public function __construct(SerializerContextBuilderInterface $decorated) |
20
|
|
|
{ |
21
|
|
|
$this->decorated = $decorated; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $group |
26
|
|
|
* @param bool $normalization |
27
|
|
|
* @param null|string $operation |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
private function getGroups(string $group, bool $normalization, ?string $operation): array |
31
|
|
|
{ |
32
|
|
|
$groups = [$group, $group . ($normalization ? '_read' : '_write')]; |
33
|
|
|
if ($operation) { |
34
|
|
|
$groups[] = "${group}_${operation}"; |
35
|
|
|
} |
36
|
|
|
return $groups; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $className |
41
|
|
|
* @param $matchClassName |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
private function matchClass($className, $matchClassName): bool |
45
|
|
|
{ |
46
|
|
|
return $className === $matchClassName || is_subclass_of($className, $matchClassName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Request $request |
51
|
|
|
* @param bool $normalization |
52
|
|
|
* @param array|null $extractedAttributes |
53
|
|
|
* @return array |
54
|
|
|
* @throws \ApiPlatform\Core\Exception\RuntimeException |
55
|
|
|
*/ |
56
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array |
57
|
|
|
{ |
58
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
59
|
|
|
if (isset($context['groups']) && \in_array('none', $context['groups'], true)) { |
60
|
|
|
return $context; |
61
|
|
|
} |
62
|
|
|
$subject = $request->attributes->get('_api_resource_class'); |
63
|
|
|
$operation = $context['item_operation_name'] ?? null; |
64
|
|
|
$groups = []; |
65
|
|
|
if ( |
66
|
|
|
$this->matchClass($subject, AbstractComponent::class) || |
67
|
|
|
$this->matchClass($subject, AbstractNavigation::class) || |
68
|
|
|
$this->matchClass($subject, ComponentLocation::class) |
69
|
|
|
) { |
70
|
|
|
$groups[] = $this->getGroups('component', $normalization, $operation); |
71
|
|
|
} |
72
|
|
|
if ( |
73
|
|
|
$this->matchClass($subject, AbstractNavigationItem::class) |
74
|
|
|
) { |
75
|
|
|
$groups[] = $this->getGroups('component_item', $normalization, $operation); |
76
|
|
|
} |
77
|
|
|
if ( |
78
|
|
|
$this->matchClass($subject, AbstractContent::class) |
79
|
|
|
) { |
80
|
|
|
$groups[] = $this->getGroups('content', $normalization, $operation); |
81
|
|
|
} |
82
|
|
|
if ( |
83
|
|
|
$this->matchClass($subject, Route::class) |
84
|
|
|
) { |
85
|
|
|
$groups[] = $this->getGroups('route', $normalization, $operation); |
86
|
|
|
} |
87
|
|
|
if ($this->matchClass($subject, Layout::class)) { |
88
|
|
|
$groups[] = $this->getGroups('layout', $normalization, $operation); |
89
|
|
|
} |
90
|
|
|
if (\count($groups)) { |
91
|
|
|
if (!isset($context['groups'])) { |
92
|
|
|
$context['groups'] = ['default']; |
93
|
|
|
} else { |
94
|
|
|
$context['groups'][] = ['default']; |
95
|
|
|
} |
96
|
|
|
array_unshift($groups, $context['groups']); |
97
|
|
|
$context['groups'] = array_merge( /** @scrutinizer ignore-type */ ...$groups); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
return $context; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|