-
Notifications
You must be signed in to change notification settings - Fork 554
/
HasRelated.php
120 lines (102 loc) · 3.77 KB
/
HasRelated.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace A17\Twill\Models\Behaviors;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\RelatedItem;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
trait HasRelated
{
protected array $relatedCache;
/**
* Defines the one-to-many relationship for related items.
*/
public function relatedItems(): MorphMany
{
return $this->morphMany(RelatedItem::class, 'subject')->orderBy('position');
}
/**
* Returns the related items for a browser field.
*/
public function getRelated(string $browserName): Collection
{
if (! isset($this->relatedCache[$browserName]) || $this->relatedCache[$browserName] === null) {
$this->loadRelated($browserName);
}
return $this->relatedCache[$browserName];
}
public function getFirstRelated(string $browserName): mixed
{
return $this->getRelated($browserName)->first();
}
/**
* Eager load related items for a browser field.
*/
public function loadRelated(string $browserName): Collection
{
if (! isset($this->relatedItems)) {
$this->load('relatedItems');
}
return $this->relatedCache[$browserName] = $this->relatedItems
->where('browser_name', $browserName)
->map(function ($item) {
/** @var \A17\Twill\Models\Model $model */
if ($model = $item->related) {
$model->setRelation('pivot', $item);
return $model;
}
return null;
})->filter();
}
/**
* Attach items to the model for a browser field.
*
* @param array<int, TwillModelContract> $items
*/
public function saveRelated(array|Collection $items, string $browserName): void
{
/** @var Collection<int, RelatedItem> $itemsToProcess */
$itemsToProcess = $this->relatedItems()->where('browser_name', $browserName)->get();
foreach ($items as $position => $item) {
$firstMatchKey = $itemsToProcess
->where('related_id', $item['id'])
->where('related_type', $item['endpointType'])
->where('browser_name', $browserName)
// We should only have one item always as you cannot select the same items twice.
->keys()
->first();
// This needs to be a strict "null" comparison, as it would otherwise pass on key 0.
if ($firstMatchKey !== null) {
$match = $itemsToProcess[$firstMatchKey];
$match->position = $position + 1;
if ($match->isDirty('position')) {
$match->save();
}
} else {
RelatedItem::create([
'subject_id' => $this->getKey(),
'subject_type' => $this->getMorphClass(),
'related_id' => $item['id'],
'related_type' => $item['endpointType'],
'browser_name' => $browserName,
'position' => $position + 1,
]);
}
// Unset the item, this way we have only items to delete left.
$itemsToProcess->offsetUnset($firstMatchKey);
}
RelatedItem::whereIn('id', $itemsToProcess->pluck('id')->toArray())->delete();
}
public function clearRelated(string $browserName): void
{
RelatedItem::where([
'browser_name' => $browserName,
'subject_id' => $this->getKey(),
'subject_type' => $this->getMorphClass(),
])->delete();
}
public function clearAllRelated(): void
{
$this->relatedItems()->delete();
}
}