1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @author Mihkel Viilveer <[email protected]> |
5
|
|
|
* @date 9.09.2014 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace opus\elastic\spooler; |
9
|
|
|
|
10
|
|
|
use opus\elastic\enum\ElasticFieldType; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Interface AbstractMappingProvider |
14
|
|
|
* |
15
|
|
|
* @package opus\elastic\spooler |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractMappingProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Creates mapping and returns it |
21
|
|
|
* @param array $languages |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
abstract public function build(array $languages); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Processes mapping attributes data |
28
|
|
|
* Formats them to correct format |
29
|
|
|
* @param array $productAttributesData |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function processMappingAttributesData(array $productAttributesData) |
34
|
|
|
{ |
35
|
|
|
$formattedAttributes = []; |
36
|
|
|
foreach ($productAttributesData as $name => $options) { |
37
|
|
|
$options = !is_array($options) ? [$options] : $options; |
38
|
|
|
$formattedAttributes[$name] = $this->getAttributeProperties($options); |
39
|
|
|
} |
40
|
|
|
return $formattedAttributes; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets attribute properties |
45
|
|
|
* Recursive if index type is object or nested |
46
|
|
|
* |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
private function getAttributeProperties(array $options) |
52
|
|
|
{ |
53
|
|
|
$attributeProperties = $this->resolveProperties($options[0]); |
54
|
|
|
if (in_array($options[0], [ElasticFieldType::OBJECT, ElasticFieldType::NESTED])) { |
55
|
|
|
$attributeProperties['properties'] = $this->processMappingAttributesData($options['properties']); |
56
|
|
|
} |
57
|
|
|
return $attributeProperties; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $type |
62
|
|
|
*/ |
63
|
|
|
public function resolveProperties($type) |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* @var $resolver TypeResolver |
67
|
|
|
*/ |
68
|
|
|
$resolver = \Yii::createObject('opus\elastic\spooler\TypeResolver'); |
69
|
|
|
return $resolver->resolve($type); |
70
|
|
|
} |
71
|
|
|
} |