Passed
Push — trunk ( ff423b...970276 )
by Christian
13:19 queued 19s
created

ElasticsearchFieldBuilder::nested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Framework;
4
5
use Shopware\Core\Framework\Context;
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Core\System\Language\LanguageLoaderInterface;
8
use Shopware\Elasticsearch\Product\CustomFieldUpdater;
9
10
/**
11
 * @final
12
 */
13
#[Package('buyers-experience')]
14
class ElasticsearchFieldBuilder
15
{
16
    /**
17
     * @internal
18
     *
19
     * @param array<string, string> $languageAnalyzerMapping
20
     */
21
    public function __construct(
22
        private readonly LanguageLoaderInterface $languageLoader,
23
        private readonly ElasticsearchIndexingUtils $indexingUtils,
24
        private readonly array $languageAnalyzerMapping
25
    ) {
26
    }
27
28
    /**
29
     * @param array<string, mixed> $fieldConfig
30
     *
31
     * @description This method is used to build the mapping for translated fields
32
     *
33
     * @return array{properties: array<string, mixed>}
34
     */
35
    public function translated(array $fieldConfig): array
36
    {
37
        $languages = $this->languageLoader->loadLanguages();
38
39
        $languageFields = [];
40
41
        foreach ($languages as $languageId => ['code' => $code]) {
42
            $parts = explode('-', $code);
43
            $locale = $parts[0];
44
45
            $languageFields[$languageId] = $fieldConfig;
46
47
            if (\array_key_exists($locale, $this->languageAnalyzerMapping)) {
48
                $languageFields[$languageId]['fields']['search']['analyzer'] = $this->languageAnalyzerMapping[$locale];
49
            }
50
        }
51
52
        return ['properties' => $languageFields];
53
    }
54
55
    /**
56
     * @description This method is used to build the mapping for translated custom fields
57
     *
58
     * @return array{ properties: array<string, array<string, string>> }
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
59
     */
60
    public function customFields(string $entity, Context $context): array
61
    {
62
        $languages = $this->languageLoader->loadLanguages();
63
64
        $customFields = [];
65
66
        foreach (array_keys($languages) as $languageId) {
67
            $customFields[$languageId] = $this->getCustomFieldsMapping($entity, $context);
68
        }
69
70
        return ['properties' => $customFields];
71
    }
72
73
    /**
74
     * @description This method is used to build the mapping for datetime fields
75
     *
76
     * @param array<string, mixed> $override
77
     *
78
     * @return array<string, mixed>
79
     */
80
    public static function datetime(array $override = []): array
81
    {
82
        return array_merge([
83
            'type' => 'date',
84
            'format' => 'yyyy-MM-dd HH:mm:ss.000||strict_date_optional_time||epoch_millis',
85
            'ignore_malformed' => true,
86
        ], $override);
87
    }
88
89
    /**
90
     * @description This method is used to build the mapping for nested fields
91
     *
92
     * @param array<string, mixed> $properties
93
     *
94
     * @return array{type: 'nested', properties: array<string, mixed>}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{type: 'nested', pr...: array<string, mixed>} at position 4 could not be parsed: Unknown type name ''nested'' at position 4 in array{type: 'nested', properties: array<string, mixed>}.
Loading history...
95
     */
96
    public static function nested(array $properties = []): array
97
    {
98
        return [
99
            'type' => 'nested',
100
            'properties' => array_filter(array_merge([
101
                'id' => AbstractElasticsearchDefinition::KEYWORD_FIELD,
102
                '_count' => AbstractElasticsearchDefinition::INT_FIELD,
103
            ], $properties)),
104
        ];
105
    }
106
107
    /**
108
     * @return array<string, mixed>
109
     */
110
    private function getCustomFieldsMapping(string $entity, Context $context): array
111
    {
112
        $fieldMapping = $this->indexingUtils->getCustomFieldTypes($entity, $context);
113
114
        $mapping = [
115
            'type' => 'object',
116
            'dynamic' => true,
117
            'properties' => [],
118
        ];
119
120
        foreach ($fieldMapping as $name => $type) {
121
            /** @var array<mixed> $esType */
122
            $esType = CustomFieldUpdater::getTypeFromCustomFieldType($type);
123
124
            $mapping['properties'][$name] = $esType;
125
        }
126
127
        return $mapping;
128
    }
129
}
130