1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreates\Projects\Traits; |
4
|
|
|
|
5
|
|
|
use Chriscreates\Projects\Models\Status; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
View Code Duplication |
trait HasStatus |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Check the status of the given model. |
12
|
|
|
* |
13
|
|
|
* @param string|int|\Chriscreates\Projects\Models\Status $status |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function hasStatus($status) : bool |
17
|
|
|
{ |
18
|
|
|
if (is_null($this->status)) { |
|
|
|
|
19
|
|
|
return false; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if (is_int($status) || is_string($status)) { |
23
|
|
|
$status = Status::findByIdOrName($status); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (is_null($status)) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($status instanceof Status) { |
31
|
|
|
$status = $status->id; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->status_id === $status; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if the given model has any status. |
39
|
|
|
* |
40
|
|
|
* @param array $statuses |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
public function hasAnyStatus(array $statuses) : bool |
44
|
|
|
{ |
45
|
|
|
return ! collect($statuses)->reject(function ($status, $key) { |
|
|
|
|
46
|
|
|
return ! $this->hasStatus($status); |
47
|
|
|
})->isEmpty(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the status of the given model. |
52
|
|
|
* |
53
|
|
|
* @param void $status |
54
|
|
|
*/ |
55
|
|
|
public function setStatus($status) : void |
56
|
|
|
{ |
57
|
|
|
if (is_int($status) || is_string($status)) { |
58
|
|
|
$status = Status::findByIdOrName($status); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (is_null($status)) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->status()->save($status); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Scope - return any model where status. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
72
|
|
|
* @param string|int|\Chriscreates\Projects\Models\Status $status |
73
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
74
|
|
|
*/ |
75
|
|
|
public function scopeWhereHasStatus(Builder $query, $status) |
76
|
|
|
{ |
77
|
|
|
$key = ! is_string($status) ? 'id' : 'name'; |
78
|
|
|
|
79
|
|
|
$status = $status instanceof Status ? $status->id : $status; |
80
|
|
|
|
81
|
|
|
return $query->whereHas('status', function ($query) use ($key, $status) { |
82
|
|
|
return $query->where($key, $status); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Scope - return any model whereIn statuses. |
88
|
|
|
* |
89
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
90
|
|
|
* @param string $key |
91
|
|
|
* @param array $statuses |
92
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
93
|
|
|
*/ |
94
|
|
|
public function scopeWhereHasAnyStatus(Builder $query, string $key, array $statuses) |
95
|
|
|
{ |
96
|
|
|
return $query->whereHas('status', function ($query) use ($key, $statuses) { |
97
|
|
|
return $query->whereIn($key, $statuses); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: