1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
6
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\FileInterface; |
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form\Form; |
9
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\Component\Form\FormViewFactory; |
10
|
|
|
use Silverback\ApiComponentBundle\Imagine\FileSystemLoader; |
11
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
13
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
14
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
15
|
|
|
|
16
|
|
|
final class ApiNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
17
|
|
|
{ |
18
|
|
|
private $decorated; |
19
|
|
|
private $imagineCacheManager; |
20
|
|
|
private $formViewFactory; |
21
|
|
|
private $fileSystemLoader; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* FileNormalizer constructor. |
25
|
|
|
* @param NormalizerInterface $decorated |
26
|
|
|
* @param CacheManager $imagineCacheManager |
27
|
|
|
* @param FormViewFactory $formViewFactory |
28
|
|
|
* @param FileSystemLoader $fileSystemLoader |
29
|
|
|
*/ |
30
|
7 |
|
public function __construct( |
31
|
|
|
NormalizerInterface $decorated, |
32
|
|
|
CacheManager $imagineCacheManager, |
33
|
|
|
FormViewFactory $formViewFactory, |
34
|
|
|
FileSystemLoader $fileSystemLoader |
35
|
|
|
) { |
36
|
7 |
|
if (!$decorated instanceof DenormalizerInterface) { |
37
|
|
|
throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class)); |
38
|
|
|
} |
39
|
7 |
|
$this->decorated = $decorated; |
40
|
7 |
|
$this->imagineCacheManager = $imagineCacheManager; |
41
|
7 |
|
$this->formViewFactory = $formViewFactory; |
42
|
7 |
|
$this->fileSystemLoader = $fileSystemLoader; |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param mixed $data |
47
|
|
|
* @param string|null $format |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
1 |
|
public function supportsNormalization($data, $format = null): bool |
51
|
|
|
{ |
52
|
1 |
|
return $this->decorated->supportsNormalization($data, $format); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $object |
57
|
|
|
* @param string|null $format |
58
|
|
|
* @param array $context |
59
|
|
|
* @return array|bool|float|int|string |
60
|
|
|
* @throws \Symfony\Component\Serializer\Exception\LogicException |
61
|
|
|
* @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException |
62
|
|
|
* @throws \Symfony\Component\Serializer\Exception\CircularReferenceException |
63
|
|
|
*/ |
64
|
2 |
|
public function normalize($object, $format = null, array $context = []) |
65
|
|
|
{ |
66
|
2 |
|
$data = $this->decorated->normalize($object, $format, $context); |
67
|
|
|
|
68
|
2 |
|
if ($object instanceof FileInterface) { |
69
|
1 |
|
$data = array_merge($data, $this->getFileData($object)); |
70
|
|
|
} |
71
|
2 |
|
if ($object instanceof Form) { |
72
|
1 |
|
$data['form'] = $this->formViewFactory->create($object); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param FileInterface $object |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
1 |
|
private function getFileData(FileInterface $object): array |
83
|
|
|
{ |
84
|
1 |
|
$data = []; |
85
|
1 |
|
$filePath = $object->getFilePath(); |
86
|
1 |
|
if ($filePath) { |
87
|
1 |
|
if (false !== \exif_imagetype($filePath)) { |
88
|
1 |
|
[$width, $height] = getimagesize($filePath); |
89
|
|
|
} else { |
90
|
|
|
$width = $height = 0; |
91
|
|
|
} |
92
|
1 |
|
$data['width'] = $width; |
93
|
1 |
|
$data['height'] = $height; |
94
|
|
|
|
95
|
1 |
|
$supported = $this->isImagineSupportedFile($filePath); |
96
|
1 |
|
foreach ($object::getImagineFilters() as $returnKey => $filter) { |
97
|
1 |
|
$data[$returnKey] = $supported ? parse_url( |
98
|
1 |
|
$this->imagineCacheManager->getBrowserPath($this->fileSystemLoader->getImaginePath($filePath), $filter), |
99
|
1 |
|
PHP_URL_PATH |
100
|
1 |
|
) : null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
1 |
|
return $data; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $data |
108
|
|
|
* @param string $type |
109
|
|
|
* @param string|null $format |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
1 |
|
public function supportsDenormalization($data, $type, $format = null): bool |
113
|
|
|
{ |
114
|
1 |
|
return $this->decorated->supportsDenormalization($data, $type, $format); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed $data |
119
|
|
|
* @param string $class |
120
|
|
|
* @param string|null $format |
121
|
|
|
* @param array $context |
122
|
|
|
* @return object |
123
|
|
|
* @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException |
124
|
|
|
* @throws \Symfony\Component\Serializer\Exception\RuntimeException |
125
|
|
|
* @throws \Symfony\Component\Serializer\Exception\LogicException |
126
|
|
|
* @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException |
127
|
|
|
* @throws \Symfony\Component\Serializer\Exception\ExtraAttributesException |
128
|
|
|
* @throws \Symfony\Component\Serializer\Exception\BadMethodCallException |
129
|
|
|
* @throws \InvalidArgumentException |
130
|
|
|
*/ |
131
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
132
|
|
|
{ |
133
|
1 |
|
$context['allow_extra_attributes'] = $class === Form::class; |
134
|
1 |
|
$entity = $this->decorated->denormalize($data, $class, $format, $context); |
135
|
|
|
if ( |
136
|
1 |
|
$entity instanceof AbstractComponent && |
137
|
1 |
|
$parentComponent = $entity->getParent() |
138
|
|
|
) { |
139
|
1 |
|
$entity->addToParentComponent($parentComponent); |
140
|
|
|
} |
141
|
1 |
|
return $entity; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param SerializerInterface $serializer |
146
|
|
|
*/ |
147
|
1 |
|
public function setSerializer(SerializerInterface $serializer) |
148
|
|
|
{ |
149
|
1 |
|
if ($this->decorated instanceof SerializerAwareInterface) { |
150
|
1 |
|
$this->decorated->setSerializer($serializer); |
151
|
|
|
} |
152
|
1 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $filePath |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
3 |
|
public function isImagineSupportedFile(?string $filePath): bool |
159
|
|
|
{ |
160
|
3 |
|
if (!$filePath) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
try { |
164
|
3 |
|
$imageType = \exif_imagetype($filePath); |
165
|
1 |
|
} catch (\Exception $e) { |
166
|
1 |
|
return false; |
167
|
|
|
} |
168
|
3 |
|
return \in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000, IMAGETYPE_PNG, IMAGETYPE_GIF], true); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|