1
|
|
|
<?php
|
2
|
|
|
namespace Fathomminds\Rest\Database;
|
3
|
|
|
|
4
|
|
|
use Fathomminds\Rest\Contracts\IFinder;
|
5
|
|
|
use Fathomminds\Rest\Exceptions\RestException;
|
6
|
|
|
use Fathomminds\Rest\Database\Where;
|
7
|
|
|
|
8
|
|
|
abstract class Finder implements IFinder
|
9
|
|
|
{
|
10
|
|
|
protected $queryConfiguration;
|
11
|
|
|
protected $resultSet;
|
12
|
|
|
protected $client;
|
13
|
|
|
protected $mainLogical = '&&';
|
14
|
|
|
protected $validGroups = [
|
15
|
|
|
'AND',
|
16
|
|
|
'OR',
|
17
|
|
|
];
|
18
|
|
|
protected $validOperators = [
|
19
|
|
|
'==',
|
20
|
|
|
'!=',
|
21
|
|
|
'<',
|
22
|
|
|
'>',
|
23
|
|
|
'<=',
|
24
|
|
|
'>=',
|
25
|
|
|
];
|
26
|
|
|
|
27
|
|
|
protected $map = [];
|
28
|
|
|
|
29
|
|
|
abstract protected function createClient();
|
30
|
|
|
abstract public function get();
|
31
|
|
|
|
32
|
|
|
private function validateConditions($conditions)
|
33
|
|
|
{
|
34
|
|
|
foreach ($conditions as $key => $condition) {
|
35
|
|
|
if (in_array(strtoupper($key), $this->validGroups)) {
|
36
|
|
|
$this->validateConditions($condition);
|
37
|
|
|
return;
|
38
|
|
|
}
|
39
|
|
|
$this->validateCondition($condition);
|
40
|
|
|
}
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
private function validateCondition($condition)
|
44
|
|
|
{
|
45
|
|
|
if (count($condition) === 3) {
|
46
|
|
|
$operator = $condition[1];
|
47
|
|
|
if (!in_array($operator, $this->validOperators)) {
|
48
|
|
|
throw new RestException('Invalid operator in where condition', [
|
49
|
|
|
'operator' => $operator,
|
50
|
|
|
]);
|
51
|
|
|
}
|
52
|
|
|
return;
|
53
|
|
|
}
|
54
|
|
|
throw new RestException('Invalid where condition', [
|
55
|
|
|
'condition' => $condition,
|
56
|
|
|
]);
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
public function __construct($client = null)
|
60
|
|
|
{
|
61
|
|
|
$this->queryConfiguration = new FinderQueryConfiguration;
|
62
|
|
|
if ($client !== null) {
|
63
|
|
|
$this->client = $client;
|
64
|
|
|
return $this;
|
|
|
|
|
65
|
|
|
}
|
66
|
|
|
$this->createClient();
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
public function database($databaseName)
|
70
|
|
|
{
|
71
|
|
|
$this->queryConfiguration->databaseName = $databaseName;
|
72
|
|
|
return $this;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
public function select($fieldList)
|
76
|
|
|
{
|
77
|
|
|
$this->queryConfiguration->select = $fieldList;
|
78
|
|
|
return $this;
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
public function from($collectionName)
|
82
|
|
|
{
|
83
|
|
|
$this->queryConfiguration->from = $collectionName;
|
84
|
|
|
return $this;
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
public function where($conditions)
|
88
|
|
|
{
|
89
|
|
|
$this->validateConditions($conditions);
|
90
|
|
|
$this->queryConfiguration->where = $conditions;
|
91
|
|
|
return $this;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
public function orderBy($fieldName, $sortMode = 'ASC')
|
95
|
|
|
{
|
96
|
|
|
$this->queryConfiguration->orderBy[] = [$fieldName => $sortMode];
|
97
|
|
|
return $this;
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
public function limit($limit)
|
101
|
|
|
{
|
102
|
|
|
$this->queryConfiguration->limit = $limit;
|
103
|
|
|
return $this;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
public function offset($offset)
|
107
|
|
|
{
|
108
|
|
|
$this->queryConfiguration->offset = $offset;
|
109
|
|
|
return $this;
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
public function first()
|
113
|
|
|
{
|
114
|
|
|
return isset($this->resultSet[0]) ? $this->resultSet[0] : null;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
public function all()
|
118
|
|
|
{
|
119
|
|
|
return $this->resultSet;
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
public function map($map)
|
123
|
|
|
{
|
124
|
|
|
$this->map = $map;
|
125
|
|
|
return $this;
|
126
|
|
|
}
|
127
|
|
|
}
|
128
|
|
|
|