forked from FriendsOfAkeneo/CustomEntityBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomEntityNormalizer.php
121 lines (104 loc) · 4.25 KB
/
CustomEntityNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Pim\Bundle\CustomEntityBundle\Normalizer;
use Akeneo\Platform\Bundle\UIBundle\Provider\StructureVersion\StructureVersionProviderInterface;
use Akeneo\Tool\Bundle\VersioningBundle\Manager\VersionManager;
use Doctrine\Common\Util\ClassUtils;
use Pim\Bundle\CustomEntityBundle\Entity\AbstractCustomEntity;
use Pim\Bundle\CustomEntityBundle\Entity\AbstractTranslatableCustomEntity;
use Pim\Bundle\CustomEntityBundle\Versioning\VersionableInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Internal API normalizer.
*
* Used to generate JSON rest responses.
* @see \Pim\Bundle\CustomEntityBundle\Controller\RestController
*
* @author JM Leroux <jean-marie.leroux@akeneo.com>
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class CustomEntityNormalizer implements NormalizerInterface
{
/** @var array $supportedFormats */
protected $supportedFormats = ['internal_api'];
/** @var NormalizerInterface */
protected $pimSerializer;
/** @var VersionManager */
protected $versionManager;
/** @var NormalizerInterface */
protected $versionNormalizer;
/** @var StructureVersionProviderInterface */
protected $structureVersionprovider;
/**
* @param NormalizerInterface $pimSerializer
* @param VersionManager $versionManager
* @param NormalizerInterface $versionNormalizer
* @param StructureVersionProviderInterface $structureVersionProvider
*/
public function __construct(
NormalizerInterface $pimSerializer,
VersionManager $versionManager,
NormalizerInterface $versionNormalizer,
StructureVersionProviderInterface $structureVersionProvider
) {
$this->pimSerializer = $pimSerializer;
$this->versionManager = $versionManager;
$this->versionNormalizer = $versionNormalizer;
$this->structureVersionprovider = $structureVersionProvider;
}
/**
* @param AbstractCustomEntity $entity
* @param null $format
* @param array $context
*
* @return array
*/
public function normalize($entity, $format = null, array $context = []): array
{
$normalizedEntity = $this->pimSerializer->normalize($entity, 'standard', $context);
$normalizedEntity['meta'] = [
'structure_version' => null,
'id' => $entity->getId(),
'customEntityName' => $context['customEntityName'],
'form' => $context['form'],
];
if ($entity instanceof VersionableInterface) {
$this->structureVersionprovider->addResource(ClassUtils::getClass($entity));
$firstVersion = $this->versionManager->getOldestLogEntry($entity);
$lastVersion = $this->versionManager->getNewestLogEntry($entity);
$firstVersion = null !== $firstVersion ?
$this->versionNormalizer->normalize($firstVersion, 'internal_api', $context) :
null;
$lastVersion = null !== $lastVersion ?
$this->versionNormalizer->normalize($lastVersion, 'internal_api', $context) :
null;
$normalizedEntity['meta']['created'] = $firstVersion;
$normalizedEntity['meta']['updated'] = $lastVersion;
$normalizedEntity['meta']['structure_version'] = $this->structureVersionprovider->getStructureVersion();
$normalizedEntity['meta']['model_type'] = $context['customEntityName'];
}
return $normalizedEntity;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof AbstractCustomEntity && in_array($format, $this->supportedFormats);
}
/**
* @param AbstractCustomEntity $entity
*
* @return array
*/
protected function getLabels(AbstractCustomEntity $entity): array
{
$labels = [];
if ($entity instanceof AbstractTranslatableCustomEntity) {
foreach ($entity->getTranslations() as $translation) {
$labels[$translation->getLocale()] = $translation->getLabel();
}
}
return $labels;
}
}