-
Notifications
You must be signed in to change notification settings - Fork 0
/
HasClears.php
85 lines (74 loc) · 1.77 KB
/
HasClears.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
<?php
namespace BiiiiiigMonster\Clearable\Concerns;
use BiiiiiigMonster\Clearable\ClearManager;
use Illuminate\Database\Eloquent\Model;
/**
* Trait HasClears
*
* @property array $clears The relationships that will be auto-cleared when deleted.
* @property string|bool|null $clearQueue The clearable that will be dispatch on this name queue.
*/
trait HasClears
{
/**
* Auto register clears.
*/
protected static function bootHasClears(): void
{
static::deleted(static fn (Model $model) => ClearManager::make($model)->handle());
}
/**
* Get clears.
*
* @return array
*/
public function getClears(): array
{
return $this->clears ?? [];
}
/**
* Set the clears attributes for the model.
*
* @param array $clears
* @return $this
*/
public function setClears(array $clears): static
{
$this->clears = $clears;
return $this;
}
/**
* Make the given, typically visible, attributes clears.
*
* @param array|string|null $clears
* @return $this
*/
public function clear(array|string|null $clears): static
{
$this->clears = array_merge(
$this->getClears(),
is_array($clears) ? $clears : func_get_args()
);
return $this;
}
/**
* Get clearQueue.
*
* @return string|bool|null
*/
public function getClearQueue(): string|bool|null
{
return $this->clearQueue;
}
/**
* Set the clearQueue attributes for the model.
*
* @param string|bool|null $clearQueue
* @return $this
*/
public function setClearQueue(string|bool|null $clearQueue): static
{
$this->clearQueue = $clearQueue;
return $this;
}
}