Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
27 / 27 |
Bitmap | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
12 | |
100.00% |
27 / 27 |
_to | |
100.00% |
1 / 1 |
10 | |
100.00% |
25 / 25 |
|||
create | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
toArray | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | // {{{ License |
4 | // This file is part of GNU social - https://www.gnu.org/software/social |
5 | // |
6 | // GNU social is free software: you can redistribute it and/or modify |
7 | // it under the terms of the GNU Affero General Public License as published by |
8 | // the Free Software Foundation, either version 3 of the License, or |
9 | // (at your option) any later version. |
10 | // |
11 | // GNU social is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU Affero General Public License for more details. |
15 | // |
16 | // You should have received a copy of the GNU Affero General Public License |
17 | // along with GNU social. If not, see <http://www.gnu.org/licenses/>. |
18 | // }}} |
19 | |
20 | namespace App\Util; |
21 | |
22 | use App\Core\Log; |
23 | use App\Util\Exception\ServerException; |
24 | |
25 | abstract class Bitmap |
26 | { |
27 | /** |
28 | * Convert from an integer to an onject or an array of constants for |
29 | * set each bit. If $instance, return an object with the corresponding |
30 | * properties set |
31 | */ |
32 | private static function _to(int $r, bool $instance) |
33 | { |
34 | $init = $r; |
35 | $class = get_called_class(); |
36 | if ($instance) { |
37 | $obj = new $class; |
38 | } else { |
39 | $vals = []; |
40 | } |
41 | |
42 | $consts = (new \ReflectionClass($class))->getConstants(); |
43 | $have_prefix = false; |
44 | if (isset($consts['PREFIX'])) { |
45 | $have_prefix = true; |
46 | unset($consts['PREFIX']); |
47 | } |
48 | |
49 | foreach ($consts as $c => $v) { |
50 | $b = ($r & $v) !== 0; |
51 | if ($instance) { |
52 | $c = strtolower($c); |
53 | $obj->{$c} = $b; |
54 | } |
55 | if ($b) { |
56 | $r -= $v; |
57 | if (!$instance) { |
58 | $vals[] = ($have_prefix ? $class::PREFIX : '') . $c; |
59 | } |
60 | } |
61 | } |
62 | |
63 | if ($r != 0) { |
64 | Log::error('Bitmap to array conversion failed'); |
65 | throw new ServerException("Bug in bitmap conversion for class {$class} from value {$init}"); |
66 | } |
67 | |
68 | if ($instance) { |
69 | return $obj; |
70 | } else { |
71 | return $vals; |
72 | } |
73 | } |
74 | |
75 | public static function create(int $r): self |
76 | { |
77 | return self::_to($r, true); |
78 | } |
79 | |
80 | public static function toArray(int $r): array |
81 | { |
82 | return self::_to($r, false); |
83 | } |
84 | } |