1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gitamin\Models; |
13
|
|
|
|
14
|
|
|
use AltThree\Validator\ValidatingTrait; |
15
|
|
|
use Gitamin\Presenters\ProjectPresenter; |
16
|
|
|
use Illuminate\Database\Eloquent\Builder; |
17
|
|
|
use Illuminate\Database\Eloquent\Model; |
18
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
19
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter; |
20
|
|
|
|
21
|
|
|
class Project extends Model implements HasPresenter |
22
|
|
|
{ |
23
|
|
|
use SoftDeletes, ValidatingTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* List of attributes that have default values. |
27
|
|
|
* |
28
|
|
|
* @var mixed[] |
29
|
|
|
*/ |
30
|
|
|
protected $attributes = [ |
31
|
|
|
'order' => 0, |
32
|
|
|
'namespace_id' => 0, |
33
|
|
|
'description' => '', |
34
|
|
|
'slug' => '', |
35
|
|
|
'enabled' => true, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The attributes that should be casted to native types. |
40
|
|
|
* |
41
|
|
|
* @var string[] |
42
|
|
|
*/ |
43
|
|
|
protected $casts = [ |
44
|
|
|
'id' => 'int', |
45
|
|
|
'order' => 'int', |
46
|
|
|
'namespace_id' => 'int', |
47
|
|
|
'description' => 'string', |
48
|
|
|
'slug' => 'string', |
49
|
|
|
'deleted_at' => 'date', |
50
|
|
|
'enabled' => 'bool', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The fillable properties. |
55
|
|
|
* |
56
|
|
|
* @var string[] |
57
|
|
|
*/ |
58
|
|
|
protected $fillable = [ |
59
|
|
|
'name', |
60
|
|
|
'description', |
61
|
|
|
'status', |
62
|
|
|
'tags', |
63
|
|
|
'slug', |
64
|
|
|
'order', |
65
|
|
|
'namespace_id', |
66
|
|
|
'enabled', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The validation rules. |
71
|
|
|
* |
72
|
|
|
* @var string[] |
73
|
|
|
*/ |
74
|
|
|
public $rules = [ |
75
|
|
|
'name' => 'required|string', |
76
|
|
|
'status' => 'int|required', |
77
|
|
|
'slug' => 'required|string', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Projects can belong to a team. |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
84
|
|
|
*/ |
85
|
|
|
public function group() |
86
|
|
|
{ |
87
|
|
|
return $this->belongsTo(Group::class, 'namespace_id', 'id'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Lookup all of the issues reported on the project. |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
94
|
|
|
*/ |
95
|
|
|
public function issues() |
96
|
|
|
{ |
97
|
|
|
return $this->hasMany(Issue::class, 'project_id', 'id'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Projects can have many tags. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
104
|
|
|
*/ |
105
|
|
|
public function tags() |
106
|
|
|
{ |
107
|
|
|
return $this->belongsToMany(Tag::class); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Finds all projects by status. |
112
|
|
|
* |
113
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
114
|
|
|
* @param int $status |
115
|
|
|
* |
116
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
117
|
|
|
*/ |
118
|
|
|
public function scopeStatus(Builder $query, $status) |
119
|
|
|
{ |
120
|
|
|
return $query->where('status', $status); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Finds all projects which don't have the given status. |
125
|
|
|
* |
126
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
127
|
|
|
* @param int $status |
128
|
|
|
* |
129
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
130
|
|
|
*/ |
131
|
|
|
public function scopeNotStatus(Builder $query, $status) |
132
|
|
|
{ |
133
|
|
|
return $query->where('status', '<>', $status); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Finds all projects which are enabled. |
138
|
|
|
* |
139
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
140
|
|
|
* |
141
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
142
|
|
|
*/ |
143
|
|
|
public function scopeEnabled(Builder $query) |
144
|
|
|
{ |
145
|
|
|
return $query->where('enabled', true); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Finds all projects which are disabled. |
150
|
|
|
* |
151
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
152
|
|
|
* |
153
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
154
|
|
|
*/ |
155
|
|
|
public function scopeDisabled(Builder $query) |
156
|
|
|
{ |
157
|
|
|
return $query->where('enabled', false); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Looks up the human readable version of the status. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getHumanStatusAttribute() |
166
|
|
|
{ |
167
|
|
|
return trans('gitamin.projects.status.'.$this->status); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns all of the tags on this project. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getTagsListAttribute() |
176
|
|
|
{ |
177
|
|
|
$tags = $this->tags->map(function ($tag) { |
|
|
|
|
178
|
|
|
return $tag->name; |
179
|
|
|
}); |
180
|
|
|
|
181
|
|
|
return implode(', ', $tags->toArray()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the presenter class. |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getPresenterClass() |
190
|
|
|
{ |
191
|
|
|
return ProjectPresenter::class; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.