2020-09-04 22:04:55 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace App\Util;
|
|
|
|
|
|
|
|
use App\Util\Exception\ServerException;
|
|
|
|
|
|
|
|
abstract class Bitmap
|
|
|
|
{
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
2021-04-05 14:44:25 +01:00
|
|
|
* Convert from an integer to an onject or an array of constants for
|
|
|
|
* set each bit. If $instance, return an object with the corresponding
|
2020-11-06 19:47:15 +00:00
|
|
|
* properties set
|
|
|
|
*/
|
2021-04-05 14:44:25 +01:00
|
|
|
private static function _to(int $r, bool $instance)
|
2020-09-04 22:04:55 +01:00
|
|
|
{
|
|
|
|
$init = $r;
|
|
|
|
$class = get_called_class();
|
2020-09-10 21:37:10 +01:00
|
|
|
if ($instance) {
|
|
|
|
$obj = new $class;
|
|
|
|
} else {
|
|
|
|
$vals = [];
|
|
|
|
}
|
|
|
|
|
2021-04-05 14:44:25 +01:00
|
|
|
$consts = (new \ReflectionClass($class))->getConstants();
|
|
|
|
$have_prefix = false;
|
|
|
|
if (isset($consts['PREFIX'])) {
|
|
|
|
$have_prefix = true;
|
|
|
|
unset($consts['PREFIX']);
|
|
|
|
}
|
2020-09-04 22:04:55 +01:00
|
|
|
|
2020-09-10 23:28:50 +01:00
|
|
|
foreach ($consts as $c => $v) {
|
2020-09-10 21:37:10 +01:00
|
|
|
$b = ($r & $v) !== 0;
|
|
|
|
if ($instance) {
|
2020-09-10 23:28:50 +01:00
|
|
|
$c = strtolower($c);
|
2020-09-10 21:37:10 +01:00
|
|
|
$obj->{$c} = $b;
|
|
|
|
}
|
|
|
|
if ($b) {
|
2020-09-04 22:04:55 +01:00
|
|
|
$r -= $v;
|
2020-09-10 21:37:10 +01:00
|
|
|
if (!$instance) {
|
2021-04-05 14:44:25 +01:00
|
|
|
$vals[] = ($have_prefix ? $class::PREFIX : '') . $c;
|
2020-09-10 21:37:10 +01:00
|
|
|
}
|
2020-09-04 22:04:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($r != 0) {
|
|
|
|
Log::error('Bitmap to array conversion failed');
|
|
|
|
throw new ServerException("Bug in bitmap conversion for class {$class} from value {$init}");
|
|
|
|
}
|
|
|
|
|
2020-09-10 21:37:10 +01:00
|
|
|
if ($instance) {
|
|
|
|
return $obj;
|
|
|
|
} else {
|
|
|
|
return $vals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function create(int $r): self
|
|
|
|
{
|
2021-04-05 14:44:25 +01:00
|
|
|
return self::_to($r, true);
|
2020-09-10 21:37:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function toArray(int $r): array
|
|
|
|
{
|
2021-04-05 14:44:25 +01:00
|
|
|
return self::_to($r, false);
|
2020-09-04 22:04:55 +01:00
|
|
|
}
|
|
|
|
}
|