@@ -96,13 +96,7 @@ public function __construct($mask = 0)
96
96
*/
97
97
public function add ($ mask )
98
98
{
99
- if (is_string ($ mask ) && defined ($ name = 'static::MASK_ ' .strtoupper ($ mask ))) {
100
- $ mask = constant ($ name );
101
<
8000
td data-grid-cell-id="diff-3f63f1c061a01a37e840d894d08e4da68bfb60dd911a238f03ea9538f4b5008f-101-98-2" data-line-anchor="diff-3f63f1c061a01a37e840d894d08e4da68bfb60dd911a238f03ea9538f4b5008fL101" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionLine-bgColor, var(--diffBlob-deletion-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell left-side-diff-cell border-right left-side">- } elseif (!is_int ($ mask )) {
102
- throw new \InvalidArgumentException ('$mask must be an integer. ' );
103
- }
104
-
105
- $ this ->mask |= $ mask ;
99
+ $ this ->mask |= $ this ->getMask ($ mask );
106
100
107
101
return $ this ;
108
102
}
@@ -152,13 +146,7 @@ public function getPattern()
152
146
*/
153
147
public function remove ($ mask )
154
148
{
155
- if (is_string ($ mask ) && defined ($ name = 'static::MASK_ ' .strtoupper ($ mask ))) {
156
- $ mask = constant ($ name );
157
- } elseif (!is_int ($ mask )) {
158
- throw new \InvalidArgumentException ('$mask must be an integer. ' );
159
- }
160
-
161
- $ this ->mask &= ~$ mask ;
149
+ $ this ->mask &= ~$ this ->getMask ($ mask );
162
150
163
151
return $ this ;
164
152
}
@@ -191,19 +179,43 @@ public static function getCode($mask)
191
179
192
180
$ reflection = new \ReflectionClass (get_called_class ());
193
181
foreach ($ reflection ->getConstants () as $ name => $ cMask ) {
194
- if (0 !== strpos ($ name , 'MASK_ ' )) {
182
+ if (0 !== strpos ($ name , 'MASK_ ' ) || $ mask !== $ cMask ) {
195
183
continue ;
196
184
}
197
185
198
- if ($ mask === $ cMask ) {
199
- if (!defined ($ cName = 'static::CODE_ ' .substr ($ name , 5 ))) {
200
- throw new \RuntimeException ('There was no code defined for this mask. ' );
201
- }
202
-
203
- return constant ($ cName );
186
+ if (!defined ($ cName = 'static::CODE_ ' .substr ($ name , 5 ))) {
187
+ throw new \RuntimeException ('There was no code defined for this mask. ' );
204
188
}
189
+
190
+ return constant ($ cName );
205
191
}
206
192
207
193
throw new \InvalidArgumentException (sprintf ('The mask "%d" is not supported. ' , $ mask ));
208
194
}
195
+
196
+ /**
197
+ * Returns the mask for the passed code
198
+ *
199
+ * @param mixed $code
200
+ *
201
+ * @return integer
202
+ *
203
+ * @throws \InvalidArgumentException
204
+ */
205
+ private function getMask ($ code )
206
+ {
207
+ if (is_string ($ code )) {
208
+ if (!defined ($ name = sprintf ('static::MASK_%s ' , strtoupper ($ code )))) {
209
+ throw new \InvalidArgumentException (sprintf ('The code "%s" is not supported ' , $ code ));
210
+ }
211
+
212
+ return constant ($ name );
213
+ }
214
+
215
+ if (!is_int ($ code )) {
216
+ throw new \InvalidArgumentException ('$code must be an integer. ' );
217
+ }
218
+
219
+ return $ code ;
220
+ }
209
221
}
0 commit comments