1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Algatux\QueryBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Expression. |
7
|
|
|
*/ |
8
|
|
|
class Expression |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
private $filters; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Expression constructor. |
15
|
|
|
*/ |
16
|
14 |
|
public function __construct() |
17
|
|
|
{ |
18
|
14 |
|
$this->filters = []; |
19
|
14 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array|Expression $expression |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function and($expression) |
|
|
|
|
27
|
|
|
{ |
28
|
7 |
|
$this->prepareFilterIndex('$and'); |
29
|
|
|
|
30
|
7 |
|
$this->filters['$and'] = array_merge( |
31
|
7 |
|
$this->filters['$and'], |
32
|
7 |
|
$this->mapExpressions(...func_get_args()) |
33
|
|
|
); |
34
|
|
|
|
35
|
7 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array|Expression $expression |
40
|
|
|
* |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function or($expression) |
|
|
|
|
44
|
|
|
{ |
45
|
2 |
|
$this->prepareFilterIndex('$or'); |
46
|
|
|
|
47
|
2 |
|
$this->filters['$or'] = array_merge( |
48
|
2 |
|
$this->filters['$or'], |
49
|
2 |
|
$this->mapExpressions(...func_get_args()) |
50
|
|
|
); |
51
|
|
|
|
52
|
2 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $field |
57
|
|
|
* @param Expression[]|array $expressions |
58
|
|
|
*/ |
59
|
2 |
|
public function notEqual(string $field, ...$expressions) |
60
|
|
|
{ |
61
|
2 |
|
$this->prepareFilterIndex($field); |
62
|
|
|
|
63
|
2 |
|
$this->filters[$field] = array_merge( |
64
|
2 |
|
$this->filters[$field], |
65
|
2 |
|
$this->operationExpressions('$ne', $expressions) |
66
|
|
|
); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
14 |
|
public function getExpressionFilters(): array |
73
|
|
|
{ |
74
|
14 |
|
return $this->filters; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $operator |
79
|
|
|
*/ |
80
|
9 |
|
private function prepareFilterIndex(string $operator) |
81
|
|
|
{ |
82
|
9 |
|
if (!isset($this->filters[$operator])) { |
83
|
9 |
|
$this->filters[$operator] = []; |
84
|
|
|
} |
85
|
9 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $expressions |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
7 |
|
private function mapExpressions($expressions): array |
|
|
|
|
93
|
|
|
{ |
94
|
7 |
|
return array_map( |
95
|
|
|
function ($expression) { |
96
|
7 |
|
return $expression instanceof Expression ? $expression->getExpressionFilters() : $expression; |
97
|
7 |
|
}, |
98
|
|
|
func_get_args() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $operation |
104
|
|
|
* @param array $expressions |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
2 |
|
private function operationExpressions(string $operation, array $expressions): array |
109
|
|
|
{ |
110
|
2 |
|
return array_map( |
111
|
2 |
|
function ($expression) use ($operation) { |
112
|
2 |
|
$filter = $expression instanceof Expression ? $expression->getExpressionFilters() : $expression; |
113
|
|
|
|
114
|
2 |
|
return [$operation => $filter]; |
115
|
2 |
|
}, |
116
|
|
|
$expressions |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.