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\Normalizer; |
15
|
|
|
|
16
|
|
|
use Silverback\ApiComponentBundle\Helper\FileHelper; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
21
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
22
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Prevent serialization and deserialzation of the internal file path. |
27
|
|
|
* |
28
|
|
|
* @author Daniel West <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class FileNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface |
31
|
|
|
{ |
32
|
|
|
use NormalizerAwareTrait; |
33
|
|
|
use DenormalizerAwareTrait; |
34
|
|
|
|
35
|
|
|
private const ALREADY_CALLED = 'FILE_NORMALIZER_ALREADY_CALLED'; |
36
|
|
|
|
37
|
|
|
private FileHelper $fileHelper; |
38
|
|
|
|
39
|
|
|
public function __construct(FileHelper $fileHelper) |
40
|
|
|
{ |
41
|
|
|
$this->fileHelper = $fileHelper; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function hasCacheableSupportsMethod(): bool |
45
|
|
|
{ |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []) |
50
|
|
|
{ |
51
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $this->fileHelper->isConfigured($type); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function supportsNormalization($data, $format = null, array $context = []) |
55
|
|
|
{ |
56
|
|
|
return !isset($context[self::ALREADY_CALLED]) && |
57
|
|
|
\is_object($data) && |
58
|
|
|
!$data instanceof \Traversable && |
59
|
|
|
$this->fileHelper->isConfigured($data); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
63
|
|
|
{ |
64
|
|
|
$context[self::ALREADY_CALLED] = true; |
65
|
|
|
$configuration = $this->fileHelper->getConfiguration($type); |
66
|
|
|
|
67
|
|
|
unset($data[$configuration->filePathFieldName]); |
68
|
|
|
|
69
|
|
|
return $this->denormalizer->denormalize($data, $type, $format, $context); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function normalize($object, $format = null, array $context = []) |
73
|
|
|
{ |
74
|
|
|
$context[self::ALREADY_CALLED] = true; |
75
|
|
|
$configuration = $this->fileHelper->getConfiguration($object); |
76
|
|
|
$data = $this->normalizer->normalize($object, $format, $context); |
77
|
|
|
|
78
|
|
|
unset($data[$configuration->filePathFieldName]); |
79
|
|
|
|
80
|
|
|
return $data; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|