1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\Serializer\Mapping\Loader; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
17
|
|
|
use Silverback\ApiComponentBundle\Annotation\Publishable; |
18
|
|
|
use Silverback\ApiComponentBundle\Entity\Utility\PublishableInterface; |
19
|
|
|
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; |
20
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Adds {CLASS}:publishable serialization group on {CLASS}.publishedAt for Publishable entities. |
24
|
|
|
* Adds {CLASS}:publishable serialization group on {CLASS}.isPublished for Publishable entities with PublishableInterface. |
25
|
|
|
* |
26
|
|
|
* @author Vincent Chalamon <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class PublishableLoader implements LoaderInterface |
29
|
|
|
{ |
30
|
|
|
private AnnotationReader $reader; |
31
|
|
|
|
32
|
|
|
public function __construct(AnnotationReader $reader) |
33
|
|
|
{ |
34
|
|
|
$this->reader = $reader; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
41
|
|
|
{ |
42
|
|
|
$reflectionClass = $classMetadata->getReflectionClass(); |
43
|
|
|
/** @var Publishable $configuration */ |
44
|
|
|
if (!$configuration = $this->reader->getClassAnnotation($reflectionClass, Publishable::class)) { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ( |
49
|
|
|
($attributeMetadata = ($classMetadata->getAttributesMetadata()[$configuration->fieldName] ?? null)) && |
50
|
|
|
!empty($attributeMetadata->getGroups()) |
51
|
|
|
) { |
52
|
|
|
$attributeMetadata->addGroup(sprintf('%s:publishable:read', $reflectionClass->getShortName())); |
53
|
|
|
$attributeMetadata->addGroup(sprintf('%s:publishable:write', $reflectionClass->getShortName())); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ( |
57
|
|
|
($attributeMetadata = ($classMetadata->getAttributesMetadata()[$configuration->associationName] ?? null)) && |
58
|
|
|
!empty($attributeMetadata->getGroups()) |
59
|
|
|
) { |
60
|
|
|
$attributeMetadata->addGroup(sprintf('%s:publishable:read', $reflectionClass->getShortName())); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ( |
64
|
|
|
is_a($classMetadata->getName(), PublishableInterface::class, true) && |
65
|
|
|
($attributeMetadata = ($classMetadata->getAttributesMetadata()['published'] ?? null)) && |
66
|
|
|
!empty($attributeMetadata->getGroups()) |
67
|
|
|
) { |
68
|
|
|
$attributeMetadata->addGroup(sprintf('%s:publishable:read', $reflectionClass->getShortName())); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|