1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Mapping\Type\Infrastructure; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\TypeInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\BinaryType; |
9
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\BooleanType; |
10
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\ByteType; |
11
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\DateRangeType; |
12
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\DateType; |
13
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\DoubleRangeType; |
14
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\DoubleType; |
15
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\FloatRangeType; |
16
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\FloatType; |
17
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\GeoPointType; |
18
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\GeoShapeType; |
19
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\HalfFloatType; |
20
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\IntegerRangeType; |
21
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\IntegerType; |
22
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\IpRangeType; |
23
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\IpType; |
24
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\JoinType; |
25
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\KeywordType; |
26
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\LongRangeType; |
27
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\LongType; |
28
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\NestedType; |
29
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\ObjectType; |
30
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\PercolatorType; |
31
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
32
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\ScaledFloatType; |
33
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\ShortType; |
34
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\TextType; |
35
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\TokenCountType; |
36
|
|
|
use BadMethodCallException; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
40
|
|
|
*/ |
41
|
|
|
class Registry |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var TypeInterface[] |
45
|
|
|
*/ |
46
|
|
|
private $idIndex = []; |
47
|
|
|
/** |
48
|
|
|
* @var TypeInterface[] |
49
|
|
|
*/ |
50
|
|
|
private $friendlyIdIndex = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $id |
54
|
|
|
* @return TypeInterface|null |
55
|
|
|
*/ |
56
|
|
|
public function find(string $id): ?TypeInterface |
57
|
|
|
{ |
58
|
|
|
if (isset($this->friendlyIdIndex[$id])) { |
59
|
|
|
return $this->friendlyIdIndex[$id]; |
60
|
|
|
} |
61
|
|
|
if (isset($this->idIndex[$id])) { |
62
|
|
|
return $this->idIndex[$id]; |
63
|
|
|
} |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function get(string $id): TypeInterface |
68
|
|
|
{ |
69
|
|
|
$type = $this->find($id); |
70
|
|
|
if ($type) { |
71
|
|
|
return $type; |
72
|
|
|
} |
73
|
|
|
throw new BadMethodCallException("Unknown mapping type `$id`"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function register(TypeInterface $type): Registry |
77
|
|
|
{ |
78
|
|
|
$this->idIndex[$type->getId()] = $type; |
79
|
|
|
$this->friendlyIdIndex[$type->getFriendlyId()] = $type; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return TypeInterface[] |
85
|
|
|
*/ |
86
|
|
|
public function getAll(): array |
87
|
|
|
{ |
88
|
|
|
return array_values($this->idIndex); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function withDefaultTypes(): Registry |
92
|
|
|
{ |
93
|
|
|
// TODO: use code generation |
94
|
|
|
$types = [ |
95
|
|
|
BinaryType::getInstance(), |
96
|
|
|
BooleanType::getInstance(), |
97
|
|
|
ByteType::getInstance(), |
98
|
|
|
DateRangeType::getInstance(), |
99
|
|
|
DateType::getInstance(), |
100
|
|
|
DoubleRangeType::getInstance(), |
101
|
|
|
DoubleType::getInstance(), |
102
|
|
|
FloatRangeType::getInstance(), |
103
|
|
|
FloatType::getInstance(), |
104
|
|
|
GeoPointType::getInstance(), |
105
|
|
|
GeoShapeType::getInstance(), |
106
|
|
|
HalfFloatType::getInstance(), |
107
|
|
|
IntegerRangeType::getInstance(), |
108
|
|
|
IntegerType::getInstance(), |
109
|
|
|
IpRangeType::getInstance(), |
110
|
|
|
IpType::getInstance(), |
111
|
|
|
JoinType::getInstance(), |
112
|
|
|
KeywordType::getInstance(), |
113
|
|
|
LongRangeType::getInstance(), |
114
|
|
|
LongType::getInstance(), |
115
|
|
|
NestedType::getInstance(), |
116
|
|
|
ObjectType::getInstance(), |
117
|
|
|
PercolatorType::getInstance(), |
118
|
|
|
RootType::getInstance(), |
119
|
|
|
ScaledFloatType::getInstance(), |
120
|
|
|
ShortType::getInstance(), |
121
|
|
|
TextType::getInstance(), |
122
|
|
|
TokenCountType::getInstance(), |
123
|
|
|
]; |
124
|
|
|
foreach ($types as $type) { |
125
|
|
|
$this->register($type); |
126
|
|
|
} |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private static $instance; |
131
|
|
|
|
132
|
|
|
public static function getInstance(): Registry |
133
|
|
|
{ |
134
|
|
|
if (!static::$instance) { |
|
|
|
|
135
|
|
|
static::$instance = (new static()) |
136
|
|
|
->withDefaultTypes(); |
137
|
|
|
} |
138
|
|
|
return static::$instance; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|