1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TZachi\PhalconRepository; |
6
|
|
|
|
7
|
|
|
use Phalcon\Mvc\Model; |
8
|
|
|
use Phalcon\Mvc\Model\Criteria; |
9
|
|
|
use Phalcon\Mvc\Model\Resultset\Simple as SimpleResultset; |
10
|
|
|
use TZachi\PhalconRepository\Resolver\QueryParameter; |
11
|
|
|
|
12
|
|
|
class Repository |
13
|
|
|
{ |
14
|
|
|
public const TYPE_AND = 'AND'; |
15
|
|
|
public const TYPE_OR = 'OR'; |
16
|
|
|
|
17
|
|
|
protected const OPERATORS = ['=', '<>', '<=', '>=', '<', '>', 'BETWEEN']; |
18
|
|
|
|
19
|
|
|
protected const CONDITION_TYPES = [self::TYPE_AND, self::TYPE_OR]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ModelWrapper |
23
|
|
|
*/ |
24
|
|
|
protected $modelWrapper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var QueryParameter |
28
|
|
|
*/ |
29
|
|
|
protected $queryParameterResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $idField; |
35
|
|
|
|
36
|
23 |
|
public function __construct( |
37
|
|
|
ModelWrapper $modelWrapper, |
38
|
|
|
QueryParameter $queryParameterResolver, |
39
|
|
|
string $idField = 'id' |
40
|
|
|
) { |
41
|
23 |
|
$this->modelWrapper = $modelWrapper; |
42
|
23 |
|
$this->queryParameterResolver = $queryParameterResolver; |
43
|
23 |
|
$this->idField = $idField; |
44
|
23 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns the first record matching the specified conditions |
48
|
|
|
* |
49
|
|
|
* @param mixed[] $where Where condition |
50
|
|
|
* @param string[] $orderBy One or more order by statements |
51
|
|
|
*/ |
52
|
2 |
|
public function findFirstWhere(array $where = [], array $orderBy = []): ?Model |
53
|
|
|
{ |
54
|
2 |
|
$parameters = $this->queryParameterResolver->where($where) + $this->queryParameterResolver->orderBy($orderBy); |
55
|
|
|
|
56
|
2 |
|
$model = $this->modelWrapper->findFirst($parameters); |
57
|
2 |
|
if ($model === false) { |
58
|
1 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return $model; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the first record that matches a single condition |
66
|
|
|
* |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @param string[] $orderBy One or more order by statements |
69
|
|
|
*/ |
70
|
2 |
|
public function findFirstBy(string $field, $value, array $orderBy = []): ?Model |
71
|
|
|
{ |
72
|
2 |
|
return $this->findFirstWhere([$field => $value], $orderBy); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the first record that matches a primary key |
77
|
|
|
* |
78
|
|
|
* @param mixed $id |
79
|
|
|
*/ |
80
|
3 |
|
public function findFirst($id): ?Model |
81
|
|
|
{ |
82
|
3 |
|
return $this->findFirstBy($this->idField, $id); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Finds all records matching the specified conditions |
87
|
|
|
* |
88
|
|
|
* @param mixed[] $where |
89
|
|
|
* @param string[] $orderBy |
90
|
|
|
*/ |
91
|
1 |
|
public function findWhere(array $where = [], array $orderBy = [], int $limit = 0, int $offset = 0): SimpleResultset |
92
|
|
|
{ |
93
|
1 |
|
$parameters = $this->queryParameterResolver->where($where) |
94
|
1 |
|
+ $this->queryParameterResolver->orderBy($orderBy) |
95
|
1 |
|
+ $this->queryParameterResolver->limit($limit, $offset); |
96
|
|
|
|
97
|
1 |
|
return $this->modelWrapper->find($parameters); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Finds all records that match a single condition |
102
|
|
|
* |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @param string[] $orderBy |
105
|
|
|
*/ |
106
|
1 |
|
public function findBy( |
107
|
|
|
string $field, |
108
|
|
|
$value, |
109
|
|
|
array $orderBy = [], |
110
|
|
|
int $limit = 0, |
111
|
|
|
int $offset = 0 |
112
|
|
|
): SimpleResultset { |
113
|
1 |
|
return $this->findWhere([$field => $value], $orderBy, $limit, $offset); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns a query builder (Criteria) to create custom queries |
118
|
|
|
*/ |
119
|
1 |
|
public function query(): Criteria |
120
|
|
|
{ |
121
|
1 |
|
return $this->modelWrapper->query(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the number of rows that match a certain condition |
126
|
|
|
* |
127
|
|
|
* @param mixed[] $where |
128
|
|
|
*/ |
129
|
2 |
|
public function count(?string $column = null, array $where = []): int |
130
|
|
|
{ |
131
|
2 |
|
$parameters = []; |
132
|
2 |
|
if ($column !== null) { |
133
|
1 |
|
$parameters += $this->queryParameterResolver->column($column); |
134
|
|
|
} |
135
|
2 |
|
$parameters += $this->queryParameterResolver->where($where); |
136
|
|
|
|
137
|
2 |
|
return $this->modelWrapper->count($parameters); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the sum on a column of rows or null if the conditions don't match any rows |
142
|
|
|
* |
143
|
|
|
* @param mixed[] $where |
144
|
|
|
*/ |
145
|
2 |
|
public function sum(string $column, array $where = []): ?float |
146
|
|
|
{ |
147
|
2 |
|
$parameters = $this->queryParameterResolver->column($column) + $this->queryParameterResolver->where($where); |
148
|
|
|
|
149
|
2 |
|
$sum = $this->modelWrapper->sum($parameters); |
150
|
2 |
|
if ($sum === null) { |
151
|
1 |
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return (float) $sum; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the average on a column of rows or null if the conditions don't match any rows |
159
|
|
|
* |
160
|
|
|
* @param mixed[] $where |
161
|
|
|
*/ |
162
|
2 |
|
public function average(string $column, array $where = []): ?float |
163
|
|
|
{ |
164
|
2 |
|
$parameters = $this->queryParameterResolver->column($column) + $this->queryParameterResolver->where($where); |
165
|
|
|
|
166
|
2 |
|
$average = $this->modelWrapper->average($parameters); |
167
|
2 |
|
if ($average === null) { |
168
|
1 |
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
return (float) $average; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the minimum on a column of rows or null if the conditions don't match any rows |
176
|
|
|
* |
177
|
|
|
* @param mixed[] $where |
178
|
|
|
*/ |
179
|
2 |
|
public function minimum(string $column, array $where = []): ?string |
180
|
|
|
{ |
181
|
2 |
|
$parameters = $this->queryParameterResolver->column($column) + $this->queryParameterResolver->where($where); |
182
|
|
|
|
183
|
2 |
|
return $this->modelWrapper->minimum($parameters); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns the maximum on a column of rows or null if the conditions don't match any rows |
188
|
|
|
* |
189
|
|
|
* @param mixed[] $where |
190
|
|
|
*/ |
191
|
2 |
|
public function maximum(string $column, array $where = []): ?string |
192
|
|
|
{ |
193
|
2 |
|
$parameters = $this->queryParameterResolver->column($column) + $this->queryParameterResolver->where($where); |
194
|
|
|
|
195
|
2 |
|
return $this->modelWrapper->maximum($parameters); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|