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 flipbox\spark\exceptions\ModelNotFoundException; |
12
|
|
|
use flipbox\spark\exceptions\RecordNotFoundException; |
13
|
|
|
use flipbox\spark\models\Model as BaseModel; |
14
|
|
|
use flipbox\spark\records\Record; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.2.0 |
19
|
|
|
*/ |
20
|
|
|
trait ModelByString |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
use Model; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var BaseModel[] |
27
|
|
|
*/ |
28
|
|
|
protected $_cacheByString = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
abstract protected function stringProperty(): string; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Record $record |
37
|
|
|
* @param string|null $toScenario |
38
|
|
|
* @return BaseModel |
39
|
|
|
*/ |
40
|
|
|
abstract protected function findByRecord(Record $record, string $toScenario = null): BaseModel; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $config |
44
|
|
|
* @param string|null $toScenario |
45
|
|
|
* @return BaseModel |
46
|
|
|
*/ |
47
|
|
|
abstract public function create($config = [], string $toScenario = null): BaseModel; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
protected function recordStringProperty(): string |
53
|
|
|
{ |
54
|
|
|
return $this->stringProperty(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param BaseModel $model |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function stringValue(BaseModel $model): string |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
$property = $this->stringProperty(); |
65
|
|
|
|
66
|
|
|
return $model->{$property}; |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/******************************************* |
71
|
|
|
* FIND/GET BY STRING |
72
|
|
|
*******************************************/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $string |
76
|
|
|
* @param string|null $toScenario |
77
|
|
|
* @return BaseModel|null |
78
|
|
|
*/ |
79
|
|
|
public function findByString(string $string, string $toScenario = null) |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
// Check cache |
83
|
|
|
if (!$model = $this->findCacheByString($string)) { |
|
|
|
|
84
|
|
|
|
85
|
|
|
// Find record in db |
86
|
|
|
if ($record = $this->findRecordByString($string)) { |
87
|
|
|
|
88
|
|
|
$model = $this->findByRecord($record, $toScenario); |
89
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
|
92
|
|
|
$this->_cacheByString[$string] = null; |
93
|
|
|
|
94
|
|
|
return null; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $model; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $string |
106
|
|
|
* @param string|null $toScenario |
107
|
|
|
* @return BaseModel|null |
108
|
|
|
* @throws ModelNotFoundException |
109
|
|
|
*/ |
110
|
|
|
public function getByString(string $string, string $toScenario = null): BaseModel |
111
|
|
|
{ |
112
|
|
|
|
113
|
|
|
if (!$model = $this->findByString($string, $toScenario)) { |
114
|
|
|
|
115
|
|
|
$this->notFoundByStringException($string); |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $model; |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $string |
125
|
|
|
* @param string|null $toScenario |
126
|
|
|
* @return BaseModel|null |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function freshFindByString(string $string, string $toScenario = null) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
|
131
|
|
|
// Find record in db |
132
|
|
|
if (!$record = $this->findRecordByString($string)) { |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$model = $this->create($record, $toScenario); |
|
|
|
|
137
|
|
|
|
138
|
|
|
return $model; |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $string |
144
|
|
|
* @param string|null $toScenario |
145
|
|
|
* @return BaseModel |
146
|
|
|
* @throws ModelNotFoundException |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function freshGetByString(string $string, string $toScenario = null): BaseModel |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
|
151
|
|
|
if (!$model = $this->freshFindByString($string, $toScenario)) { |
152
|
|
|
|
153
|
|
|
$this->notFoundByStringException($string); |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $model; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/******************************************* |
162
|
|
|
* CACHE |
163
|
|
|
*******************************************/ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Find an existing cache by string |
167
|
|
|
* |
168
|
|
|
* @param string $string |
169
|
|
|
* @return null |
170
|
|
|
*/ |
171
|
|
|
public function findCacheByString(string $string) |
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
// Check if already in cache |
175
|
|
|
if (!$this->isCachedByString($string)) { |
176
|
|
|
return null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->_cacheByString[$string]; |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Identify whether in cache by string |
185
|
|
|
* |
186
|
|
|
* @param string $string |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
private function isCachedByString(string $string): bool |
190
|
|
|
{ |
191
|
|
|
return array_key_exists($string, $this->_cacheByString); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param BaseModel $model |
197
|
|
|
* @return static |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
protected function cacheByString(BaseModel $model) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
|
202
|
|
|
$stringValue = $this->stringValue($model); |
203
|
|
|
|
204
|
|
|
if (null === $stringValue) { |
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Check if already in cache |
209
|
|
|
if (!$this->isCachedByString($stringValue)) { |
210
|
|
|
|
211
|
|
|
// Cache it |
212
|
|
|
$this->_cacheByString[$stringValue] = $model; |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param Record $record |
222
|
|
|
* @return BaseModel|null |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
protected function findCacheByRecordByString(Record $record) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
|
227
|
|
|
$property = $this->recordStringProperty(); |
228
|
|
|
|
229
|
|
|
$stringValue = $record->{$property}; |
230
|
|
|
|
231
|
|
|
if ($stringValue === null) { |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->findCacheByString($stringValue); |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/******************************************* |
240
|
|
|
* RECORD BY STRING |
241
|
|
|
*******************************************/ |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $string |
245
|
|
|
* @param string|null $toScenario |
246
|
|
|
* @return Record|null |
247
|
|
|
*/ |
248
|
|
|
public function findRecordByString(string $string, string $toScenario = null) |
249
|
|
|
{ |
250
|
|
|
|
251
|
|
|
return $this->findRecordByCondition( |
252
|
|
|
[ |
253
|
|
|
$this->recordStringProperty() => $string |
254
|
|
|
], |
255
|
|
|
$toScenario |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $string |
262
|
|
|
* @param string|null $toScenario |
263
|
|
|
* @throws RecordNotFoundException |
264
|
|
|
* @return Record|null |
265
|
|
|
*/ |
266
|
|
|
public function getRecordByString(string $string, string $toScenario = null) |
267
|
|
|
{ |
268
|
|
|
|
269
|
|
|
if (!$record = $this->findRecordByString($string, $toScenario)) { |
270
|
|
|
|
271
|
|
|
$this->notFoundRecordByStringException($string); |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $record; |
276
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param BaseModel $model |
282
|
|
|
* @param bool $mirrorScenario |
283
|
|
|
* @return Record |
284
|
|
|
*/ |
285
|
|
View Code Duplication |
protected function toRecordByString(BaseModel $model, bool $mirrorScenario = true): Record |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
|
288
|
|
|
// Get record |
289
|
|
|
if (!$record = $this->findRecordByString($this->stringValue($model))) { |
290
|
|
|
return null; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
// Populate the record attributes |
294
|
|
|
$this->transferToRecord($model, $record, $mirrorScenario); |
295
|
|
|
|
296
|
|
|
return $record; |
297
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/******************************************* |
301
|
|
|
* EXCEPTIONS |
302
|
|
|
*******************************************/ |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param string|null $string |
306
|
|
|
* @throws ModelNotFoundException |
307
|
|
|
*/ |
308
|
|
|
protected function notFoundByStringException(string $string = null) |
309
|
|
|
{ |
310
|
|
|
|
311
|
|
|
throw new ModelNotFoundException( |
312
|
|
|
sprintf( |
313
|
|
|
'Model does not exist with the string "%s".', |
314
|
|
|
(string)$string |
315
|
|
|
) |
316
|
|
|
); |
317
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param string|null $string |
322
|
|
|
* @throws RecordNotFoundException |
323
|
|
|
*/ |
324
|
|
|
protected function notFoundRecordByStringException(string $string = null) |
325
|
|
|
{ |
326
|
|
|
|
327
|
|
|
throw new RecordNotFoundException( |
328
|
|
|
sprintf( |
329
|
|
|
'Record does not exist with the string "%s".', |
330
|
|
|
(string)$string |
331
|
|
|
) |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
} |
337
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.