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
|
|
|
'path' => '', |
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
|
|
|
'path' => '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
|
|
|
'visibility_level', |
62
|
|
|
'tags', |
63
|
|
|
'path', |
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
|
|
|
'visibility_level' => 'int|required', |
77
|
|
|
'path' => 'required|string|max:5', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Projects can belong to a group. |
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
|
|
|
* Projects can belong to a namespace. |
92
|
|
|
* |
93
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
94
|
|
|
*/ |
95
|
|
|
public function projectNamespace() |
96
|
|
|
{ |
97
|
|
|
return $this->belongsTo(ProjectNamespace::class, 'namespace_id', 'id'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Lookup all of the issues reported on the project. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
104
|
|
|
*/ |
105
|
|
|
public function issues() |
106
|
|
|
{ |
107
|
|
|
return $this->hasMany(Issue::class, 'project_id', 'id'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Projects can have many tags. |
112
|
|
|
* |
113
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
114
|
|
|
*/ |
115
|
|
|
public function tags() |
116
|
|
|
{ |
117
|
|
|
return $this->belongsToMany(Tag::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Finds all projects by visibility_level. |
122
|
|
|
* |
123
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
124
|
|
|
* @param int $visibility_level |
125
|
|
|
* |
126
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
127
|
|
|
*/ |
128
|
|
|
public function scopeVisibilityLevel(Builder $query, $visibility_level) |
129
|
|
|
{ |
130
|
|
|
return $query->where('visibility_level', $visibility_level); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Finds all projects which don't have the given visibility_level. |
135
|
|
|
* |
136
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
137
|
|
|
* @param int $visibility_level |
138
|
|
|
* |
139
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
140
|
|
|
*/ |
141
|
|
|
public function scopeNotVisibilityLevel(Builder $query, $visibility_level) |
142
|
|
|
{ |
143
|
|
|
return $query->where('visibility_level', '<>', $visibility_level); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Finds all projects which are enabled. |
148
|
|
|
* |
149
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
150
|
|
|
* |
151
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
152
|
|
|
*/ |
153
|
|
|
public function scopeEnabled(Builder $query) |
154
|
|
|
{ |
155
|
|
|
return $query->where('enabled', true); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Finds all projects which are disabled. |
160
|
|
|
* |
161
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
162
|
|
|
* |
163
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
164
|
|
|
*/ |
165
|
|
|
public function scopeDisabled(Builder $query) |
166
|
|
|
{ |
167
|
|
|
return $query->where('enabled', false); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Looks up the human readable version of the visibility_level. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getHumanVisibilityLevelAttribute() |
176
|
|
|
{ |
177
|
|
|
return trans('gitamin.projects.status.'.$this->visibility_level); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns the namespace on this project. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getNamespaceAttribute() |
186
|
|
|
{ |
187
|
|
|
return $this->projectNamespace->path; |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns all of the tags on this project. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public function getTagsListAttribute() |
196
|
|
|
{ |
197
|
|
|
$tags = $this->tags->map(function ($tag) { |
|
|
|
|
198
|
|
|
return $tag->name; |
199
|
|
|
}); |
200
|
|
|
|
201
|
|
|
return implode(', ', $tags->toArray()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the presenter class. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getPresenterClass() |
210
|
|
|
{ |
211
|
|
|
return ProjectPresenter::class; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.