1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipbox/spark |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\spark\services\traits; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\spark\exceptions\RecordNotFoundException; |
13
|
|
|
use flipbox\spark\helpers\QueryHelper; |
14
|
|
|
use flipbox\spark\helpers\RecordHelper; |
15
|
|
|
use flipbox\spark\records\Record; |
16
|
|
|
use yii\db\ActiveQuery; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.1.0 |
21
|
|
|
*/ |
22
|
|
|
trait Object |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public abstract static function recordClass(): string; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public static function recordClassInstance(): string |
34
|
|
|
{ |
35
|
|
|
return Record::class; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $config |
40
|
|
|
* @return \yii\db\ActiveQuery |
41
|
|
|
*/ |
42
|
|
|
public function getRecordQuery($config = []): ActiveQuery |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** @var Record $recordClass */ |
46
|
|
|
$recordClass = $this->recordClass(); |
47
|
|
|
|
48
|
|
|
$query = $recordClass::find(); |
49
|
|
|
|
50
|
|
|
if ($config) { |
|
|
|
|
51
|
|
|
|
52
|
|
|
QueryHelper::configure( |
53
|
|
|
$query, |
54
|
|
|
$config |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $query; |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/******************************************* |
64
|
|
|
* CREATE |
65
|
|
|
*******************************************/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $attributes |
69
|
|
|
* @param string $toScenario |
70
|
|
|
* @return Record |
71
|
|
|
*/ |
72
|
|
|
public function createRecord(array $attributes = [], string $toScenario = null) |
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
/** @var string $recordClass */ |
76
|
|
|
$recordClass = static::recordClass(); |
77
|
|
|
|
78
|
|
|
/** @var Record $record */ |
79
|
|
|
$record = new $recordClass(); |
80
|
|
|
|
81
|
|
|
// Set scenario |
82
|
|
|
if ($toScenario) { |
|
|
|
|
83
|
|
|
$record->setScenario($toScenario); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Do we need to set properties too |
87
|
|
|
if (!empty($attributes)) { |
88
|
|
|
$record->setAttributes($attributes); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $record; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $condition |
97
|
|
|
* @param string $toScenario |
98
|
|
|
* @return Record|null |
99
|
|
|
*/ |
100
|
|
|
public function findRecordByCondition($condition, string $toScenario = null) |
101
|
|
|
{ |
102
|
|
|
|
103
|
|
|
if (empty($condition)) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->findRecordByCriteria( |
108
|
|
|
RecordHelper::conditionToCriteria($condition), |
109
|
|
|
$toScenario |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $criteria |
116
|
|
|
* @param string $toScenario |
117
|
|
|
* @return Record |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function findRecordByCriteria($criteria, string $toScenario = null) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
$query = $this->getRecordQuery($criteria); |
123
|
|
|
|
124
|
|
|
/** @var Record $record */ |
125
|
|
|
if ($record = $query->one()) { |
126
|
|
|
|
127
|
|
|
// Set scenario |
128
|
|
|
if ($toScenario) { |
|
|
|
|
129
|
|
|
$record->setScenario($toScenario); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $record; |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $condition |
140
|
|
|
* @param string $toScenario |
141
|
|
|
* @return Record |
142
|
|
|
* @throws RecordNotFoundException |
143
|
|
|
*/ |
144
|
|
|
public function getRecordByCondition($condition, string $toScenario = null) |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
if (!$record = $this->findRecordByCondition($condition, $toScenario)) { |
148
|
|
|
|
149
|
|
|
$this->notFoundRecordException(); |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $record; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $criteria |
159
|
|
|
* @param string $toScenario |
160
|
|
|
* @return Record |
161
|
|
|
* @throws RecordNotFoundException |
162
|
|
|
*/ |
163
|
|
|
public function getRecordByCriteria($criteria, string $toScenario = null) |
164
|
|
|
{ |
165
|
|
|
|
166
|
|
|
if (!$record = $this->findRecordByCriteria($criteria, $toScenario)) { |
167
|
|
|
|
168
|
|
|
$this->notFoundRecordException(); |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $record; |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $toScenario |
179
|
|
|
* @return Record[] |
180
|
|
|
*/ |
181
|
|
|
public function findAllRecords(string $toScenario = null) |
182
|
|
|
{ |
183
|
|
|
return $this->findAllRecordsByCondition(null, $toScenario); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $condition |
188
|
|
|
* @param string $toScenario |
189
|
|
|
* @return Record[] |
190
|
|
|
*/ |
191
|
|
|
public function findAllRecordsByCondition($condition = [], string $toScenario = null) |
192
|
|
|
{ |
193
|
|
|
|
194
|
|
|
return $this->findAllRecordsByCriteria( |
195
|
|
|
RecordHelper::conditionToCriteria($condition), |
196
|
|
|
$toScenario |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $criteria |
203
|
|
|
* @param string $toScenario |
204
|
|
|
* @return Record[] |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
public function findAllRecordsByCriteria($criteria = [], string $toScenario = null) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
|
209
|
|
|
$query = $this->getRecordQuery($criteria); |
210
|
|
|
|
211
|
|
|
/** @var Record[] $record s */ |
212
|
|
|
$records = $query->all(); |
213
|
|
|
|
214
|
|
|
// Set scenario |
215
|
|
|
if ($toScenario) { |
|
|
|
|
216
|
|
|
|
217
|
|
|
/** @var Record $record */ |
218
|
|
|
foreach ($records as $record) { |
219
|
|
|
|
220
|
|
|
// Set scenario |
221
|
|
|
$record->setScenario($toScenario); |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $records; |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @deprecated |
234
|
|
|
* @param array $condition |
235
|
|
|
* @param string $toScenario |
236
|
|
|
* @return Record[] |
237
|
|
|
* @throws RecordNotFoundException |
238
|
|
|
*/ |
239
|
|
|
public function getAllRecords($condition = [], string $toScenario = null) |
240
|
|
|
{ |
241
|
|
|
|
242
|
|
|
Craft::$app->getDeprecator()->log( |
243
|
|
|
__METHOD__, |
244
|
|
|
'Use the "getAllRecordsByCondition" method' |
245
|
|
|
); |
246
|
|
|
|
247
|
|
|
return $this->getAllRecordsByCondition($condition, $toScenario); |
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param array $condition |
253
|
|
|
* @param string $toScenario |
254
|
|
|
* @return Record[] |
255
|
|
|
* @throws RecordNotFoundException |
256
|
|
|
*/ |
257
|
|
|
public function getAllRecordsByCondition($condition = [], string $toScenario = null) |
258
|
|
|
{ |
259
|
|
|
|
260
|
|
|
if (!$records = $this->findAllRecordsByCondition($condition, $toScenario)) { |
261
|
|
|
|
262
|
|
|
$this->notFoundRecordException(); |
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $records; |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param array $criteria |
272
|
|
|
* @param string $toScenario |
273
|
|
|
* @return Record[] |
274
|
|
|
* @throws RecordNotFoundException |
275
|
|
|
*/ |
276
|
|
|
public function getAllRecordsByCriteria($criteria = [], string $toScenario = null) |
277
|
|
|
{ |
278
|
|
|
|
279
|
|
|
if (!$records = $this->findAllRecordsByCriteria($criteria, $toScenario)) { |
280
|
|
|
|
281
|
|
|
$this->notFoundRecordException(); |
282
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $records; |
286
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/******************************************* |
290
|
|
|
* EXCEPTIONS |
291
|
|
|
*******************************************/ |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @throws RecordNotFoundException |
295
|
|
|
*/ |
296
|
|
|
protected function notFoundRecordException() |
297
|
|
|
{ |
298
|
|
|
|
299
|
|
|
throw new RecordNotFoundException( |
300
|
|
|
sprintf( |
301
|
|
|
"Record does not exist." |
302
|
|
|
) |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.