1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Traits; |
4
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Elasticsearch\DocumentPath; |
6
|
|
|
use Isswp101\Persimmon\Elasticsearch\InnerHits; |
7
|
|
|
use Isswp101\Persimmon\Elasticsearch\Response; |
8
|
|
|
use Isswp101\Persimmon\Exceptions\InvalidModelEndpointException; |
9
|
|
|
|
10
|
|
|
trait Elasticsearchable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected static $index; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected static $type; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $parentType; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var InnerHits |
29
|
|
|
*/ |
30
|
|
|
public $_innerHits; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var float |
34
|
|
|
*/ |
35
|
|
|
public $_score; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
public $_position; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
final public static function getIndex() |
46
|
|
|
{ |
47
|
|
|
return static::$index; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
final public static function getType() |
54
|
|
|
{ |
55
|
|
|
return static::$type; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
final public static function getParentType() |
62
|
|
|
{ |
63
|
|
|
return static::$parentType; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws InvalidModelEndpointException |
68
|
|
|
*/ |
69
|
|
|
final protected function validateModelEndpoint() |
70
|
|
|
{ |
71
|
|
|
if (!$this->getIndex()) { |
72
|
|
|
throw new InvalidModelEndpointException(sprintf( |
73
|
|
|
'Please specify the index for your Elasticsearch model %s', static::class |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$this->getType()) { |
78
|
|
|
throw new InvalidModelEndpointException(sprintf( |
79
|
|
|
'Please specify the type for your Elasticsearch model %s', static::class |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param InnerHits $innerHits |
86
|
|
|
*/ |
87
|
|
|
protected function setInnerHits(InnerHits $innerHits) |
88
|
|
|
{ |
89
|
|
|
$this->_innerHits = $innerHits; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return InnerHits |
94
|
|
|
*/ |
95
|
|
|
protected function getInnerHits() |
96
|
|
|
{ |
97
|
|
|
return $this->_innerHits; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $response |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function fillByResponse(array $response) |
105
|
|
|
{ |
106
|
|
|
$res = new Response($response); |
107
|
|
|
$this->fill($res->getSource()); |
|
|
|
|
108
|
|
|
$this->setId($res->getId()); |
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $response |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function fillByInnerHits(array $response) |
117
|
|
|
{ |
118
|
|
|
$innerHits = new InnerHits($response); |
119
|
|
|
$this->setInnerHits($innerHits); |
120
|
|
|
$this->setParentId($innerHits->getParentId($this->getParentType())); |
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getPath() |
125
|
|
|
{ |
126
|
|
|
return new DocumentPath($this->getIndex(), $this->getType(), $this->getId()); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getPosition() |
130
|
|
|
{ |
131
|
|
|
return $this->_position; |
132
|
|
|
} |
133
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.