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
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Builder constructor. |
21
|
|
|
* |
22
|
|
|
* @param Collection $collection |
23
|
|
|
*/ |
24
|
9 |
|
public function __construct(Collection $collection) |
25
|
|
|
{ |
26
|
9 |
|
$this->collection = $collection; |
27
|
9 |
|
$this->queryType = Query::TYPE_FIND; |
|
|
|
|
28
|
9 |
|
$this->expression = new Expression(); |
29
|
9 |
|
$this->options = []; |
30
|
9 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
1 |
|
public function find() |
36
|
|
|
{ |
37
|
1 |
|
return $this->setType(Query::TYPE_FIND); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
1 |
|
public function count() |
44
|
|
|
{ |
45
|
1 |
|
return $this->setType(Query::TYPE_COUNT); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $type |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
2 |
|
protected function setType(int $type) |
54
|
|
|
{ |
55
|
2 |
|
$this->queryType = $type; |
56
|
|
|
|
57
|
2 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array|Expression $expression |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function and($expression) |
|
|
|
|
66
|
|
|
{ |
67
|
4 |
|
$this->expression->and(...func_get_args()); |
68
|
|
|
|
69
|
4 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array|Expression $expression |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
1 |
|
public function or($expression) |
|
|
|
|
78
|
|
|
{ |
79
|
1 |
|
$this->expression->or(...func_get_args()); |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Query |
86
|
|
|
*/ |
87
|
9 |
|
public function getQuery(): Query |
88
|
|
|
{ |
89
|
9 |
|
return new Query( |
90
|
9 |
|
$this->collection, |
91
|
9 |
|
$this->queryType, |
92
|
9 |
|
$this->expression->getExpressionFilters(), |
93
|
9 |
|
$this->options |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
2 |
|
public function getQueryType(): int |
101
|
|
|
{ |
102
|
2 |
|
return $this->queryType; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $fields |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
1 |
|
public function sort(array $fields) |
111
|
|
|
{ |
112
|
1 |
|
return $this->setQueryOption('sort', $fields); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $limit |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
1 |
|
public function limit(int $limit) |
121
|
|
|
{ |
122
|
1 |
|
return $this->setQueryOption('limit', $limit); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int $skip |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function skip(int $skip) |
131
|
|
|
{ |
132
|
|
|
return $this->setQueryOption('skip', $skip); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array $projection |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
1 |
|
public function select(...$projection) |
141
|
|
|
{ |
142
|
1 |
|
$this->setQueryOption('projection', array_fill_keys($projection, 1)); |
143
|
|
|
|
144
|
1 |
|
if (!in_array('_id', $projection)) { |
145
|
1 |
|
$this->options['projection']['_id'] = -1; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $option |
153
|
|
|
* @param mixed $value |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
4 |
|
public function setQueryOption(string $option, $value) |
158
|
|
|
{ |
159
|
4 |
|
$this->options[$option] = $value; |
160
|
|
|
|
161
|
4 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return Expression |
166
|
|
|
*/ |
167
|
2 |
|
public function expr(): Expression |
168
|
|
|
{ |
169
|
2 |
|
return new Expression(); |
170
|
|
|
} |
171
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: