1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Mapping; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
class Mapping implements MappingInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $type; |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $parameters = []; |
21
|
|
|
/** |
22
|
|
|
* @var MappingInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $properties = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $type |
28
|
|
|
*/ |
29
|
|
|
public function __construct(string $type = null) |
30
|
|
|
{ |
31
|
|
|
$this->type = $type; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getType(): ?string |
38
|
|
|
{ |
39
|
|
|
return $this->type; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $type |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function setType(string $type) |
47
|
|
|
{ |
48
|
|
|
$this->type = $type; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getParameters(): array |
56
|
|
|
{ |
57
|
|
|
return $this->parameters; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $parameters |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setParameters(array $parameters) |
65
|
|
|
{ |
66
|
|
|
$this->parameters = $parameters; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setParameter(string $parameter, $value) |
71
|
|
|
{ |
72
|
|
|
$this->parameters[$parameter] = $value; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return MappingInterface[] |
78
|
|
|
*/ |
79
|
|
|
public function getProperties(): array |
80
|
|
|
{ |
81
|
|
|
return $this->properties; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param MappingInterface[] $properties |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setProperties(array $properties) |
89
|
|
|
{ |
90
|
|
|
$this->properties = $properties; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setProperty(string $property, MappingInterface $mapping) |
95
|
|
|
{ |
96
|
|
|
$this->properties[$property] = $mapping; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function asObject(MappingInterface $source) |
100
|
|
|
{ |
101
|
|
|
$result = new stdClass(); |
102
|
|
|
foreach ($source->getParameters() as $parameter => $value) { |
103
|
|
|
$result->$parameter = $value; |
104
|
|
|
} |
105
|
|
|
if (!empty($source->getProperties())) { |
106
|
|
|
$properties = new stdClass(); |
107
|
|
|
foreach ($source->getProperties() as $property => $mapping) { |
108
|
|
|
$properties->$property = static::asObject($mapping); |
109
|
|
|
} |
110
|
|
|
$result->properties = $properties; |
111
|
|
|
} |
112
|
|
|
if ($source->getType() && $source->getType() !== RootType::ID) { |
|
|
|
|
113
|
|
|
$result->type = $source->getType(); |
114
|
|
|
} |
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function asArray(MappingInterface $source): array |
119
|
|
|
{ |
120
|
|
|
$result = []; |
121
|
|
|
foreach ($source->getParameters() as $parameter => $value) { |
122
|
|
|
$result[$parameter] = $value; |
123
|
|
|
} |
124
|
|
|
if (!empty($source->getProperties())) { |
125
|
|
|
$properties = []; |
126
|
|
|
foreach ($source->getProperties() as $property => $mapping) { |
127
|
|
|
$properties[$property] = static::asArray($mapping); |
128
|
|
|
} |
129
|
|
|
$result['properties'] = $properties; |
130
|
|
|
} |
131
|
|
|
if ($source->getType() && $source->getType() !== RootType::ID) { |
|
|
|
|
132
|
|
|
$result['type'] = $source->getType(); |
133
|
|
|
} |
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: