1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Isswp101\Persimmon\Elasticsearch\DocumentPath; |
7
|
|
|
use Isswp101\Persimmon\Elasticsearch\InnerHits; |
8
|
|
|
use Isswp101\Persimmon\Elasticsearch\Response; |
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 \Exception |
68
|
|
|
*/ |
69
|
|
|
final protected function validateIndexAndType() |
70
|
|
|
{ |
71
|
|
|
if (!$this->getIndex()) { |
72
|
|
|
throw new Exception('Please specify the index for your Elasticsearch model'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!$this->getType()) { |
76
|
|
|
throw new Exception('Please specify the type for your Elasticsearch model'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param InnerHits $innerHits |
82
|
|
|
*/ |
83
|
|
|
protected function setInnerHits(InnerHits $innerHits) |
84
|
|
|
{ |
85
|
|
|
$this->_innerHits = $innerHits; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return InnerHits |
90
|
|
|
*/ |
91
|
|
|
protected function getInnerHits() |
92
|
|
|
{ |
93
|
|
|
return $this->_innerHits; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $response |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function fillByResponse(array $response) |
101
|
|
|
{ |
102
|
|
|
$res = new Response($response); |
103
|
|
|
$this->fill($res->getSource()); |
|
|
|
|
104
|
|
|
$this->setId($res->getId()); |
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $response |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function fillByInnerHits(array $response) |
113
|
|
|
{ |
114
|
|
|
$innerHits = new InnerHits($response); |
115
|
|
|
$this->setInnerHits($innerHits); |
116
|
|
|
$this->setParentId($innerHits->getParentId($this->getParentType())); |
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getPath() |
121
|
|
|
{ |
122
|
|
|
return new DocumentPath($this->getIndex(), $this->getType(), $this->getId()); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
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.