1
|
|
|
<?php |
2
|
|
|
namespace Fathomminds\Rest\Database\Clusterpoint; |
3
|
|
|
|
4
|
|
|
use Fathomminds\Rest\Exceptions\RestException; |
5
|
|
|
use Fathomminds\Rest\Database\Finder as BaseFinder; |
6
|
|
|
use Clusterpoint\Client; |
7
|
|
|
|
8
|
|
|
class Finder extends BaseFinder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
12
|
|
|
*/ |
13
|
|
|
protected function setLimit($collection) |
14
|
|
|
{ |
15
|
|
|
if ($this->queryConfiguration->limit !== null) { |
16
|
|
|
$collection->limit($this->queryConfiguration->limit); |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
22
|
|
|
*/ |
23
|
|
|
protected function setOffset($collection) |
24
|
|
|
{ |
25
|
|
|
if ($this->queryConfiguration->offset !== null) { |
26
|
|
|
$collection->offset($this->queryConfiguration->offset); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
32
|
|
|
*/ |
33
|
|
|
protected function setOrderBy($collection) |
34
|
|
|
{ |
35
|
|
|
foreach ($this->queryConfiguration->orderBy as $orderBy) { |
36
|
|
|
$collection->orderBy(key($orderBy), current($orderBy)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
42
|
|
|
*/ |
43
|
|
|
protected function setSelect($collection) |
44
|
|
|
{ |
45
|
|
|
if ($this->queryConfiguration->select !== '*') { |
46
|
|
|
$collection->select($this->queryConfiguration->select); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
52
|
|
|
*/ |
53
|
|
|
protected function setWhere($collection) |
54
|
|
|
{ |
55
|
|
|
if (!empty($this->queryConfiguration->where)) { |
56
|
|
|
$this->parseWhere($collection, $this->queryConfiguration->where, $this->mainLogical); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
62
|
|
|
*/ |
63
|
|
|
protected function configQuery($collection) |
64
|
|
|
{ |
65
|
|
|
$this->setLimit($collection); |
66
|
|
|
$this->setOffset($collection); |
67
|
|
|
$this->setOrderBy($collection); |
68
|
|
|
$this->setSelect($collection); |
69
|
|
|
$this->setWhere($collection); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function get() |
73
|
|
|
{ |
74
|
|
|
if (!($this->client instanceof Client)) { |
75
|
|
|
throw new RestException( |
76
|
|
|
'Clusterpoint Finder can be used with Clusterpoint Client only', |
77
|
|
|
['type' => get_class($this->client)] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
$collection = $this->client->database( |
81
|
|
|
$this->queryConfiguration->databaseName . |
82
|
|
|
'.' . |
83
|
|
|
$this->queryConfiguration->from |
84
|
|
|
); |
85
|
|
|
$this->configQuery($collection); |
86
|
|
|
$items = json_decode(json_encode($collection->get()->toArray())); |
87
|
|
|
$count = count($items); |
88
|
|
|
for ($idx = 0; $idx < $count; $idx++) { |
89
|
|
|
$this->resultSet[] = $items[$idx]; |
90
|
|
|
} |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function createClient() |
95
|
|
|
{ |
96
|
|
|
$this->client = new Client; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
102
|
|
|
* @param array $conditions @param string $logical |
103
|
|
|
* @return \Closure |
104
|
|
|
*/ |
105
|
|
|
protected function addWhereGroup($collection, $conditions, $logical) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
/** |
108
|
|
|
* @codeCoverageIgnore |
109
|
|
|
* Passing anonymous function to Clusterpoint API |
110
|
|
|
*/ |
111
|
|
|
return function($collection) use ($conditions, $logical) { |
112
|
|
|
$this->parseWhere($collection, $conditions, $logical); |
113
|
|
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $logical |
118
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
119
|
|
|
* @param array $condition |
120
|
|
|
* @param string $nextLogical |
121
|
|
|
*/ |
122
|
|
|
protected function parseWhereGroup($logical, $collection, $condition, $nextLogical) |
123
|
|
|
{ |
124
|
|
|
if ($logical === '||') { |
125
|
|
|
$collection->orWhere($this->addWhereGroup($collection, $condition, $nextLogical)); |
126
|
|
|
} |
127
|
|
|
if ($logical === '&&') { |
128
|
|
|
$collection->where($this->addWhereGroup($collection, $condition, $nextLogical)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \Clusterpoint\Instance\Service $collection |
134
|
|
|
* @param array $conditions |
135
|
|
|
* @param string $logical |
136
|
|
|
*/ |
137
|
|
|
private function parseWhere($collection, $conditions, $logical) |
138
|
|
|
{ |
139
|
|
|
foreach ($conditions as $key => $condition) { |
140
|
|
|
switch (strtoupper($key)) { |
141
|
|
|
case 'AND': |
142
|
|
|
$this->parseWhereGroup($logical, $collection, $condition, '&&'); |
143
|
|
|
break; |
144
|
|
|
case 'OR': |
145
|
|
|
$this->parseWhereGroup($logical, $collection, $condition, '||'); |
146
|
|
|
break; |
147
|
|
|
default: |
148
|
|
|
list($fieldName, $operator, $value) = $condition; |
149
|
|
|
$collection->where($fieldName, $operator, $value, $logical, false); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.