openvk/Web/Util/Bitmask.php

98 lines
2.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-07 19:04:43 +03:00
namespace openvk\Web\Util;
class Bitmask
{
protected $data;
protected $length;
protected $mapping;
public function __construct(int $data, int $length = 1, array $mapping = [])
2020-06-07 19:04:43 +03:00
{
$this->data = str_pad(decbin($data), 63, "0", STR_PAD_RIGHT);
$this->length = $length;
if ((sizeof($mapping) - 1) > (64 / $length)) {
2020-06-07 19:04:43 +03:00
throw new \OutOfRangeException("Mapping contains more keys than a bitmask can fit in itself.");
} else {
2020-06-07 19:04:43 +03:00
$this->mapping = $mapping;
}
2020-06-07 19:04:43 +03:00
}
2020-06-07 19:04:43 +03:00
private function getOffsetByKey(string $key): int
{
$offset = array_search($key, $this->mapping);
if ($offset === false) {
2020-06-07 19:04:43 +03:00
throw new \OutOfBoundsException("Key '$key' is not present in bitmask.");
}
2020-06-07 19:04:43 +03:00
return $offset;
}
public function toInteger(): int
2020-06-07 19:04:43 +03:00
{
return (int) bindec($this->data);
}
public function __toString(): string
2020-06-07 19:04:43 +03:00
{
return (string) $this->toInteger();
}
public function getNumberByOffset(int $offset): float
2020-06-07 19:04:43 +03:00
{
$offset *= $this->length;
if ($offset > (64 / $this->length)) {
2020-06-07 19:04:43 +03:00
return (float) 'NaN';
}
2020-06-07 19:04:43 +03:00
return (float) bindec(substr($this->data, $offset, $this->length));
}
public function getBoolByOffset(int $offset): ?bool
2020-06-07 19:04:43 +03:00
{
if ($this->length !== 1) {
return null;
}
2020-06-07 19:04:43 +03:00
$number = $this->getNumberByOffset($offset);
return is_nan($number) ? null : (bool) $number;
2020-06-07 19:04:43 +03:00
}
public function setByOffset(int $offset, int $number): void
2020-06-07 19:04:43 +03:00
{
$offset *= $this->length;
if (($offset + $this->length) > 64) {
2020-06-07 19:04:43 +03:00
throw new \OutOfRangeException("$offset is invalid offset. Bitmask length is 64 bits.");
}
2020-06-07 19:04:43 +03:00
$this->data = substr_replace($this->data, str_pad(decbin($number), $this->length, "0", STR_PAD_LEFT), $offset, $this->length);
}
public function set($key, int $data): Bitmask
2020-06-07 19:04:43 +03:00
{
if (gettype($key) === "string") {
2020-06-07 19:04:43 +03:00
$this->setByOffset($this->getOffsetByKey($key), $data);
} elseif (gettype($key) === "int") {
2020-06-07 19:04:43 +03:00
$this->setByOffset($key, $data);
} else {
2020-06-07 19:04:43 +03:00
throw new TypeError("Key must be either offset (int) or a string index");
}
2020-06-07 19:04:43 +03:00
return $this;
}
public function get($key)
2020-06-07 19:04:43 +03:00
{
if (gettype($key) === "string") {
2020-06-07 19:04:43 +03:00
$key = $this->getOffsetByKey($key);
} elseif (gettype($key) !== "int") {
2020-06-07 19:04:43 +03:00
throw new TypeError("Key must be either offset (int) or a string index");
}
2020-06-07 19:04:43 +03:00
return $this->length === 1 ? $this->getBoolByOffset($key) : $this->getNumberByOffset($key);
}
}