1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Repository; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
6
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
7
|
|
|
use Isswp101\Persimmon\Collection\ElasticsearchCollection; |
8
|
|
|
use Isswp101\Persimmon\Collection\IElasticsearchCollection; |
9
|
|
|
use Isswp101\Persimmon\CollectionParser\ElasticsearchCollectionParser; |
10
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
11
|
|
|
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException; |
12
|
|
|
use Isswp101\Persimmon\Exceptions\ModelNotFoundException; |
13
|
|
|
use Isswp101\Persimmon\Model\IElasticsearchModel; |
14
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
15
|
|
|
use Isswp101\Persimmon\Response\ElasticsearchCollectionResponse; |
16
|
|
|
use Isswp101\Persimmon\Response\ElasticsearchItemResponse; |
17
|
|
|
|
18
|
|
|
class ElasticsearchRepository implements IRepository |
19
|
|
|
{ |
20
|
|
|
const SOURCE_FALSE = [false]; |
21
|
|
|
|
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
public function __construct(Client $client) |
25
|
|
|
{ |
26
|
|
|
$this->client = $client; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function instantiate(string $class): Storable |
30
|
|
|
{ |
31
|
|
|
$instance = new $class; |
32
|
|
|
if (!$instance instanceof IElasticsearchModel) { |
33
|
|
|
throw new ClassTypeErrorException(IElasticsearchModel::class); |
34
|
|
|
} |
35
|
|
|
return $instance; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function fill(Storable $model, ElasticsearchItemResponse $response) |
39
|
|
|
{ |
40
|
|
|
$model->fill($response->source()); |
41
|
|
|
$model->setPrimaryKey($response->id()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function find($id, string $class, array $columns = []): Storable |
45
|
|
|
{ |
46
|
|
|
$model = $this->instantiate($class); |
47
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
48
|
|
|
$params = [ |
49
|
|
|
'index' => $collection->getIndex(), |
50
|
|
|
'type' => $collection->getType(), |
51
|
|
|
'id' => $id, |
52
|
|
|
'_source' => $columns === ElasticsearchRepository::SOURCE_FALSE ? false : $columns |
53
|
|
|
]; |
54
|
|
|
try { |
55
|
|
|
$response = new ElasticsearchItemResponse($this->client->get($params)); |
|
|
|
|
56
|
|
|
} catch (Missing404Exception $e) { |
57
|
|
|
throw new ModelNotFoundException($class, $id); |
58
|
|
|
} |
59
|
|
|
$this->fill($model, $response); |
60
|
|
|
return $model; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function all( |
64
|
|
|
IQueryBuilder $query, |
65
|
|
|
string $class, |
66
|
|
|
array $columns = [], |
67
|
|
|
callable $callback = null |
68
|
|
|
): IElasticsearchCollection { |
69
|
|
|
$model = $this->instantiate($class); |
70
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
71
|
|
|
$params = [ |
72
|
|
|
'index' => $collection->getIndex(), |
73
|
|
|
'type' => $collection->getType(), |
74
|
|
|
'body' => $query->build(), |
75
|
|
|
'_source' => $columns === ElasticsearchRepository::SOURCE_FALSE ? false : $columns |
76
|
|
|
]; |
77
|
|
|
$response = new ElasticsearchCollectionResponse($this->client->search($params)); |
|
|
|
|
78
|
|
|
$models = new ElasticsearchCollection([], $response); |
79
|
|
|
foreach ($response->hits() as $hit) { |
80
|
|
|
$model = $this->instantiate($class); |
81
|
|
|
$this->fill($model, new ElasticsearchItemResponse($hit)); |
82
|
|
|
if ($callback != null) { |
83
|
|
|
$callback($model); |
84
|
|
|
} |
85
|
|
|
$models->put($model->getPrimaryKey(), $model); |
86
|
|
|
} |
87
|
|
|
return $models; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function insert(Storable $model) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
93
|
|
|
$params = [ |
94
|
|
|
'index' => $collection->getIndex(), |
95
|
|
|
'type' => $collection->getType(), |
96
|
|
|
'id' => $model->getPrimaryKey(), |
97
|
|
|
'body' => $model->toArray() |
98
|
|
|
]; |
99
|
|
|
$this->client->index($params); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function update(Storable $model) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
105
|
|
|
$params = [ |
106
|
|
|
'index' => $collection->getIndex(), |
107
|
|
|
'type' => $collection->getType(), |
108
|
|
|
'id' => $model->getPrimaryKey(), |
109
|
|
|
'body' => $model->toArray() |
110
|
|
|
]; |
111
|
|
|
$this->client->index($params); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
public function delete(Storable $model) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
117
|
|
|
$params = [ |
118
|
|
|
'index' => $collection->getIndex(), |
119
|
|
|
'type' => $collection->getType(), |
120
|
|
|
'id' => $model->getPrimaryKey() |
121
|
|
|
]; |
122
|
|
|
$this->client->delete($params); |
123
|
|
|
} |
124
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: