1 | <?php |
||
13 | trait HasStatuses |
||
14 | { |
||
15 | public function statuses(): MorphMany |
||
16 | { |
||
17 | return $this->morphMany($this->getStatusModelClassName(), 'model')->latest('id'); |
||
|
|||
18 | } |
||
19 | |||
20 | public function status(): ?Status |
||
21 | { |
||
22 | return $this->latestStatus(); |
||
23 | } |
||
24 | |||
25 | public function setStatus(string $name, string $reason = ''): self |
||
26 | { |
||
27 | if (! $this->isValidStatus($name, $reason)) { |
||
28 | throw InvalidStatus::create($name); |
||
29 | } |
||
30 | |||
31 | return $this->forceSetStatus($name, $reason); |
||
32 | } |
||
33 | |||
34 | public function isValidStatus(string $name, string $reason = ''): bool |
||
38 | |||
39 | /** |
||
40 | * @param string|array $names |
||
41 | * |
||
42 | * @return null|Status |
||
43 | */ |
||
44 | public function latestStatus(...$names): ?Status |
||
56 | |||
57 | public function scopeCurrentStatus(Builder $builder, ...$names) |
||
75 | |||
76 | /** |
||
77 | * @param string|array $names |
||
78 | * |
||
79 | * @return void |
||
80 | **/ |
||
81 | public function scopeOtherCurrentStatus(Builder $builder, ...$names) |
||
100 | |||
101 | public function getStatusAttribute(): string |
||
105 | |||
106 | public function forceSetStatus(string $name, string $reason = ''): self |
||
119 | |||
120 | protected function getStatusTableName(): string |
||
126 | |||
127 | protected function getStatusModelClassName(): string |
||
131 | |||
132 | protected function getStatusModelType(): string |
||
136 | } |
||
137 |
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
Idable
provides a methodequalsId
that 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.