$permission_name; return static::readBitPair($perm_int, $bit_n); } /** * Returns the value of the operation for the given permission. * * @param string $permission_name The name of the permission, for which the operation should be returned. * @param int $bit_n The (lower) bit number of the bit pair for the operation. * * @return bool|null The value of the operation. True, if the given operation is allowed, false if disallowed * and null if it should inherit from parent. */ public function getPermissionValue(string $permission_name, int $bit_n): ?bool { $value = $this->getBitValue($permission_name, $bit_n); if (self::ALLOW == $value) { return true; } if (self::DISALLOW == $value) { return false; } return null; } /** * Reads a bit pair from $data. * * @param $data int The data from where the bits should be extracted from. * @param $n int The number of the lower bit (of the pair) that should be read. Starting from zero. * * @return int The value of the bit pair. */ final protected static function readBitPair(int $data, int $n): int { Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.'); if (0 !== $n % 2) { throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!'); } $mask = 0b11 << $n; //Create a mask for the data return ($data & $mask) >> $n; //Apply mask and shift back } /** * Writes a bit pair in the given $data and returns it. * * @param $data int The data which should be modified. * @param $n int The number of the lower bit of the pair which should be written. * @param $new int The new value of the pair. * * @return int The new data with the modified pair. */ final protected static function writeBitPair(int $data, int $n, int $new): int { Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.'); Assert::lessThanEq($new, 3, '$new must be smaller than 3, because a bit pair is written! Got %s.'); if (0 !== $n % 2) { throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!'); } $mask = 0b11 << $n; //Mask all bits that should be writen $newval = $new << $n; //The new value. $data = ($data & ~$mask) | ($newval & $mask); return $data; } }