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