|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Mapping; |
|
6
|
|
|
use AmaTeam\ElasticSearch\API\MappingInterface; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
class Operations |
|
10
|
|
|
{ |
|
11
|
|
|
const KEY_PROPERTIES = 'properties'; |
|
12
|
|
|
const KEY_TYPE = 'type'; |
|
13
|
|
|
|
|
14
|
|
|
public static function merge(MappingInterface ...$mappings): Mapping |
|
15
|
|
|
{ |
|
16
|
|
|
$target = new Mapping(); |
|
17
|
|
|
foreach ($mappings as $source) { |
|
18
|
|
|
if ($source->getType()) { |
|
|
|
|
|
|
19
|
|
|
$target->setType($source->getType()); |
|
20
|
|
|
} |
|
21
|
|
|
foreach ($source->getParameters() as $parameter => $value) { |
|
22
|
|
|
$target->setParameter($parameter, $value); |
|
23
|
|
|
} |
|
24
|
|
|
foreach ($source->getProperties() as $property => $mapping) { |
|
25
|
|
|
$candidate = $target->getProperty($property); |
|
26
|
|
|
$target->setProperty($property, $candidate ? static::merge($candidate, $mapping) : $mapping); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
return $target; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function from(MappingInterface $mapping): Mapping |
|
33
|
|
|
{ |
|
34
|
|
|
return static::merge($mapping); |
|
35
|
|
|
} |
|
36
|
|
|
public static function conflict(MappingInterface ...$mappings): bool |
|
37
|
|
|
{ |
|
38
|
|
|
$mappings = array_values($mappings); |
|
39
|
|
|
for ($i = 0; $i < sizeof($mappings) - 1; $i++) { |
|
40
|
|
|
$subject = $mappings[$i]; |
|
41
|
|
|
for ($j = $i + 1; $j < sizeof($mappings); $j++) { |
|
|
|
|
|
|
42
|
|
|
if (static::haveConflicts($subject, $mappings[$j])) { |
|
43
|
|
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private static function haveConflicts(MappingInterface $subject, MappingInterface $opponent): bool |
|
51
|
|
|
{ |
|
52
|
|
|
if (static::haveTypeConflict($subject, $opponent)) { |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
if (static::haveParameterConflicts($subject, $opponent)) { |
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
return static::havePropertyConflicts($subject, $opponent); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private static function haveTypeConflict(MappingInterface $subject, MappingInterface $opponent): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $subject->getType() && $opponent->getType() && $subject->getType() !== $opponent->getType(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private static function haveParameterConflicts(MappingInterface $subject, MappingInterface $opponent): bool |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($subject->getParameters() as $parameter => $value) { |
|
69
|
|
|
if ($opponent->hasParameter($parameter) && $opponent->getParameter($parameter) != $value) { |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private static function havePropertyConflicts(MappingInterface $subject, MappingInterface $opponent): bool |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($subject->getProperties() as $name => $property) { |
|
79
|
|
|
if ($opponent->hasProperty($name) && static::haveConflicts($property, $opponent->getProperty($name))) { |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public static function toArray(MappingInterface $mapping): array |
|
87
|
|
|
{ |
|
88
|
|
|
$target = []; |
|
89
|
|
|
foreach ($mapping->getParameters() as $parameter => $value) { |
|
90
|
|
|
$target[$parameter] = $value; |
|
91
|
|
|
} |
|
92
|
|
View Code Duplication |
if (!empty($mapping->getProperties())) { |
|
|
|
|
|
|
93
|
|
|
$properties = []; |
|
94
|
|
|
foreach ($mapping->getProperties() as $property => $mapping) { |
|
95
|
|
|
$properties[$property] = static::toArray($mapping); |
|
96
|
|
|
} |
|
97
|
|
|
$target[self::KEY_PROPERTIES] = $properties; |
|
98
|
|
|
} |
|
99
|
|
|
if ($mapping->getType()) { |
|
|
|
|
|
|
100
|
|
|
$target[self::KEY_TYPE] = $mapping->getType(); |
|
101
|
|
|
} |
|
102
|
|
|
return $target; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function toStdObject(MappingInterface $mapping) |
|
106
|
|
|
{ |
|
107
|
|
|
$target = new stdClass(); |
|
108
|
|
|
foreach ($mapping->getParameters() as $parameter => $value) { |
|
109
|
|
|
$target->$parameter = $value; |
|
110
|
|
|
} |
|
111
|
|
View Code Duplication |
if (!empty($mapping->getProperties())) { |
|
|
|
|
|
|
112
|
|
|
$properties = []; |
|
113
|
|
|
foreach ($mapping->getProperties() as $name => $property) { |
|
114
|
|
|
$properties[$name] = static::toStdObject($property); |
|
115
|
|
|
} |
|
116
|
|
|
$target->properties = $properties; |
|
117
|
|
|
} |
|
118
|
|
|
if ($mapping->getType()) { |
|
|
|
|
|
|
119
|
|
|
$target->type = $mapping->getType(); |
|
120
|
|
|
} |
|
121
|
|
|
return $target; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public static function fromArray(array $source): Mapping |
|
125
|
|
|
{ |
|
126
|
|
|
$target = new Mapping(); |
|
127
|
|
|
if (isset($source[self::KEY_TYPE])) { |
|
128
|
|
|
$target->setType($source[self::KEY_TYPE]); |
|
129
|
|
|
} |
|
130
|
|
|
if (isset($source[self::KEY_PROPERTIES]) && is_array($source[self::KEY_PROPERTIES])) { |
|
131
|
|
|
$properties = []; |
|
132
|
|
|
foreach ($source[self::KEY_PROPERTIES] as $property => $mapping) { |
|
133
|
|
|
$properties[$property] = static::fromArray($mapping); |
|
134
|
|
|
} |
|
135
|
|
|
$target->setProperties($properties); |
|
136
|
|
|
} |
|
137
|
|
|
$parameters = []; |
|
138
|
|
|
foreach ($source as $parameter => $value) { |
|
139
|
|
|
if (in_array($parameter, [self::KEY_PROPERTIES, self::KEY_TYPE])) { |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
$parameters[$parameter] = $value; |
|
143
|
|
|
} |
|
144
|
|
|
$target->setParameters($parameters); |
|
145
|
|
|
return $target; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: