$permission_name); } /** * Returns the bit pair value of the given permission. * * @param string $permission_name The name of the permission, for which the bit pair should be returned. * @param int $bit_n The (lower) bit number of the bit pair, which should be read. * * @return int The value of the bit pair. Compare to the INHERIT, ALLOW, and DISALLOW consts in this class. */ public function getBitValue(string $permission_name, int $bit_n): int { if (!$this->isValidPermissionName($permission_name)) { throw new \InvalidArgumentException(sprintf('No permission with the name "%s" is existing!', $permission_name)); } $perm_int = $this->$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; } /** * Sets the value of the given permission and operation. * @param string $permission_name The name of the permission, for which the bit pair should be written. * @param int $bit_n The (lower) bit number of the bit pair, which should be written. * @param bool|null $new_value The new value for the operation: * True, if the given operation is allowed, false if disallowed * and null if it should inherit from parent. * @return PermissionsEmbed The instance itself. */ public function setPermissionValue(string $permission_name, int $bit_n, ?bool $new_value) : self { //Determine which bit value the given value is. if ($new_value === true) { $bit_value = static::ALLOW; } elseif ($new_value === false) { $bit_value = static::DISALLOW; } else { $bit_value = static::INHERIT; } $this->setBitValue($permission_name, $bit_n, $bit_value); return $this; } /** * Sets the bit value of the given permission and operation. * @param string $permission_name The name of the permission, for which the bit pair should be written. * @param int $bit_n The (lower) bit number of the bit pair, which should be written. * @param int $new_value The new (bit) value of the bit pair, which should be written. * @return PermissionsEmbed The instance itself. */ public function setBitValue(string $permission_name, int $bit_n, int $new_value) : self { if (!$this->isValidPermissionName($permission_name)) { throw new \InvalidArgumentException('No permission with the given name is existing!'); } $this->$permission_name = static::writeBitPair($this->$permission_name, $bit_n, $new_value); return $this; } /** * Returns the given permission as raw int (all bit at once) * @param string $permission_name The name of the permission, which should be retrieved. * If this is not existing an exception is thrown. * @return int The raw permission value. */ public function getRawPermissionValue(string $permission_name) : int { if (!$this->isValidPermissionName($permission_name)) { throw new \InvalidArgumentException('No permission with the given name is existing!'); } return $this->$permission_name; } /** * Sets the given permission to the value. * @param string $permission_name The name of the permission to that should be set. * @param int $value The new value of the permsission * @return $this */ public function setRawPermissionValue(string $permission_name, int $value) : self { if (!$this->isValidPermissionName($permission_name)) { throw new \InvalidArgumentException( sprintf('No permission with the given name %s is existing!', $permission_name) ); } $this->$permission_name = $value; return $this; } /** * Sets multiple permissions at once. * @param array $values An array in the form ['perm_name' => $value], containing the new data * @param array|null $values2 If this array is not null, the first array will treated of list of perm names, * and this array as an array of new values. * @return $this */ public function setRawPermissionValues(array $values, array $values2 = null) : self { if (!empty($values2)) { $values = array_combine($values, $values2); } foreach ($values as $key => $value) { $this->setRawPermissionValue($key, $value); } return $this; } /** * 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.'); Assert::greaterThanEq($new, 0, '$new must not be negative, 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; } }