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
|
4 |
|
protected function setLimit($collection) |
11
|
|
|
{ |
12
|
4 |
|
if ($this->limit !== null) { |
13
|
1 |
|
$collection->limit($this->limit); |
14
|
|
|
} |
15
|
4 |
|
} |
16
|
|
|
|
17
|
4 |
|
protected function setOffset($collection) |
18
|
|
|
{ |
19
|
4 |
|
if ($this->offset !== null) { |
20
|
1 |
|
$collection->offset($this->offset); |
21
|
|
|
} |
22
|
4 |
|
} |
23
|
|
|
|
24
|
4 |
|
protected function setOrderBy($collection) |
25
|
|
|
{ |
26
|
4 |
|
foreach ($this->orderBy as $orderBy) { |
27
|
1 |
|
$collection->orderBy(key($orderBy), current($orderBy)); |
28
|
|
|
} |
29
|
4 |
|
} |
30
|
|
|
|
31
|
4 |
|
protected function setSelect($collection) |
32
|
|
|
{ |
33
|
4 |
|
if ($this->select !== '*') { |
34
|
1 |
|
$collection->select($this->select); |
35
|
|
|
} |
36
|
4 |
|
} |
37
|
|
|
|
38
|
4 |
|
public function setWhere($collection) |
39
|
|
|
{ |
40
|
4 |
|
if (!empty($this->where)) { |
41
|
3 |
|
$this->parseWhere($collection, $this->where, $this->mainLogical); |
42
|
|
|
} |
43
|
4 |
|
} |
44
|
|
|
|
45
|
5 |
|
public function get() |
46
|
|
|
{ |
47
|
5 |
|
if (!($this->client instanceof Client)) { |
48
|
1 |
|
throw new RestException( |
49
|
1 |
|
'Clusterpoint Finder can be used with Clusterpoint Client only', |
50
|
1 |
|
['type' => get_class($this->client)] |
51
|
|
|
); |
52
|
|
|
} |
53
|
4 |
|
$collection = $this->client->database($this->databaseName.'.'.$this->from); |
54
|
4 |
|
$this->setLimit($collection); |
55
|
4 |
|
$this->setOffset($collection); |
56
|
4 |
|
$this->setOrderBy($collection); |
57
|
4 |
|
$this->setSelect($collection); |
58
|
4 |
|
$this->setWhere($collection); |
59
|
4 |
|
$items = json_decode(json_encode($collection->get()->toArray())); |
60
|
4 |
|
$c = count($items); |
61
|
4 |
|
for ($i=0; $i<$c; $i++) { |
62
|
4 |
|
$this->resultSet[] = $items[$i]; |
63
|
|
|
} |
64
|
4 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
protected function createClient() |
68
|
|
|
{ |
69
|
1 |
|
$this->client = new Client; |
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function addWhereGroup($collection, $conditions, $logical) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
/** |
76
|
|
|
* @codeCoverageIgnore |
77
|
|
|
* Passing anonymous function to Clusterpoint API |
78
|
|
|
*/ |
79
|
|
|
return function ($collection) use ($conditions, $logical) { |
80
|
|
|
$this->parseWhere($collection, $conditions, $logical); |
81
|
|
|
}; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
protected function parseWhereGroup($logical, $collection, $condition, $nextLogical) |
85
|
|
|
{ |
86
|
3 |
|
if ($logical === '||') { |
87
|
1 |
|
$collection->orWhere($this->addWhereGroup($collection, $condition, $nextLogical)); |
88
|
|
|
} |
89
|
3 |
|
if ($logical === '&&') { |
90
|
2 |
|
$collection->where($this->addWhereGroup($collection, $condition, $nextLogical)); |
91
|
|
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
3 |
|
private function parseWhere($collection, $conditions, $logical) |
95
|
|
|
{ |
96
|
3 |
|
foreach ($conditions as $key => $condition) { |
97
|
3 |
|
switch (strtoupper($key)) { |
98
|
3 |
|
case 'AND': |
99
|
2 |
|
$this->parseWhereGroup($logical, $collection, $condition, '&&'); |
100
|
2 |
|
break; |
101
|
3 |
|
case 'OR': |
102
|
3 |
|
$this->parseWhereGroup($logical, $collection, $condition, '||'); |
103
|
3 |
|
break; |
104
|
|
|
default: |
105
|
2 |
|
list($fieldName, $operator, $value) = $condition; |
106
|
3 |
|
$collection->where($fieldName, $operator, $value, $logical); |
107
|
|
|
} |
108
|
|
|
} |
109
|
3 |
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.