-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocument.php
121 lines (107 loc) · 3.11 KB
/
Document.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace App\Models;
use Database\Factories\DocumentFactory;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Carbon;
/**
* App\Models\Document
*
* @property int $id
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property int $type
* @property int $is_required
* @property DocumentState|null $state
* @property string $name
* @property int $owner_id
* @property string|null $path
* @property DocumentCategory|null $category
* @property-read Model|Eloquent $documentable
* @property-read User|null $owner
*
* @method static DocumentFactory factory(...$parameters)
* @method static Builder|Document newModelQuery()
* @method static Builder|Document newQuery()
* @method static Builder|Document query()
* @method static Builder|Document whereCategory($value)
* @method static Builder|Document whereCreatedAt($value)
* @method static Builder|Document whereId($value)
* @method static Builder|Document whereIsRequired($value)
* @method static Builder|Document whereName($value)
* @method static Builder|Document whereOwnerId($value)
* @method static Builder|Document wherePath($value)
* @method static Builder|Document whereState($value)
* @method static Builder|Document whereType($value)
* @method static Builder|Document whereUpdatedAt($value)
*
* @property-read Collection|Comment[] $comments
* @property-read int|null $comments_count
*
* @mixin Eloquent
*/
class Document extends Model
{
use HasFactory;
/**
* Possible Document types
*/
public const TYPE_DIGITAL = 0;
public const TYPE_ANALOG = 1;
protected $casts = [
'state' => DocumentState::class,
'category' => DocumentCategory::class,
];
protected $fillable = [
'category',
'type',
'name',
'state',
];
public function documentable(): MorphTo
{
return $this->morphTo();
}
/**
* Indicates whether the document is approved.
*/
public function isApproved(): bool
{
return $this->state == DocumentState::Approved;
}
/**
* Indicates whether the document is submitted.
*/
public function isSubmitted(): bool
{
return $this->state == DocumentState::Submitted;
}
/**
* @return BelongsTo<User, Document>
*/
public function owner(): BelongsTo
{
return $this->belongsTo(User::class, 'owner_id');
}
public function createComment(string $withContent, mixed $authorId): Comment|bool
{
$comment = new Comment([
'content' => $withContent,
'author_id' => $authorId,
]);
return $this->comments()->save($comment);
}
/**
* @retrun HasMany<Comment>
*/
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
}