|
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
|
|
|
/** |
|
18
|
|
|
* @var string[][] |
|
19
|
|
|
*/ |
|
20
|
|
|
public const CLASS_GROUP_MAPPING = [ |
|
21
|
|
|
AbstractComponent::class => ['component'], |
|
22
|
|
|
AbstractNavigation::class => ['component'], |
|
23
|
|
|
ComponentLocation::class => ['component'], |
|
24
|
|
|
AbstractNavigationItem::class => ['component_item'], |
|
25
|
|
|
AbstractContent::class => ['content'], |
|
26
|
|
|
Route::class => ['route'], |
|
27
|
|
|
Layout::class => ['layout'] |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SerializerContextBuilderInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $decorated; |
|
34
|
|
|
|
|
35
|
4 |
|
public function __construct(SerializerContextBuilderInterface $decorated) |
|
36
|
|
|
{ |
|
37
|
4 |
|
$this->decorated = $decorated; |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $group |
|
42
|
|
|
* @param bool $normalization |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
3 |
|
private function getGroupNames(string $group, bool $normalization): array |
|
46
|
|
|
{ |
|
47
|
3 |
|
return [$group, $group . ($normalization ? '_read' : '_write')]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $className |
|
52
|
|
|
* @param $matchClassName |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
3 |
|
private function matchClass($className, $matchClassName): bool |
|
56
|
|
|
{ |
|
57
|
3 |
|
return $className === $matchClassName || is_subclass_of($className, $matchClassName); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $subject |
|
62
|
|
|
* @param bool $normalization |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
3 |
|
private function getGroups(string $subject, bool $normalization): array |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var string[] $groups */ |
|
68
|
3 |
|
$groups = [['default']]; |
|
69
|
3 |
|
foreach (self::CLASS_GROUP_MAPPING as $class=>$groupMapping) { |
|
70
|
3 |
|
if ($this->matchClass($subject, $class)) { |
|
71
|
3 |
|
foreach ($groupMapping as $group) { |
|
72
|
3 |
|
$groups[] = $this->getGroupNames($group, $normalization); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
3 |
|
return $groups; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Request $request |
|
81
|
|
|
* @param bool $normalization |
|
82
|
|
|
* @param array|null $extractedAttributes |
|
83
|
|
|
* @return array |
|
84
|
|
|
* @throws \ApiPlatform\Core\Exception\RuntimeException |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array |
|
87
|
|
|
{ |
|
88
|
4 |
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
|
89
|
4 |
|
if (isset($context['groups']) && \in_array('none', $context['groups'], true)) { |
|
90
|
1 |
|
return $context; |
|
91
|
|
|
} |
|
92
|
3 |
|
$subject = $request->attributes->get('_api_resource_class'); |
|
93
|
3 |
|
$groups = $this->getGroups($subject, $normalization); |
|
94
|
3 |
|
if (\count($groups)) { |
|
95
|
3 |
|
$context['groups'] = array_merge($context['groups'] ?? [], ...$groups); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
3 |
|
return $context; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|