-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSelectQuery.php
100 lines (89 loc) · 2.34 KB
/
SelectQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Aternos\Model\Query;
/**
* Class SelectQuery
*
* @package Aternos\Model\Query
*/
class SelectQuery extends Query
{
/**
* @var SelectField[]
*/
protected ?array $fields = null;
protected ?array $group = null;
/**
* SelectQuery constructor.
*
* @param array|WhereCondition|WhereGroup|null $where
* @param array|null $order
* @param array|null $fields
* @param array|int|Limit|null $limit
* @param array|GroupField[]|string[]|null $group
*/
public function __construct(null|WhereCondition|array|WhereGroup $where = null,
null|array $order = null,
null|array $fields = null,
null|Limit|array|int $limit = null,
null|array $group = null)
{
if ($where) {
$this->where($where);
}
if ($order) {
$this->orderBy($order);
}
if ($fields) {
$this->fields($fields);
}
if ($limit) {
$this->limit($limit);
}
if ($group) {
$this->groupBy($group);
}
}
/**
* Set fields
*
* @param array $fields
* @return $this
*/
public function fields(array $fields): static
{
$this->fields = [];
foreach ($fields as $field) {
if ($field instanceof SelectField) {
$this->fields[] = $field;
} else if (is_string($field)) {
$this->fields[] = new SelectField($field);
}
}
return $this;
}
/**
* Set group by fields
*
* @param array|GroupField[]|string[] $fields
* @return $this
*/
public function groupBy(array $fields): static
{
$this->group = null;
foreach ($fields as $key => $field) {
if ($field instanceof GroupField) {
$this->group[] = $field;
} else if (is_string($field)) {
$this->group[] = new GroupField($field);
}
}
return $this;
}
/**
* @return array|null|GroupField[]
*/
public function getGroup(): ?array
{
return $this->group;
}
}