1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Algatux\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use MongoDB\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Builder. |
9
|
|
|
*/ |
10
|
|
|
class Builder |
11
|
|
|
{ |
12
|
|
|
/** @var Collection */ |
13
|
|
|
private $collection; |
14
|
|
|
/** @var Expression */ |
15
|
|
|
private $expression; |
16
|
|
|
/** @var array */ |
17
|
|
|
private $options; |
18
|
|
|
/** @var int */ |
19
|
|
|
private $queryType; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Builder constructor. |
23
|
|
|
* |
24
|
|
|
* @param Collection $collection |
25
|
|
|
*/ |
26
|
11 |
|
public function __construct(Collection $collection) |
27
|
|
|
{ |
28
|
11 |
|
$this->collection = $collection; |
29
|
11 |
|
$this->queryType = Query::TYPE_FIND; |
30
|
11 |
|
$this->expression = new Expression(); |
31
|
11 |
|
$this->options = []; |
32
|
11 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
1 |
|
public function find() |
38
|
|
|
{ |
39
|
1 |
|
return $this->setType(Query::TYPE_FIND); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
1 |
|
public function count() |
46
|
|
|
{ |
47
|
1 |
|
return $this->setType(Query::TYPE_COUNT); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $type |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
2 |
|
protected function setType(int $type) |
56
|
|
|
{ |
57
|
2 |
|
$this->queryType = $type; |
58
|
|
|
|
59
|
2 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array|Expression $expression |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function and($expression) |
|
|
|
|
68
|
|
|
{ |
69
|
4 |
|
$this->expression->and(...func_get_args()); |
70
|
|
|
|
71
|
4 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array|Expression $expression |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
1 |
|
public function or($expression) |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
$this->expression->or(...func_get_args()); |
82
|
|
|
|
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function notEqual(string $field, ...$expressions) |
87
|
|
|
{ |
88
|
1 |
|
$this->expression->notEqual($field, ...$expressions); |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Query |
95
|
|
|
*/ |
96
|
11 |
|
public function getQuery(): Query |
97
|
|
|
{ |
98
|
11 |
|
return new Query( |
99
|
11 |
|
$this->collection, |
100
|
11 |
|
$this->queryType, |
101
|
11 |
|
$this->expression->getExpressionFilters(), |
102
|
11 |
|
$this->options |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
2 |
|
public function getQueryType(): int |
110
|
|
|
{ |
111
|
2 |
|
return $this->queryType; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $fields |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
1 |
|
public function sort(array $fields) |
120
|
|
|
{ |
121
|
1 |
|
return $this->setQueryOption('sort', $fields); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param int $limit |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
1 |
|
public function limit(int $limit) |
130
|
|
|
{ |
131
|
1 |
|
return $this->setQueryOption('limit', $limit); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $skip |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
1 |
|
public function skip(int $skip) |
140
|
|
|
{ |
141
|
1 |
|
return $this->setQueryOption('skip', $skip); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $projection |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
1 |
|
public function select(...$projection) |
150
|
|
|
{ |
151
|
1 |
|
$this->setQueryOption('projection', array_fill_keys($projection, 1)); |
152
|
|
|
|
153
|
1 |
|
if (!in_array('_id', $projection)) { |
154
|
1 |
|
$this->options['projection']['_id'] = -1; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $option |
162
|
|
|
* @param mixed $value |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
5 |
|
public function setQueryOption(string $option, $value) |
167
|
|
|
{ |
168
|
5 |
|
$this->options[$option] = $value; |
169
|
|
|
|
170
|
5 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return Expression |
175
|
|
|
*/ |
176
|
2 |
|
public function expr(): Expression |
177
|
|
|
{ |
178
|
2 |
|
return new Expression(); |
179
|
|
|
} |
180
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.