1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreates\Projects\Traits; |
4
|
|
|
|
5
|
|
|
use Chriscreates\Projects\Models\Priority; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
View Code Duplication |
trait HasPriority |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Check the priority of the given model. |
12
|
|
|
* |
13
|
|
|
* @param string|int|\Chriscreates\Projects\Models\Priority $priority |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function hasPriority($priority) : bool |
17
|
|
|
{ |
18
|
|
|
if (is_null($this->priority)) { |
|
|
|
|
19
|
|
|
return false; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if (is_int($priority) || is_string($priority)) { |
23
|
|
|
$priority = Priority::findByIdOrName($priority); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (is_null($priority)) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($priority instanceof Priority) { |
31
|
|
|
$priority = $priority->id; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->priority_id === $priority; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if the given model has any priority. |
39
|
|
|
* |
40
|
|
|
* @param array $priorities |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
public function hasAnyPriority(array $priorities) : bool |
44
|
|
|
{ |
45
|
|
|
return ! collect($priorities)->reject(function ($priority, $key) { |
|
|
|
|
46
|
|
|
return ! $this->hasPriority($priority); |
47
|
|
|
})->isEmpty(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the priority of the given model. |
52
|
|
|
* |
53
|
|
|
* @param void $priority |
54
|
|
|
*/ |
55
|
|
|
public function setPriority($priority) : void |
56
|
|
|
{ |
57
|
|
|
if (is_int($priority) || is_string($priority)) { |
58
|
|
|
$priority = Priority::findByIdOrName($priority); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (is_null($priority)) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->priority()->save($priority); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Scope - return any model where priority. |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
72
|
|
|
* @param string|int|\Chriscreates\Projects\Models\Priority $priority |
73
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
74
|
|
|
*/ |
75
|
|
|
public function scopeWhereHasPriority(Builder $query, $priority) |
76
|
|
|
{ |
77
|
|
|
$key = ! is_string($priority) ? 'id' : 'name'; |
78
|
|
|
|
79
|
|
|
$priority = $priority instanceof Priority ? $priority->id : $priority; |
80
|
|
|
|
81
|
|
|
return $query->whereHas('priority', function ($query) use ($key, $priority) { |
82
|
|
|
return $query->where($key, $priority); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Scope - return any model whereIn priorities. |
88
|
|
|
* |
89
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
90
|
|
|
* @param string $key |
91
|
|
|
* @param array $priorities |
92
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
93
|
|
|
*/ |
94
|
|
|
public function scopeWhereHasAnyPriority(Builder $query, string $key, array $priorities) |
95
|
|
|
{ |
96
|
|
|
return $query->whereHas('priority', function ($query) use ($key, $priorities) { |
97
|
|
|
return $query->whereIn($key, $priorities); |
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: