-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathModel.php
299 lines (268 loc) · 8.44 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Copyright (c) 2015, Muhannad Shelleh
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace ItvisionSy\EsMapper;
use Elasticsearch\Client;
use Exception;
/**
* A document model class returned from ES find/query methods
*
* @package ItvisionSy\EsMapper
* @author Muhannad Shelleh <[email protected]>
*
*/
class Model implements IModel {
/**
* The raw hit element array returned from ES
* @var array
*/
protected $esHitData;
/**
* The es client object
* @var Client
*/
protected $esClient;
/**
* Meta information about the model
* @var array contains the following (not always):
* @var mixed id The id of the document.
* @var string type The type of the document.
* @var string index The index of the document.
* @var float score The score of the hit.
*/
protected $meta = [];
/**
* The data array from the raw
* @var array
*/
protected $attributes = [];
/**
* A custom factory method that will try to detect the correct model class
* and instantiate and object of it.
*
* @param array $esHit
* @param string $className a class name or pattern.
* @param Client $esClient ElasticSearch client
* @return Model
*/
public static function MakeOfType(array $esHit, $className = "", Client $esClient = null) {
if ($className && strpos($className, '{type}') !== false) {
$baseClassName = str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9]+/', ' ', $esHit['_type'])));
$fullClassName = str_replace("{type}", $baseClassName, $className);
if (!class_exists($fullClassName)) {
$fullClassName = array_key_exists('_source', $esHit) ? static::class : ArrayObject::class;
}
} else {
$fullClassName = $className? : static::class;
}
return $fullClassName::make($esHit, $esClient);
}
/**
* A factory method to create a new class object for the provided hit data.
*
* @param array $esHit
* @param Client $esClient ElasticSearch client
* @return Model
*/
public static function make(array $esHit, Client $esClient = null) {
return new static($esHit, $esClient);
}
/**
* Constructor method
*
* @param array $esHit
* @param Client $esClient ElasticSearch client
*/
public function __construct(array $esHit, Client $esClient = null) {
$this->esHitData = $esHit;
$this->esClient = $esClient;
$source = array_key_exists("_source", $esHit) ? "_source" : "fields";
$this->attributes = $esHit[$source];
unset($esHit[$source]);
foreach ($esHit as $key => $value) {
$key = trim($key, "_");
$this->meta[$key] = $value;
}
}
public function __get($name) {
if (substr($name, 0, 2) == '__') {
$tName = substr($name, 1);
return property_exists($this, $tName) ? $this->$tName : ($this->offsetExists($name) ? $this->attributes[$name] : (array_key_exists($tName, $this->meta) ? $this->meta[$tName] : null));
} elseif (substr($name, 0, 1) == '_') {
$tName = substr($name, 1);
return array_key_exists($tName, $this->meta) ? $this->meta[$tName] : ($this->offsetExists($name) ? $this->attributes[$name] : (property_exists($this, $tName) ? $this->$tName : null));
} else {
return $this->offsetExists($name) ? $this->attributes[$name] : (array_key_exists($name, $this->meta) ? $this->meta[$name] : (property_exists($this, $name) ? $this->$name : null));
}
}
public function __set($name, $value) {
$this->offsetSet($name, $value);
}
/**
* Returns true if there is an attribute named $offset, false otherwise
*
* @param string $offset
* @return boolean
*/
public function offsetExists($offset) {
return array_key_exists($offset, $this->attributes);
}
/**
* Returns the attribute specified in $offset.
*
* @param string $offset
* @return mixed
*/
public function offsetGet($offset) {
return @$this->attributes[$offset];
}
/**
* Sets the attribute name of the $offset
*
* @param string $offset
* @param mixed $value
* @return Model
*/
public function offsetSet($offset, $value) {
$this->attributes[$offset] = $value;
return $this;
}
public function canAlter() {
if (!array_key_exists('id', $this->meta)) {
return false;
}
if (!array_key_exists('type', $this->meta)) {
return false;
}
if (!array_key_exists('index', $this->meta)) {
return false;
}
return true;
}
/**
* Sets elasticsearch client
* @param Client $esClient
*/
public function setElasticClient(Client $esClient) {
$this->esClient = $esClient;
}
/**
* Returns elastic hit data (the original data provided).
*
* @return array
*/
public function getEsHitData() {
return $this->esHitData;
}
/**
* Return attributes (actual document data)
* @return array
*/
public function getAttributes() {
$values = func_get_args();
if (count($values) === 1) {
$result = [];
foreach ($values as $value) {
$result[$value] = @$this->attributes[$value];
}
return $result;
} elseif (count($values) > 1) {
return @$this->attributes[$values[0]];
} else {
return $this->attributes;
}
}
/**
* Return meta data
* @return array
*/
public function getMeta($key = null) {
if ($key) {
return $this->meta[$key];
}
return $this->meta;
}
/**
* ElasticSearch Client
* @return Client|null
*/
public function getElasticClient() {
return $this->esClient;
}
/**
* Updates the document
*
* @param array $data the normal elastic update parameters to be used
* @param array $parameters the parameters array
* @return array
* @throws Exception
*/
public function update(array $data, array $parameters = []) {
if (!$this->canAlter()) {
throw new Exception('Need index, type, and key to update a document');
}
if (!$this->esClient) {
throw new Exception('Need ElasticSearch client object for ALTER operations');
}
$params = [
'index' => $this->meta['index'],
'type' => $this->meta['type'],
'id' => $this->meta['id'],
'body' => $data] + $parameters;
return $this->esClient->update($params);
}
/**
* Deletes a document
*
* @param array $parameters
* @return array
* @throws Exception
*/
public function delete(array $parameters = []) {
if (!$this->canAlter()) {
throw new Exception('Need index, type, and key to delete a document');
}
if (!$this->esClient) {
throw new Exception('Need ElasticSearch client object for ALTER operations');
}
$params = [
'index' => $this->meta['index'],
'type' => $this->meta['type'],
'id' => $this->meta['id']] + $parameters;
$result = $this->esClient->delete($params);
return $result;
}
/**
*
* @param type $offset
* @return Model
*/
public function offsetUnset($offset) {
unset($this->attributes[$offset]);
return $this;
}
public function current() {
return current($this->attributes);
}
public function key() {
return key($this->attributes);
}
public function next() {
return next($this->attributes);
}
public function rewind() {
return rewind($this->attributes);
}
public function valid() {
return valid($this->attributes);
}
}