-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathDocumentNormalizer.php
More file actions
97 lines (83 loc) · 3.34 KB
/
DocumentNormalizer.php
File metadata and controls
97 lines (83 loc) · 3.34 KB
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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Elasticsearch\Serializer;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Document normalizer for Elasticsearch.
*
* @experimental
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
final class DocumentNormalizer implements NormalizerInterface, SerializerAwareInterface
{
public const FORMAT = 'elasticsearch';
private readonly ObjectNormalizer $decoratedNormalizer;
public function __construct(
?ClassMetadataFactoryInterface $classMetadataFactory = null,
?NameConverterInterface $nameConverter = null,
?PropertyAccessorInterface $propertyAccessor = null,
?PropertyTypeExtractorInterface $propertyTypeExtractor = null,
?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null,
?callable $objectClassResolver = null,
array $defaultContext = [],
) {
$this->decoratedNormalizer = new ObjectNormalizer($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);
}
/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
// Ensure that a resource is being normalized
if (!\is_object($data)) {
return false;
}
// Only normalize for the elasticsearch format
return self::FORMAT === $format;
}
/**
* {@inheritdoc}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$normalizedData = $this->decoratedNormalizer->normalize($data, $format, $context);
// Add _id and _source if not already present
// This is a basic implementation and might need to be more sophisticated based on specific needs.
// It assumes 'id' is the primary identifier for the resource.
if (\is_array($normalizedData) && !isset($normalizedData['_id']) && isset($normalizedData['id'])) {
$normalizedData = ['_id' => (string) $normalizedData['id'], '_source' => $normalizedData];
}
return $normalizedData;
}
/**
* {@inheritdoc}
*/
public function setSerializer(SerializerInterface $serializer): void
{
$this->decoratedNormalizer->setSerializer($serializer);
}
/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array
{
return self::FORMAT === $format ? ['object' => true] : [];
}
}