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
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
public function __construct(Client $client) |
23
|
|
|
{ |
24
|
|
|
$this->client = $client; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function instantiate(string $class): Storable |
28
|
|
|
{ |
29
|
|
|
$instance = new $class; |
30
|
|
|
if (!$instance instanceof IElasticsearchModel) { |
31
|
|
|
throw new ClassTypeErrorException(IElasticsearchModel::class); |
32
|
|
|
} |
33
|
|
|
return $instance; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function fill(Storable $model, ElasticsearchItemResponse $response) |
37
|
|
|
{ |
38
|
|
|
$model->fill($response->source()); |
39
|
|
|
$model->setPrimaryKey($response->id()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function find($id, string $class, array $columns = []): Storable |
43
|
|
|
{ |
44
|
|
|
$model = $this->instantiate($class); |
45
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
46
|
|
|
$params = [ |
47
|
|
|
'index' => $collection->getIndex(), |
48
|
|
|
'type' => $collection->getType(), |
49
|
|
|
'id' => $id, |
50
|
|
|
'_source' => $columns |
51
|
|
|
]; |
52
|
|
|
try { |
53
|
|
|
$response = new ElasticsearchItemResponse($this->client->get($params)); |
|
|
|
|
54
|
|
|
} catch (Missing404Exception $e) { |
55
|
|
|
throw new ModelNotFoundException($class, $id); |
56
|
|
|
} |
57
|
|
|
$this->fill($model, $response); |
58
|
|
|
return $model; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): IElasticsearchCollection |
62
|
|
|
{ |
63
|
|
|
$model = $this->instantiate($class); |
64
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
65
|
|
|
$params = [ |
66
|
|
|
'index' => $collection->getIndex(), |
67
|
|
|
'type' => $collection->getType(), |
68
|
|
|
'body' => $query->build() |
69
|
|
|
]; |
70
|
|
|
$response = new ElasticsearchCollectionResponse($this->client->search($params)); |
|
|
|
|
71
|
|
|
$models = new ElasticsearchCollection([], $response); |
72
|
|
|
foreach ($response->hits() as $hit) { |
73
|
|
|
$model = $this->instantiate($class); |
74
|
|
|
$this->fill($model, new ElasticsearchItemResponse($hit)); |
75
|
|
|
if ($callback != null) { |
76
|
|
|
$callback($model); |
77
|
|
|
} |
78
|
|
|
$models->put($model->getPrimaryKey(), $model); |
79
|
|
|
} |
80
|
|
|
return $models; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function insert(Storable $model) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
86
|
|
|
$params = [ |
87
|
|
|
'index' => $collection->getIndex(), |
88
|
|
|
'type' => $collection->getType(), |
89
|
|
|
'id' => $model->getPrimaryKey(), |
90
|
|
|
'body' => $model->toArray() |
91
|
|
|
]; |
92
|
|
|
$this->client->index($params); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function update(Storable $model) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
98
|
|
|
$params = [ |
99
|
|
|
'index' => $collection->getIndex(), |
100
|
|
|
'type' => $collection->getType(), |
101
|
|
|
'id' => $model->getPrimaryKey(), |
102
|
|
|
'body' => $model->toArray() |
103
|
|
|
]; |
104
|
|
|
$this->client->index($params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function delete(Storable $model) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollectionName()); |
110
|
|
|
$params = [ |
111
|
|
|
'index' => $collection->getIndex(), |
112
|
|
|
'type' => $collection->getType(), |
113
|
|
|
'id' => $model->getPrimaryKey() |
114
|
|
|
]; |
115
|
|
|
$this->client->delete($params); |
116
|
|
|
} |
117
|
|
|
} |
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: