Completed
Push — master ( aee63e...997989 )
by Nicolas
02:11
created

Mapping::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica;
4
5
use Elastica\Exception\InvalidException;
6
use Elasticsearch\Endpoints\Indices\Mapping\Put;
7
8
/**
9
 * Elastica Mapping object.
10
 *
11
 * @author Nicolas Ruflin <[email protected]>
12
 *
13
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
14
 */
15
class Mapping
16
{
17
    /**
18
     * Mapping.
19
     *
20
     * @var array Mapping
21
     */
22
    protected $_mapping = [];
23
24
    /**
25
     * Construct Mapping.
26
     *
27
     * @param array $properties OPTIONAL Properties
28
     */
29
    public function __construct(array $properties = [])
30
    {
31
        if (!empty($properties)) {
32
            $this->setProperties($properties);
33
        }
34
    }
35
36
    /**
37
     * Sets the mapping properties.
38
     *
39
     * @param array $properties Properties
40
     *
41
     * @return $this
42
     */
43
    public function setProperties(array $properties): Mapping
44
    {
45
        return $this->setParam('properties', $properties);
46
    }
47
48
    /**
49
     * Gets the mapping properties.
50
     *
51
     * @return array $properties Properties
52
     */
53
    public function getProperties(): array
54
    {
55
        return $this->getParam('properties');
56
    }
57
58
    /**
59
     * Sets the mapping _meta.
60
     *
61
     * @param array $meta metadata
62
     *
63
     * @return $this
64
     *
65
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-meta.html
66
     */
67
    public function setMeta(array $meta): Mapping
68
    {
69
        return $this->setParam('_meta', $meta);
70
    }
71
72
    /**
73
     * Sets source values.
74
     *
75
     * To disable source, argument is
76
     * array('enabled' => false)
77
     *
78
     * @param array $source Source array
79
     *
80
     * @return $this
81
     *
82
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html
83
     */
84
    public function setSource(array $source): Mapping
85
    {
86
        return $this->setParam('_source', $source);
87
    }
88
89
    /**
90
     * Disables the source in the index.
91
     *
92
     * Param can be set to true to enable again
93
     *
94
     * @param bool $enabled OPTIONAL (default = false)
95
     *
96
     * @return $this
97
     */
98
    public function disableSource(bool $enabled = false): Mapping
99
    {
100
        return $this->setSource(['enabled' => $enabled]);
101
    }
102
103
    /**
104
     * Sets raw parameters.
105
     *
106
     * Possible options:
107
     * _uid
108
     * _id
109
     * _type
110
     * _source
111
     * _analyzer
112
     * _boost
113
     * _routing
114
     * _index
115
     * _size
116
     * properties
117
     *
118
     * @param string $key   Key name
119
     * @param mixed  $value Key value
120
     *
121
     * @return $this
122
     */
123
    public function setParam(string $key, $value): Mapping
124
    {
125
        $this->_mapping[$key] = $value;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Get raw parameters.
132
     *
133
     * @see setParam
134
     *
135
     * @param string $key Key name
136
     *
137
     * @return mixed $value Key value
138
     */
139
    public function getParam(string $key)
140
    {
141
        return $this->_mapping[$key] ?? null;
142
    }
143
144
    /**
145
     * Converts the mapping to an array.
146
     *
147
     * @throws InvalidException
148
     *
149
     * @return array Mapping as array
150
     */
151
    public function toArray(): array
152
    {
153
        return $this->_mapping;
154
    }
155
156
    /**
157
     * Submits the mapping and sends it to the server.
158
     *
159
     * @param Index $index the index to send the mappings to
160
     * @param array $query Query string parameters to send with mapping
161
     *
162
     * @return Response Response object
163
     */
164
    public function send(Index $index, array $query = []): Response
165
    {
166
        $endpoint = new Put();
167
        $endpoint->setBody($this->toArray());
168
        $endpoint->setParams($query);
169
170
        return $index->requestEndpoint($endpoint);
171
    }
172
173
    /**
174
     * Creates a mapping object.
175
     *
176
     * @param array|Mapping $mapping Mapping object or properties array
177
     *
178
     * @throws InvalidException If invalid type
179
     *
180
     * @return self
181
     */
182
    public static function create($mapping): Mapping
183
    {
184
        if (\is_array($mapping)) {
185
            $mappingObject = new static();
186
            $mappingObject->setProperties($mapping);
187
188
            return $mappingObject;
189
        }
190
191
        if ($mapping instanceof self) {
192
            return $mapping;
193
        }
194
195
        throw new InvalidException('Invalid object type');
196
    }
197
}
198