File tree Expand file tree Collapse file tree 2 files changed +11
-17
lines changed Expand file tree Collapse file tree 2 files changed +11
-17
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ Updated after reading this blog post: https://aaronfrancis.com/2021/bitmasking-i
7
7
I just used it in Laravel so far, but you should be able to use it anywhere else with minor modifications.
8
8
9
9
## PHP Version
10
- Version v2.* requires PHP 8 +. If you're stuck to an older version, please use v1.*
10
+ Version v2.* requires PHP 7.4 +. If you're stuck to an older version, please use v1.*
11
11
12
12
## Installation
13
13
@@ -89,20 +89,14 @@ Maybe you want to define that in a constant or variable.
89
89
90
90
To make your life easier, I recommend to use custom getters and setters.
91
91
``` php
92
-
93
- /**
94
- * @param bool $sent
95
- * @return bool
96
- */
97
- public function setSentAttribute($sent = true)
92
+ public function setSentAttribute($sent = true): self
98
93
{
99
- return $this->setFlag('status', self::MESSAGE_SENT, $sent);
94
+ $this->setFlag('status', self::MESSAGE_SENT, $sent);
95
+
96
+ return $this;
100
97
}
101
98
102
- /**
103
- * @return bool
104
- */
105
- public function getSentAttribute()
99
+ public function getSentAttribute(): bool
106
100
{
107
101
return $this->getFlag('status', self::MESSAGE_SENT);
108
102
}
Original file line number Diff line number Diff line change @@ -15,21 +15,21 @@ protected function getFlag(string $name, int $flag): bool
15
15
return ($ this ->$ name & $ flag ) === $ flag ;
16
16
}
17
17
18
- protected function setFlag (string $ name , int $ flag , bool $ value ): self
18
+ protected function setFlag (string $ name , int $ flag , bool $ value ): bool
19
19
{
20
20
if ($ value ) {
21
21
$ this ->$ name |= $ flag ;
22
22
} else {
23
23
$ this ->$ name &= ~$ flag ;
24
24
}
25
-
26
- return $ this ;
25
+
26
+ return ( $ this -> $ name & $ flag ) === $ flag ;
27
27
}
28
28
29
- protected function toggleFlag (string $ name , int $ flag ): self
29
+ protected function toggleFlag (string $ name , int $ flag ): bool
30
30
{
31
31
$ this ->$ name ^= $ flag ;
32
32
33
- return $ this ;
33
+ return $ this -> getFlag ( $ name , $ flag ) ;
34
34
}
35
35
}
You can’t perform that action at this time.
0 commit comments