1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Manganel; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
13
|
|
|
use Maslosoft\Mangan\Helpers\CollectionNamer; |
14
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface; |
15
|
|
|
use Maslosoft\Mangan\Traits\CriteriaAwareTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* QueryBuilder |
19
|
|
|
* @method SearchCriteria getCriteria() |
20
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
21
|
|
|
*/ |
22
|
|
|
class QueryBuilder implements CriteriaAwareInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
use CriteriaAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Manganel instance |
29
|
|
|
* @var Manganel |
30
|
|
|
*/ |
31
|
|
|
private $manganel = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Annotated model |
35
|
|
|
* @var AnnotatedInterface |
36
|
|
|
*/ |
37
|
|
|
private $model; |
38
|
|
|
|
39
|
7 |
|
public function __construct($model) |
40
|
|
|
{ |
41
|
7 |
|
$this->model = $model; |
42
|
7 |
|
$this->manganel = Manganel::create($this->model); |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @param string $q |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
6 |
|
public function count($q = null) |
51
|
|
|
{ |
52
|
6 |
|
$params = $this->getParams($q); |
53
|
6 |
|
$result = $this->manganel->getClient()->count($params); |
54
|
6 |
|
if (empty($result) && empty($result['count'])) |
55
|
|
|
{ |
56
|
|
|
return 0; // @codeCoverageIgnore |
57
|
|
|
} |
58
|
6 |
|
return $result['count']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get search results |
63
|
|
|
* @param string $q |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
5 |
|
public function search($q = null) |
67
|
|
|
{ |
68
|
5 |
|
$params = $this->getParams($q); |
69
|
5 |
|
$result = $this->manganel->getClient()->search($params); |
70
|
5 |
|
if (empty($result) && empty($result['hits']) && empty($result['hits']['hits'])) |
71
|
|
|
{ |
72
|
|
|
return []; // @codeCoverageIgnore |
73
|
|
|
} |
74
|
5 |
|
return $result['hits']['hits']; |
75
|
|
|
} |
76
|
|
|
|
77
|
7 |
|
private function getParams($q = null) |
78
|
|
|
{ |
79
|
|
|
// Try to get query from criteria if empty |
80
|
7 |
|
$criteria = $this->getCriteria(); |
81
|
7 |
|
if (null === $q && !empty($criteria)) |
82
|
|
|
{ |
83
|
|
|
$q = $criteria->getSearch(); |
84
|
|
|
} |
85
|
|
|
|
86
|
7 |
|
if (null === $q) |
87
|
|
|
{ |
88
|
|
|
// Match all documents if query is null |
89
|
|
|
$query = [ |
90
|
|
|
'match_all' => [] |
91
|
1 |
|
]; |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
// Use query string matching |
96
|
|
|
$query = [ |
97
|
|
|
'query_string' => [ |
98
|
6 |
|
'query' => $q |
99
|
|
|
] |
100
|
|
|
]; |
101
|
|
|
} |
102
|
7 |
|
$body['query'] = $query; |
|
|
|
|
103
|
|
|
|
104
|
7 |
|
if (!empty($criteria)) |
105
|
|
|
{ |
106
|
4 |
|
if ($criteria->getLimit() || $criteria->getOffset()) |
107
|
|
|
{ |
108
|
4 |
|
$body['from'] = $criteria->getOffset(); |
109
|
4 |
|
$body['size'] = $criteria->getLimit(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$params = [ |
114
|
7 |
|
'index' => strtolower($this->manganel->index), |
115
|
7 |
|
'type' => CollectionNamer::nameCollection($this->model), |
116
|
7 |
|
'body' => $body |
117
|
|
|
]; |
118
|
7 |
|
return $params; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.