|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ModelStatus; |
|
4
|
|
|
|
|
5
|
|
|
use DB; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Spatie\ModelStatus\Exceptions\InvalidStatus; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
9
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
|
10
|
|
|
|
|
11
|
|
|
trait HasStatuses |
|
12
|
|
|
{ |
|
13
|
|
|
public function statuses(): MorphMany |
|
14
|
|
|
{ |
|
15
|
|
|
return $this->morphMany($this->getStatusModelClassName(), 'model')->latest(); |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function status(): ?Status |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->latestStatus(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setStatus(string $name, string $reason = ''): self |
|
24
|
|
|
{ |
|
25
|
|
|
if (! $this->isValidStatus($name, $reason)) { |
|
26
|
|
|
throw InvalidStatus::create($name, $reason); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->statuses()->create([ |
|
30
|
|
|
'name' => $name, |
|
31
|
|
|
'reason' => $reason, |
|
32
|
|
|
]); |
|
33
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function isValidStatus(string $name, string $reason = ''): bool |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string|array $names |
|
44
|
|
|
* |
|
45
|
|
|
* @return null|Status |
|
46
|
|
|
*/ |
|
47
|
|
|
public function latestStatus(...$names): ?Status |
|
48
|
|
|
{ |
|
49
|
|
|
$names = is_array($names) ? array_flatten($names) : func_get_args(); |
|
50
|
|
|
|
|
51
|
|
|
if (count($names) < 1) { |
|
52
|
|
|
return $this->statuses()->orderByDesc('id')->first(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->statuses()->whereIn('name', $names)->orderByDesc('id')->first(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function scopeCurrentStatus(Builder $builder, string $name) |
|
59
|
|
|
{ |
|
60
|
|
|
$builder |
|
61
|
|
|
->whereHas('statuses', function (Builder $query) use ($name) { |
|
62
|
|
|
$query |
|
63
|
|
|
->where('name', $name) |
|
64
|
|
|
->whereIn('id', function (QueryBuilder $query) use ($name) { |
|
65
|
|
|
$query |
|
66
|
|
|
->select(DB::raw('max(id)')) |
|
67
|
|
|
->from($this->getStatusTableName()) |
|
68
|
|
|
->where('model_type', static::class) |
|
69
|
|
|
->groupBy('model_id'); |
|
70
|
|
|
}); |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function scopeWithoutStatus(Builder $builder) |
|
75
|
|
|
{ |
|
76
|
|
|
$builder->whereDoesntHave('statuses'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getStatusAttribute(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return (string) $this->latestStatus(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function getStatusTableName(): string |
|
85
|
|
|
{ |
|
86
|
|
|
$modelClass = $this->getStatusModelClassName(); |
|
87
|
|
|
|
|
88
|
|
|
return (new $modelClass)->getTable(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function getStatusModelClassName(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return config('model-status.status_model'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.