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
|
|
|
abstract protected function createClient(); |
28
|
|
|
abstract public function get(); |
29
|
|
|
|
30
|
8 |
|
private function validateConditions($conditions) |
31
|
|
|
{ |
32
|
8 |
|
foreach ($conditions as $key => $condition) { |
33
|
6 |
|
if (in_array(strtoupper($key), $this->validGroups)) { |
34
|
5 |
|
$this->validateConditions($condition); |
35
|
4 |
|
return; |
36
|
|
|
} |
37
|
6 |
|
$this->validateCondition($condition); |
38
|
|
|
} |
39
|
6 |
|
} |
40
|
|
|
|
41
|
6 |
|
private function validateCondition($condition) |
42
|
|
|
{ |
43
|
6 |
|
if (count($condition) === 3) { |
44
|
5 |
|
$operator = $condition[1]; |
45
|
5 |
|
if (!in_array($operator, $this->validOperators)) { |
46
|
1 |
|
throw new RestException('Invalid operator in where condition', [ |
47
|
1 |
|
'operator' => $operator, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
4 |
|
return; |
51
|
|
|
} |
52
|
1 |
|
throw new RestException('Invalid where condition', [ |
53
|
1 |
|
'condition' => $condition, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
25 |
|
public function __construct($client = null) |
58
|
|
|
{ |
59
|
25 |
|
$this->queryConfiguration = new FinderQueryConfiguration; |
60
|
25 |
|
if ($client !== null) { |
61
|
24 |
|
$this->client = $client; |
62
|
24 |
|
return $this; |
|
|
|
|
63
|
|
|
} |
64
|
2 |
|
$this->createClient(); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
6 |
|
public function database($databaseName) |
68
|
|
|
{ |
69
|
6 |
|
$this->queryConfiguration->databaseName = $databaseName; |
70
|
6 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function select($fieldList) |
74
|
|
|
{ |
75
|
2 |
|
$this->queryConfiguration->select = $fieldList; |
76
|
2 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function from($collectionName) |
80
|
|
|
{ |
81
|
6 |
|
$this->queryConfiguration->from = $collectionName; |
82
|
6 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
public function where($conditions) |
86
|
|
|
{ |
87
|
8 |
|
$this->validateConditions($conditions); |
88
|
6 |
|
$this->queryConfiguration->where = $conditions; |
89
|
6 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function orderBy($fieldName, $sortMode = 'ASC') |
93
|
|
|
{ |
94
|
2 |
|
$this->queryConfiguration->orderBy[] = [$fieldName => $sortMode]; |
95
|
2 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
public function limit($limit) |
99
|
|
|
{ |
100
|
2 |
|
$this->queryConfiguration->limit = $limit; |
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function offset($offset) |
105
|
|
|
{ |
106
|
2 |
|
$this->queryConfiguration->offset = $offset; |
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
public function first() |
111
|
|
|
{ |
112
|
4 |
|
return isset($this->resultSet[0]) ? $this->resultSet[0] : null; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function all() |
116
|
|
|
{ |
117
|
1 |
|
return $this->resultSet; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|