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
|
8 |
|
public function __construct(Collection $collection) |
25
|
|
|
{ |
26
|
8 |
|
$this->collection = $collection; |
27
|
8 |
|
$this->queryType = Query::TYPE_FIND; |
|
|
|
|
28
|
8 |
|
$this->expression = new Expression(); |
29
|
8 |
|
$this->options = []; |
30
|
8 |
|
} |
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
|
8 |
|
public function getQuery(): Query |
88
|
|
|
{ |
89
|
8 |
|
return new Query( |
90
|
8 |
|
$this->collection, |
91
|
8 |
|
$this->queryType, |
92
|
8 |
|
$this->expression->getExpressionFilters(), |
93
|
8 |
|
$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
|
1 |
|
public function sort(array $fields) |
109
|
|
|
{ |
110
|
1 |
|
$this->options['sort'] = $fields; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $limit |
115
|
|
|
*/ |
116
|
1 |
|
public function setMaxResults(int $limit) |
117
|
|
|
{ |
118
|
1 |
|
$this->options['limit'] = $limit; |
119
|
1 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $projection |
123
|
|
|
*/ |
124
|
1 |
|
public function select(...$projection) |
125
|
|
|
{ |
126
|
1 |
|
$this->options['projection'] = array_fill_keys($projection, 1); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return Expression |
131
|
|
|
*/ |
132
|
2 |
|
public function expr(): Expression |
133
|
|
|
{ |
134
|
2 |
|
return new Expression(); |
135
|
|
|
} |
136
|
|
|
} |
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: