1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Locastic\Loggastic\Serializer; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use Locastic\Loggastic\Identifier\LogIdentifierExtractorInterface; |
7
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
8
|
|
|
|
9
|
|
|
// Use identifier as collection index |
10
|
|
|
final class ActivityLogCollectionNormalizer implements ActivityLogCollectionNormalizerInterface |
11
|
|
|
{ |
12
|
|
|
public const FORMAT = 'activityLog'; |
13
|
|
|
|
14
|
|
|
public function __construct( |
15
|
|
|
private readonly NormalizerInterface $decorated, |
16
|
|
|
private readonly LogIdentifierExtractorInterface $logIdentifierExtractor, |
17
|
|
|
private readonly bool $useIdentifierExtractor |
18
|
|
|
) |
19
|
|
|
{ |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function normalize($data, string $format = null, array $context = []): array |
23
|
|
|
{ |
24
|
|
|
$collection = []; |
25
|
|
|
|
26
|
|
|
if ($this->useIdentifierExtractor) { |
27
|
|
|
foreach ($data as $item) { |
28
|
|
|
$normalizedItem = $this->decorated->normalize($item, 'array', $context); |
29
|
|
|
|
30
|
|
|
$logId = $this->logIdentifierExtractor->getIdentifierValue($item); |
31
|
|
|
if ($logId === null) { |
32
|
|
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$collection[$logId] = $normalizedItem; |
36
|
|
|
} |
37
|
|
|
} else { |
38
|
|
|
foreach ($data as $item) { |
39
|
|
|
$normalizedItem = $this->decorated->normalize($item, 'array', $context); |
40
|
|
|
$collection[] = $normalizedItem; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $collection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function supportsNormalization($data, string $format = null, array $context = []): bool |
48
|
|
|
{ |
49
|
|
|
return $data instanceof Collection && self::FORMAT === $format; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getSupportedTypes(?string $format): array |
53
|
|
|
{ |
54
|
|
|
if (self::FORMAT !== $format) { |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return [ |
59
|
|
|
Collection::class => true, |
60
|
|
|
'array' => true, |
61
|
|
|
'object' => true, |
62
|
|
|
'Locastic\Loggastic\Model\Output\ActivityLogInterface' => true, |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|