1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OfflineAgency\MongoAutoSync\Traits; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use MongoDB\BSON\UTCDateTime; |
10
|
|
|
use OfflineAgency\MongoAutoSync\Http\Models\MDModel; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
trait MongoSyncTrait |
14
|
|
|
{ |
15
|
|
|
protected $has_partial_request; |
16
|
|
|
protected $request; |
17
|
|
|
protected $target_additional_data; |
18
|
|
|
protected $partial_generated_request; |
19
|
|
|
protected $options; |
20
|
|
|
protected $tempEM; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Request $request |
24
|
|
|
* @param array $additionalData |
25
|
|
|
* @param array $options |
26
|
|
|
* @param array $target_additional_data |
27
|
|
|
* @return $this |
28
|
|
|
* @throws Exception |
29
|
|
|
*/ |
30
|
|
|
public function storeWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = []) |
31
|
|
|
{ |
32
|
|
|
$this->initDataForSync($request, $additionalData, $options, $target_additional_data); |
33
|
|
|
$this->storeEditAllItems($request, 'add', $options); |
34
|
|
|
$this->processAllRelationships($request, 'add', '', '', $options); |
35
|
|
|
|
36
|
|
|
//Dispatch the creation event |
37
|
|
|
$this->fireModelEvent('storeWithSync'); |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $this->fresh(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Request $request |
44
|
|
|
* @param string $event |
45
|
|
|
* @param array $options |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
public function storeEditAllItems(Request $request, string $event, array $options) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
//Get the item name |
51
|
|
|
$items = $this->getItems(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
//Current Obj Create |
54
|
|
|
foreach ($items as $key => $item) { |
55
|
|
|
$is_ML = isML($item); |
56
|
|
|
$is_MD = isMD($item); |
57
|
|
|
|
58
|
|
|
$is_fillable = isFillable($item, $event); |
59
|
|
|
$is_skippable = $this->getIsSkippable($request->has($key)); |
60
|
|
|
|
61
|
|
|
if ($is_skippable) { |
62
|
|
|
continue; |
63
|
|
|
} else { |
64
|
|
|
$this->checkRequestExistence( |
65
|
|
|
$request, |
66
|
|
|
$key |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($is_fillable) { |
71
|
|
|
if ($is_ML) { |
72
|
|
|
if (is_null($this->$key)) { |
73
|
|
|
$old_value = []; |
74
|
|
|
} else { |
75
|
|
|
$old_value = $this->$key; |
76
|
|
|
} |
77
|
|
|
$this->$key = ml($old_value, $request->input($key)); |
78
|
|
|
} elseif ($is_MD) { |
79
|
|
|
if ($request->input($key) == '' || $request->input($key) == null) { |
80
|
|
|
$this->$key = null; |
81
|
|
|
} else { |
82
|
|
|
$this->$key = new UTCDateTime(new DateTime($request->input($key))); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$this->$key = $request->input($key); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->save(); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Request $request |
95
|
|
|
* @param string $event |
96
|
|
|
* @param string $parent |
97
|
|
|
* @param string $counter |
98
|
|
|
* @param array $options |
99
|
|
|
* @throws Exception |
100
|
|
|
*/ |
101
|
|
|
public function processAllRelationships(Request $request, string $event, string $parent, string $counter, array $options) |
102
|
|
|
{ |
103
|
|
|
$this->setMiniModels(); // For target Sync |
|
|
|
|
104
|
|
|
|
105
|
|
|
//Get the relation info |
106
|
|
|
$relations = $this->getMongoRelation(); |
|
|
|
|
107
|
|
|
|
108
|
|
|
//Process all relationships |
109
|
|
|
foreach ($relations as $method => $relation) { |
110
|
|
|
//Get Relation Save Mode |
111
|
|
|
$type = $relation['type']; |
112
|
|
|
$model = $relation['model']; |
113
|
|
|
$hasTarget = hasTarget($relation); |
114
|
|
|
if ($hasTarget) { |
115
|
|
|
$modelTarget = $relation['modelTarget']; |
116
|
|
|
$methodOnTarget = $relation['methodOnTarget']; |
117
|
|
|
$modelOnTarget = $relation['modelOnTarget']; |
118
|
|
|
$typeOnTarget = Arr::has($relation, 'typeOnTarget') ? Arr::get($relation, 'typeOnTarget') : 'EmbedsMany'; |
119
|
|
|
} else { |
120
|
|
|
$modelTarget = ''; |
121
|
|
|
$methodOnTarget = ''; |
122
|
|
|
$modelOnTarget = ''; |
123
|
|
|
$typeOnTarget = ''; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$is_EO = is_EO($type); |
127
|
|
|
$is_EM = is_EM($type); |
128
|
|
|
|
129
|
|
|
$is_EM_target = is_EM($typeOnTarget); |
130
|
|
|
$is_EO_target = is_EO($typeOnTarget); |
131
|
|
|
|
132
|
|
|
$key = $parent.$method.$counter; |
133
|
|
|
$is_skippable = $this->getIsSkippable($request->has($key), $hasTarget); |
134
|
|
|
|
135
|
|
|
if ($is_skippable) { |
136
|
|
|
continue; |
137
|
|
|
} |
138
|
|
|
$current_request = $request->has($key) ? $request : $this->getPartialGeneratedRequest(); |
139
|
|
|
|
140
|
|
|
$value = $this->getRelationshipRequest($key, $current_request); |
141
|
|
|
|
142
|
|
|
$is_embeds_has_to_be_updated = $request->has($key); |
143
|
|
|
|
144
|
|
|
if (! is_null($value) && ! ($value == '') && ! ($value == '[]')) { |
145
|
|
|
$objs = json_decode($value); |
146
|
|
|
} else { |
147
|
|
|
$objs = getArrayWithEmptyObj($model, $is_EO, $is_EM); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create |
151
|
|
|
if ($event == 'update' && $is_embeds_has_to_be_updated) { |
152
|
|
|
|
153
|
|
|
//Delete EmbedsMany or EmbedsOne on Target - TODO: check if it is necessary to run deleteTargetObj method |
154
|
|
|
if ($hasTarget) { |
155
|
|
|
$this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target); |
156
|
|
|
} |
157
|
|
|
//Delete EmbedsMany or EmbedsOne on current object |
158
|
|
|
if ($is_EM) { |
159
|
|
|
$this->$method = []; |
160
|
|
|
$this->save(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (! empty($objs)) { |
165
|
|
|
if ($is_EM) { |
166
|
|
|
$this->tempEM = []; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$i = 0; |
170
|
|
|
foreach ($objs as $obj) { |
171
|
|
|
$this->processOneEmbeddedRelationship( |
172
|
|
|
$request, |
173
|
|
|
$obj, |
174
|
|
|
$type, |
175
|
|
|
$model, |
176
|
|
|
$method, |
177
|
|
|
$modelTarget, |
178
|
|
|
$methodOnTarget, |
179
|
|
|
$modelOnTarget, $event, |
180
|
|
|
$hasTarget, |
181
|
|
|
$is_EO, |
182
|
|
|
$is_EM, |
183
|
|
|
$is_EO_target, |
184
|
|
|
$is_EM_target, |
185
|
|
|
$i, |
186
|
|
|
$is_embeds_has_to_be_updated, |
187
|
|
|
$options); |
188
|
|
|
$i++; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($is_EM) { |
192
|
|
|
$this->$method = $this->tempEM; |
193
|
|
|
} |
194
|
|
|
} else { |
195
|
|
|
$this->$method = []; |
196
|
|
|
} |
197
|
|
|
$this->save(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param $mini_model |
204
|
|
|
* @param string $method_on_target |
205
|
|
|
* @param bool $is_EO_target |
206
|
|
|
* @param bool $is_EM_target |
207
|
|
|
*/ |
208
|
|
|
public function updateRelationWithSync($mini_model, string $method_on_target, $is_EO_target, $is_EM_target) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
if ($is_EM_target) { |
211
|
|
|
$new_values = []; |
212
|
|
|
foreach ($this->$method_on_target as $temp) { |
213
|
|
|
$new_values[] = $temp->attributes; |
214
|
|
|
} |
215
|
|
|
$new_values[] = $mini_model->attributes; |
216
|
|
|
} else { |
217
|
|
|
$new_values = $mini_model->attributes; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$this->$method_on_target = $new_values; |
221
|
|
|
$this->save(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param Request $request |
226
|
|
|
* @param $obj |
227
|
|
|
* @param $type |
228
|
|
|
* @param $model |
229
|
|
|
* @param $method |
230
|
|
|
* @param $modelTarget |
231
|
|
|
* @param $methodOnTarget |
232
|
|
|
* @param $modelOnTarget |
233
|
|
|
* @param $event |
234
|
|
|
* @param $hasTarget |
235
|
|
|
* @param bool $is_EO |
236
|
|
|
* @param bool $is_EM |
237
|
|
|
* @param bool $is_EO_target |
238
|
|
|
* @param bool $is_EM_target |
239
|
|
|
* @param $i |
240
|
|
|
* @param bool $is_embeds_has_to_be_updated |
241
|
|
|
* @param $options |
242
|
|
|
* @throws Exception |
243
|
|
|
*/ |
244
|
|
|
public function processOneEmbeddedRelationship(Request $request, $obj, $type, $model, $method, $modelTarget, $methodOnTarget, $modelOnTarget, $event, $hasTarget, $is_EO, $is_EM, $is_EO_target, $is_EM_target, $i, $is_embeds_has_to_be_updated, $options) |
245
|
|
|
{ |
246
|
|
|
if ($is_embeds_has_to_be_updated) { |
247
|
|
|
$this->processEmbedOnCurrentCollection($request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if ($hasTarget) { |
251
|
|
|
$this->processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param Request $request |
257
|
|
|
* @param array $additionalData |
258
|
|
|
* @param array $options |
259
|
|
|
* @param array $target_additional_data |
260
|
|
|
* @return $this |
261
|
|
|
* @throws Exception |
262
|
|
|
*/ |
263
|
|
|
public function updateWithSync(Request $request, array $additionalData = [], array $options = [], array $target_additional_data = []) |
264
|
|
|
{ |
265
|
|
|
$this->initDataForSync($request, $additionalData, $options, $target_additional_data); |
266
|
|
|
$this->storeEditAllItems($request, 'update', $options); |
267
|
|
|
$this->processAllRelationships($request, 'update', '', '', $options); |
268
|
|
|
|
269
|
|
|
//Dispatch the update event |
270
|
|
|
$this->fireModelEvent('updateWithSync'); |
271
|
|
|
|
272
|
|
|
return $this->fresh(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param $method |
277
|
|
|
* @param $modelTarget |
278
|
|
|
* @param $methodOnTarget |
279
|
|
|
* @param bool $is_EO |
280
|
|
|
* @param bool $is_EO_target |
281
|
|
|
* @param bool $is_EM_target |
282
|
|
|
*/ |
283
|
|
|
public function deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO, $is_EO_target, $is_EM_target) |
284
|
|
|
{ |
285
|
|
|
if ($is_EO) { |
286
|
|
|
$embedObj = $this->$method; |
287
|
|
|
if (! is_null($embedObj)) { |
288
|
|
|
$target_id = $embedObj->ref_id; |
289
|
|
|
$this->handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target); |
290
|
|
|
} |
291
|
|
|
} else { |
292
|
|
|
foreach ($this->$method as $target) { |
293
|
|
|
$this->handleSubTarget($target->ref_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param $target_id |
300
|
|
|
* @param $modelTarget |
301
|
|
|
* @param $methodOnTarget |
302
|
|
|
* @param bool $is_EO_target |
303
|
|
|
* @param bool $is_EM_target |
304
|
|
|
*/ |
305
|
|
|
public function handleSubTarget($target_id, $modelTarget, $methodOnTarget, $is_EO_target, $is_EM_target) |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
if ($is_EM_target) { |
308
|
|
|
$target = new $modelTarget; |
309
|
|
|
$target = $target->all()->where('id', $target_id)->first(); |
310
|
|
|
if (! is_null($target)) { |
311
|
|
|
$new_values = []; |
312
|
|
|
foreach ($target->$methodOnTarget as $temp) { |
313
|
|
|
if ($temp->ref_id !== $this->getId()) { |
|
|
|
|
314
|
|
|
$new_values[] = $temp->attributes; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
$target->$methodOnTarget = $new_values; |
318
|
|
|
$target->save(); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return $this |
325
|
|
|
*/ |
326
|
|
|
public function destroyWithSync() |
327
|
|
|
{ |
328
|
|
|
//Get the relation info |
329
|
|
|
$relations = $this->getMongoRelation(); |
330
|
|
|
|
331
|
|
|
//Process all relationships |
332
|
|
|
foreach ($relations as $method => $relation) { |
333
|
|
|
//Get Relation Save Mode |
334
|
|
|
$type = $relation['type']; |
335
|
|
|
$hasTarget = hasTarget($relation); |
336
|
|
|
if ($hasTarget) { |
337
|
|
|
$modelTarget = $relation['modelTarget']; |
338
|
|
|
$methodOnTarget = $relation['methodOnTarget']; |
339
|
|
|
$modelOnTarget = $relation['modelOnTarget']; |
|
|
|
|
340
|
|
|
|
341
|
|
|
$is_EO = is_EO($type); |
342
|
|
|
$is_EM = is_EM($type); |
343
|
|
|
$is_HO = is_HO($type); |
|
|
|
|
344
|
|
|
$is_HM = is_HM($type); |
|
|
|
|
345
|
|
|
|
346
|
|
|
if ($is_EO || $is_EM) {//EmbedsOne Create - EmbedsMany Create |
347
|
|
|
//Delete EmbedsMany or EmbedsOne on Target |
348
|
|
|
$this->deleteTargetObj($method, $modelTarget, $methodOnTarget, $is_EO); |
|
|
|
|
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
//TODO: Need to be implemented |
352
|
|
|
/* elseif ($is_HM) {//HasMany |
353
|
|
|
} elseif ($is_HO) {//HasOne Create |
354
|
|
|
}*/ |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
//Delete current object |
358
|
|
|
$this->delete(); |
|
|
|
|
359
|
|
|
|
360
|
|
|
//Dispatch the destroy event |
361
|
|
|
$this->fireModelEvent('destroyWithSync'); |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param array $options |
368
|
|
|
* @param string $key |
369
|
|
|
* @return bool|mixed |
370
|
|
|
*/ |
371
|
|
|
private function getOptionValue(array $options, string $key) |
372
|
|
|
{ |
373
|
|
|
return Arr::has($options, $key) ? $options[$key] : ''; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @param $obj |
378
|
|
|
* @param string $EOkey |
379
|
|
|
* @param string $method |
380
|
|
|
* @param string $model |
381
|
|
|
* @throws Exception |
382
|
|
|
*/ |
383
|
|
|
public function checkPropertyExistence($obj, string $EOkey, $method = '', $model = '') |
384
|
|
|
{ |
385
|
|
|
if (! property_exists($obj, $EOkey)) { |
386
|
|
|
$msg = 'Error - '.$EOkey.' attribute not found on obj '.json_encode($obj).' during save of model: '.$model.' and attribute: '.$method; |
387
|
|
|
throw new Exception($msg); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param $arr |
393
|
|
|
* @param string $key |
394
|
|
|
* @throws Exception |
395
|
|
|
*/ |
396
|
|
|
public function checkArrayExistence($arr, string $key) |
397
|
|
|
{ |
398
|
|
|
if (! Arr::has($arr, $key)) { |
399
|
|
|
$msg = ('Error - '.$key.' attribute not found on obj '.json_encode($arr)); |
400
|
|
|
throw new Exception($msg); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @param Request $request |
406
|
|
|
* @param string $key |
407
|
|
|
* @throws Exception |
408
|
|
|
*/ |
409
|
|
|
private function checkRequestExistence(Request $request, string $key) |
410
|
|
|
{ |
411
|
|
|
if (! $request->has($key)) { |
412
|
|
|
$msg = ('Error - '.$key.' attribute not found in Request '.json_encode($request->all())); |
413
|
|
|
throw new Exception($msg); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @param bool $request_has_key |
419
|
|
|
* @param bool $hasTarget |
420
|
|
|
* @return bool |
421
|
|
|
*/ |
422
|
|
|
public function getIsSkippable($request_has_key, $hasTarget = false) |
423
|
|
|
{ |
424
|
|
|
return ! $request_has_key && $this->getHasPartialRequest() && ! $hasTarget; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @return bool |
429
|
|
|
*/ |
430
|
|
|
public function getHasPartialRequest() |
431
|
|
|
{ |
432
|
|
|
return $this->has_partial_request; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
public function setHasPartialRequest(): void |
436
|
|
|
{ |
437
|
|
|
$this->has_partial_request = $this->getOptionValue( |
438
|
|
|
$this->getOptions(), |
439
|
|
|
'request_type' |
440
|
|
|
) == 'partial'; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @param Request $request |
445
|
|
|
* @param $obj |
446
|
|
|
* @param $type |
447
|
|
|
* @param $model |
448
|
|
|
* @param $method |
449
|
|
|
* @param $event |
450
|
|
|
* @param $is_EO |
451
|
|
|
* @param $is_EM |
452
|
|
|
* @param $i |
453
|
|
|
* @param $options |
454
|
|
|
* @throws Exception |
455
|
|
|
*/ |
456
|
|
|
private function processEmbedOnCurrentCollection(Request $request, $obj, $type, $model, $method, $event, $is_EO, $is_EM, $i, $options) |
|
|
|
|
457
|
|
|
{ |
458
|
|
|
//Init the embed one model |
459
|
|
|
$embedObj = new $model; |
460
|
|
|
|
461
|
|
|
$EOitems = $embedObj->getItems(); |
462
|
|
|
//Current Obj Create |
463
|
|
|
foreach ($EOitems as $EOkey => $item) { |
464
|
|
|
if (! is_null($obj)) { |
465
|
|
|
$is_ML = isML($item); |
466
|
|
|
$is_MD = isMD($item); |
467
|
|
|
$this->checkPropertyExistence($obj, $EOkey, $method, $model); |
468
|
|
|
|
469
|
|
|
if ($is_ML) { |
470
|
|
|
$embedObj->$EOkey = ml([], $obj->$EOkey); |
471
|
|
|
} elseif ($EOkey == 'updated_at' || $EOkey == 'created_at') { |
472
|
|
|
$embedObj->$EOkey = now(); |
473
|
|
|
} elseif ($is_MD) { |
474
|
|
|
if ($obj->$EOkey == '' || $obj->$EOkey == null) { |
475
|
|
|
$embedObj->$EOkey = null; |
476
|
|
|
} else { |
477
|
|
|
$embedObj->$EOkey = new UTCDateTime(new DateTime($obj->$EOkey)); |
|
|
|
|
478
|
|
|
} |
479
|
|
|
} else { |
480
|
|
|
$embedObj->$EOkey = $obj->$EOkey; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
//else if($is_EM){//To be implemented} |
486
|
|
|
//else if($is_HM){//To be implemented} |
487
|
|
|
//else if($is_HO){//To be implemented} |
488
|
|
|
|
489
|
|
|
//Get counter for embeds many with level > 1 |
490
|
|
|
$counter = getCounterForRelationships($method, $is_EO, $is_EM, $i); |
491
|
|
|
//Check for another Level of Relationship |
492
|
|
|
$embedObj->processAllRelationships($request, $event, $method.'-', $counter, $options); |
493
|
|
|
|
494
|
|
|
if ($is_EO) { |
495
|
|
|
$this->$method = $embedObj->attributes; |
496
|
|
|
} else { |
497
|
|
|
$this->tempEM[] = $embedObj->attributes; |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param $modelTarget |
503
|
|
|
* @param $obj |
504
|
|
|
* @param $methodOnTarget |
505
|
|
|
* @param $modelOnTarget |
506
|
|
|
* @param bool $is_EO_target |
507
|
|
|
* @param bool $is_EM_target |
508
|
|
|
* @throws Exception |
509
|
|
|
*/ |
510
|
|
|
private function processEmbedOnTargetCollection($modelTarget, $obj, $methodOnTarget, $modelOnTarget, $is_EO_target, $is_EM_target) |
511
|
|
|
{ |
512
|
|
|
$modelToBeSync = $this->getModelTobeSync($modelTarget, $obj); |
513
|
|
|
if (! is_null($modelToBeSync)) { |
514
|
|
|
$miniModel = $this->getEmbedModel($modelOnTarget); |
|
|
|
|
515
|
|
|
$modelToBeSync->updateRelationWithSync($miniModel, $methodOnTarget, $is_EO_target, $is_EM_target); |
516
|
|
|
//TODO:Sync target on level > 1 |
517
|
|
|
//$modelToBeSync->processAllRelationships($request, $event, $methodOnTarget, $methodOnTarget . "-"); |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* @param string $modelTarget |
523
|
|
|
* @param stdClass $obj |
524
|
|
|
* @return MDModel |
525
|
|
|
* @throws Exception |
526
|
|
|
*/ |
527
|
|
|
private function getModelTobeSync(string $modelTarget, stdClass $obj) |
528
|
|
|
{ |
529
|
|
|
$this->checkPropertyExistence($obj, 'ref_id'); |
530
|
|
|
$target_id = $obj->ref_id; |
531
|
|
|
|
532
|
|
|
//Init the Target Model |
533
|
|
|
$modelToBeSync = new $modelTarget; |
534
|
|
|
|
535
|
|
|
return $modelToBeSync->find($target_id); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* @param string $key |
540
|
|
|
* @param Request $request |
541
|
|
|
* @return mixed |
542
|
|
|
* @throws Exception |
543
|
|
|
*/ |
544
|
|
|
private function getRelationshipRequest(string $key, Request $request) |
545
|
|
|
{ |
546
|
|
|
$this->checkRequestExistence( |
547
|
|
|
$request, |
548
|
|
|
$key |
549
|
|
|
); |
550
|
|
|
|
551
|
|
|
return $request->input($key); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* @return Request |
556
|
|
|
*/ |
557
|
|
|
public function getRequest() |
558
|
|
|
{ |
559
|
|
|
return $this->request; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @param Request $request |
564
|
|
|
* @param array $additionalData |
565
|
|
|
*/ |
566
|
|
|
public function setRequest(Request $request, array $additionalData): void |
567
|
|
|
{ |
568
|
|
|
$request = $request->merge($additionalData); |
569
|
|
|
$this->request = $request; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* @return Request |
574
|
|
|
*/ |
575
|
|
|
public function getPartialGeneratedRequest() |
576
|
|
|
{ |
577
|
|
|
return $this->partial_generated_request; |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* @param array $arr |
582
|
|
|
*/ |
583
|
|
|
public function setPartialGeneratedRequest(array $arr): void |
584
|
|
|
{ |
585
|
|
|
$partial_generated_request = new Request; |
586
|
|
|
$partial_generated_request->merge($arr); |
587
|
|
|
|
588
|
|
|
$this->partial_generated_request = $partial_generated_request; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* @return array |
593
|
|
|
*/ |
594
|
|
|
public function getOptions() |
595
|
|
|
{ |
596
|
|
|
return $this->options; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* @param array $options |
601
|
|
|
*/ |
602
|
|
|
public function setOptions(array $options): void |
603
|
|
|
{ |
604
|
|
|
$this->options = $options; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* @return array |
609
|
|
|
*/ |
610
|
|
|
public function getTargetAdditionalData() |
611
|
|
|
{ |
612
|
|
|
return $this->target_additional_data; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* @param array $target_additional_data |
617
|
|
|
*/ |
618
|
|
|
public function setTargetAdditionalData($target_additional_data): void |
619
|
|
|
{ |
620
|
|
|
$this->target_additional_data = $target_additional_data; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* @param Request $request |
625
|
|
|
* @param array $additionalData |
626
|
|
|
* @param array $options |
627
|
|
|
* @param array $target_additional_data |
628
|
|
|
*/ |
629
|
|
|
public function initDataForSync(Request $request, array $additionalData, array $options, array $target_additional_data) |
630
|
|
|
{ |
631
|
|
|
$this->setRequest($request, $additionalData); |
632
|
|
|
$this->setTargetAdditionalData($target_additional_data); |
633
|
|
|
$this->setOptions($options); |
634
|
|
|
$this->setHasPartialRequest(); |
635
|
|
|
} |
636
|
|
|
} |
637
|
|
|
|