Completed
Push — master ( b42d84...ac0b69 )
by Nicolas
02:23
created

Mapping::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace Elastica\Type;
3
4
use Elastica\Exception\InvalidException;
5
use Elastica\Type;
6
use Elasticsearch\Endpoints\Indices\Mapping\Put;
7
8
/**
9
 * Elastica Mapping object.
10
 *
11
 * @author Nicolas Ruflin <[email protected]>
12
 *
13
 * @link 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
     * Type.
26
     *
27
     * @var \Elastica\Type Type object
28
     */
29
    protected $_type;
30
31
    /**
32
     * Construct Mapping.
33
     *
34
     * @param \Elastica\Type $type       OPTIONAL Type object
35
     * @param array          $properties OPTIONAL Properties
36
     */
37
    public function __construct(Type $type = null, array $properties = [])
38
    {
39
        if ($type) {
40
            $this->setType($type);
41
        }
42
43
        if (!empty($properties)) {
44
            $this->setProperties($properties);
45
        }
46
    }
47
48
    /**
49
     * Sets the mapping type
50
     * Enter description here ...
51
     *
52
     * @param \Elastica\Type $type Type object
53
     *
54
     * @return $this
55
     */
56
    public function setType(Type $type)
57
    {
58
        $this->_type = $type;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Sets the mapping properties.
65
     *
66
     * @param array $properties Properties
67
     *
68
     * @return $this
69
     */
70
    public function setProperties(array $properties)
71
    {
72
        return $this->setParam('properties', $properties);
73
    }
74
75
    /**
76
     * Gets the mapping properties.
77
     *
78
     * @return array $properties Properties
79
     */
80
    public function getProperties()
81
    {
82
        return $this->getParam('properties');
83
    }
84
85
    /**
86
     * Sets the mapping _meta.
87
     *
88
     * @param array $meta metadata
89
     *
90
     * @return $this
91
     *
92
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-meta.html
93
     */
94
    public function setMeta(array $meta)
95
    {
96
        return $this->setParam('_meta', $meta);
97
    }
98
99
    /**
100
     * Returns mapping type.
101
     *
102
     * @return \Elastica\Type Type
103
     */
104
    public function getType()
105
    {
106
        return $this->_type;
107
    }
108
109
    /**
110
     * Sets source values.
111
     *
112
     * To disable source, argument is
113
     * array('enabled' => false)
114
     *
115
     * @param array $source Source array
116
     *
117
     * @return $this
118
     *
119
     * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html
120
     */
121
    public function setSource(array $source)
122
    {
123
        return $this->setParam('_source', $source);
124
    }
125
126
    /**
127
     * Disables the source in the index.
128
     *
129
     * Param can be set to true to enable again
130
     *
131
     * @param bool $enabled OPTIONAL (default = false)
132
     *
133
     * @return $this
134
     */
135
    public function disableSource($enabled = false)
136
    {
137
        return $this->setSource(['enabled' => $enabled]);
138
    }
139
140
    /**
141
     * Sets raw parameters.
142
     *
143
     * Possible options:
144
     * _uid
145
     * _id
146
     * _type
147
     * _source
148
     * _analyzer
149
     * _boost
150
     * _parent
151
     * _routing
152
     * _index
153
     * _size
154
     * properties
155
     *
156
     * @param string $key   Key name
157
     * @param mixed  $value Key value
158
     *
159
     * @return $this
160
     */
161
    public function setParam($key, $value)
162
    {
163
        $this->_mapping[$key] = $value;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Get raw parameters.
170
     *
171
     * @see setParam
172
     *
173
     * @param string $key Key name
174
     *
175
     * @return mixed $value Key value
176
     */
177
    public function getParam($key)
178
    {
179
        return isset($this->_mapping[$key]) ? $this->_mapping[$key] : null;
180
    }
181
182
    /**
183
     * Set parent type.
184
     *
185
     * @param string $type Parent type
186
     *
187
     * @return $this
188
     */
189
    public function setParent($type)
190
    {
191
        return $this->setParam('_parent', ['type' => $type]);
192
    }
193
194
    /**
195
     * Converts the mapping to an array.
196
     *
197
     * @throws \Elastica\Exception\InvalidException
198
     *
199
     * @return array Mapping as array
200
     */
201
    public function toArray()
202
    {
203
        $type = $this->getType();
204
205
        if (empty($type)) {
206
            throw new InvalidException('Type has to be set');
207
        }
208
209
        return [$type->getName() => $this->_mapping];
210
    }
211
212
    /**
213
     * Submits the mapping and sends it to the server.
214
     *
215
     * @param array $query Query string parameters to send with mapping
216
     *
217
     * @return \Elastica\Response Response object
218
     */
219
    public function send(array $query = [])
220
    {
221
        $endpoint = new Put();
222
        $endpoint->setBody($this->toArray());
223
        $endpoint->setParams($query);
224
225
        return $this->getType()->requestEndpoint($endpoint);
226
    }
227
228
    /**
229
     * Creates a mapping object.
230
     *
231
     * @param array|\Elastica\Type\Mapping $mapping Mapping object or properties array
232
     *
233
     * @throws \Elastica\Exception\InvalidException If invalid type
234
     *
235
     * @return self
236
     */
237
    public static function create($mapping)
238
    {
239
        if (is_array($mapping)) {
240
            $mappingObject = new self();
241
            $mappingObject->setProperties($mapping);
242
243
            return $mappingObject;
244
        }
245
246
        if ($mapping instanceof self) {
247
            return $mapping;
248
        }
249
250
        throw new InvalidException('Invalid object type');
251
    }
252
}
253