1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Eloquent Flag. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Flag\Traits\Classic; |
15
|
|
|
|
16
|
|
|
trait HasPublishedFlagHelpers |
17
|
|
|
{ |
18
|
|
|
public function initializeHasPublishedFlagHelpers(): void |
19
|
|
|
{ |
20
|
|
|
$this->casts['is_published'] = 'boolean'; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set published flag. |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
|
|
public function setPublishedFlag() |
29
|
|
|
{ |
30
|
|
|
$this->setAttribute('is_published', true); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Unset published flag. |
37
|
|
|
* |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
|
|
public function unsetPublishedFlag() |
41
|
|
|
{ |
42
|
|
|
$this->setAttribute('is_published', false); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* If entity is published. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function isPublished(): bool |
53
|
|
|
{ |
54
|
|
|
return $this->getAttributeValue('is_published'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* If entity is unpublished. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function isUnpublished(): bool |
63
|
|
|
{ |
64
|
|
|
return !$this->isPublished(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Mark entity as published. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function publish(): void |
73
|
|
|
{ |
74
|
|
|
$this->setPublishedFlag()->save(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->fireModelEvent('published', false); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Mark entity as unpublished. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function unpublish(): void |
85
|
|
|
{ |
86
|
|
|
$this->unsetPublishedFlag()->save(); |
87
|
|
|
|
88
|
|
|
$this->fireModelEvent('unpublished', false); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|