1 | <?php |
||
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) |
|
72 | |||
73 | 2 | public function select($fieldList) |
|
78 | |||
79 | 6 | public function from($collectionName) |
|
84 | |||
85 | 8 | public function where($conditions) |
|
91 | |||
92 | 2 | public function orderBy($fieldName, $sortMode = 'ASC') |
|
97 | |||
98 | 2 | public function limit($limit) |
|
103 | |||
104 | 2 | public function offset($offset) |
|
109 | |||
110 | 4 | public function first() |
|
114 | |||
115 | 1 | public function all() |
|
119 | } |
||
120 |