1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model\Behavior; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Allow to enable or disable trait |
16
|
|
|
* on the current model instance ($enabled) |
17
|
|
|
* or globally for every model instance ($staticEnabled) |
18
|
|
|
*/ |
19
|
|
|
trait SkippableTrait |
20
|
|
|
{ |
21
|
|
|
public bool $enabled = true; |
22
|
|
|
public static bool $staticEnabled = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Return true if the behavior is enabled |
26
|
|
|
* on the current model instance |
27
|
|
|
*/ |
28
|
|
|
public function getEnabled(): bool |
29
|
|
|
{ |
30
|
|
|
return $this->enabled; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set true to enable the behavior |
35
|
|
|
* on the current model instance |
36
|
|
|
*/ |
37
|
|
|
public function setEnabled(bool $enabled): void |
38
|
|
|
{ |
39
|
|
|
$this->enabled = $enabled; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return true if the behavior is enabled |
44
|
|
|
* globally for every model instance |
45
|
|
|
*/ |
46
|
|
|
public static function getStaticEnabled(): bool |
47
|
|
|
{ |
48
|
|
|
return self::$staticEnabled; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set true to enable the behavior |
53
|
|
|
* globally for every model instance |
54
|
|
|
*/ |
55
|
|
|
public static function setStaticEnabled(bool $staticEnabled): void |
56
|
|
|
{ |
57
|
|
|
self::$staticEnabled = $staticEnabled; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Enable the behavior |
62
|
|
|
* on the current model instance |
63
|
|
|
*/ |
64
|
|
|
public function enable(): void |
65
|
|
|
{ |
66
|
|
|
$this->setEnabled(true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Disable the behavior |
71
|
|
|
* on the current model instance |
72
|
|
|
*/ |
73
|
|
|
public function disable(): void |
74
|
|
|
{ |
75
|
|
|
$this->setEnabled(false); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Enable the behavior |
80
|
|
|
* globally for every model instance |
81
|
|
|
*/ |
82
|
|
|
public static function staticEnable(): void |
83
|
|
|
{ |
84
|
|
|
self::setStaticEnabled(true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Disable the behavior |
89
|
|
|
* globally for every model instance |
90
|
|
|
*/ |
91
|
|
|
public static function staticDisable(): void |
92
|
|
|
{ |
93
|
|
|
self::setStaticEnabled(false); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return true if the behavior is enabled |
98
|
|
|
* on the current model instance and globally |
99
|
|
|
*/ |
100
|
|
|
public function isEnabled(): bool |
101
|
|
|
{ |
102
|
|
|
return $this->getEnabled() && self::getStaticEnabled(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return true if the behavior is enabled |
107
|
|
|
* on the current model instance and globally |
108
|
|
|
*/ |
109
|
|
|
public function isDisabled(): bool |
110
|
|
|
{ |
111
|
|
|
return !$this->isEnabled(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|