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