1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Xervice\Elasticsearch\Business\Model\Index; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class MappingConverter implements MappingConverterInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $typeMapping = [ |
13
|
|
|
'int' => 'integer', |
14
|
|
|
'bool' => 'boolean', |
15
|
|
|
'double' => 'double', |
16
|
|
|
'float' => 'float', |
17
|
|
|
'string' => 'text' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $dataProvider |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
* @throws \ReflectionException |
25
|
|
|
*/ |
26
|
2 |
|
public function convertToMapping(string $dataProvider): array |
27
|
|
|
{ |
28
|
2 |
|
$reflector = new \ReflectionClass($dataProvider); |
29
|
2 |
|
$method = $reflector->getMethod('getElements'); |
30
|
2 |
|
$method->setAccessible(true); |
31
|
|
|
|
32
|
2 |
|
$dataProvider = new $dataProvider(); |
33
|
|
|
|
34
|
2 |
|
$configs = $method->invoke($dataProvider); |
35
|
|
|
|
36
|
2 |
|
$mapping = []; |
37
|
|
|
|
38
|
2 |
|
foreach ($configs as $field => $config) { |
39
|
2 |
|
$data = []; |
40
|
|
|
|
41
|
2 |
|
$data = $this->addType($data, $config); |
42
|
2 |
|
$data = $this->addProperties($data, $config); |
43
|
|
|
|
44
|
|
|
|
45
|
2 |
|
$mapping[$field] = $data; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
return $mapping; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $data |
53
|
|
|
* @param array $config |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
* @throws \ReflectionException |
57
|
|
|
*/ |
58
|
2 |
|
protected function addProperties(array $data, array $config): array |
59
|
|
|
{ |
60
|
2 |
|
if ($config['is_dataprovider']) { |
61
|
2 |
|
$data['properties'] = $this->convertToMapping( |
62
|
2 |
|
$config['type'] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return $data; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $data |
71
|
|
|
* @param array $config |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
2 |
|
protected function addType(array $data, array $config): array |
76
|
|
|
{ |
77
|
2 |
|
$originalType = $config['type']; |
78
|
|
|
|
79
|
2 |
|
if (array_key_exists($originalType, $this->typeMapping)) { |
80
|
2 |
|
$data['type'] = $this->typeMapping[$originalType]; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if ($config['is_dataprovider']) { |
84
|
2 |
|
$data['type'] = 'object'; |
85
|
|
|
} |
86
|
2 |
|
if ($config['is_collection']) { |
87
|
2 |
|
$data['type'] = 'nested'; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $data; |
91
|
|
|
} |
92
|
|
|
} |