Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
build:
environment:
php:
version: 7.1
version: 7.4
tests:
override:
-
Expand Down
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ cache:
- $HOME/.composer/cache
matrix:
include:
- php: 7.0
- php: 7.4
env: deps=low
- php: 7.0
- php: 7.1
- php: 8.0
- php: nightly
fast_finish: true
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Lesnykh Ilia
Copyright (c) 2016-2021 Lesnykh Ilia

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ About

Bitmask is a simple PHP implementation of bitwise operations for creating masks.
Can be used for some flags implementation.
Currently supported PHP version: >= 7.0
Currently supported PHP version: >= 7.4

Installation
---
Expand All @@ -35,12 +35,14 @@ Read: no
Update: no
Delete: no
–––––––––––––––––––––––––––––––––––

Check user for all access levels:
Create: no
Read: yes
Update: no
Delete: no
–––––––––––––––––––––––––––––––––––

```

Tests
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
}
},
"require": {
"php-64bit": ">=7.0"
"php-64bit": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "~6.2"
"phpunit/phpunit": "~9"
}
}
14 changes: 8 additions & 6 deletions example/example.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

require_once realpath(__DIR__ . '/../vendor/autoload.php');

/*
Expand All @@ -8,10 +10,10 @@
* Users should have the ACL for creating, reading, updating and deleting news.
*/

define('ACCESS_CREATE', 1);
define('ACCESS_READ', 2);
define('ACCESS_UPDATE', 3);
define('ACCESS_DELETE', 4);
define('ACCESS_CREATE', 0);
define('ACCESS_READ', 1);
define('ACCESS_UPDATE', 2);
define('ACCESS_DELETE', 3);
// etc, up to 63

// some users from storage
Expand All @@ -20,7 +22,7 @@
];

// create a Bitmask object, passing user bitmask from storage
$Bitmask = \Aliance\Bitmask\Bitmask::create($user['access_level']);
$Bitmask = new \Aliance\Bitmask\Bitmask($user['access_level']);

checkRights($Bitmask);

Expand All @@ -36,5 +38,5 @@ function checkRights(\Aliance\Bitmask\Bitmask $Bitmask)
echo 'Read: ', $Bitmask->issetBit(ACCESS_READ) ? 'yes' : 'no', PHP_EOL;
echo 'Update: ', $Bitmask->issetBit(ACCESS_UPDATE) ? 'yes' : 'no', PHP_EOL;
echo 'Delete: ', $Bitmask->issetBit(ACCESS_DELETE) ? 'yes' : 'no', PHP_EOL;
echo str_repeat('–', 35), PHP_EOL;
echo str_repeat('–', 35), PHP_EOL, PHP_EOL;
}
31 changes: 12 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true"
>
<testsuites>
<testsuite name="general">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="general">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
82 changes: 16 additions & 66 deletions src/Bitmask.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Aliance\Bitmask;
Expand All @@ -9,109 +10,47 @@
*/
class Bitmask
{
/**
* @var int
*/
const MAX_BIT = 63;
private const MAX_BIT = 63;

/**
* @var int
*/
private $mask;
private int $mask;

/**
* @param int $mask
*/
public function __construct(int $mask = 0)
{
$this->setMask($mask);
}

/**
* @param int $mask
* @return self
*/
public static function create(int $mask = 0): self
{
return new self($mask);
}

/**
* @param int $bit
* @return self
*/
public function setBit(int $bit): self
{
return $this->addMask(1 << $this->checkBit($bit));
}

/**
* @param int $mask
* @return self
*/
public function addMask(int $mask): self
{
$this->mask |= $mask;
return $this;
}

/**
* @param int $bit
* @return int
* @throws \InvalidArgumentException
*/
private function checkBit(int $bit): int
{
if ($bit > self::MAX_BIT) {
throw new \InvalidArgumentException(sprintf(
'Bit number %d is greater than possible limit %d.',
$bit,
self::MAX_BIT
));
}
return $bit;
}

/**
* @param int $bit
* @return self
*/
public function unsetBit(int $bit): self
{
return $this->deleteMask(1 << $this->checkBit($bit));
}

/**
* @param int $mask
* @return self
*/
public function deleteMask(int $mask): self
{
$this->mask &= ~$mask;
return $this;
}

/**
* @param int $bit
* @return bool
*/
public function issetBit(int $bit): bool
{
return (bool)($this->getMask() & (1 << $this->checkBit($bit)));
return (bool) ($this->getMask() & (1 << $this->checkBit($bit)));
}

/**
* @return int
*/
public function getMask(): int
{
return $this->mask;
}

/**
* @param int $mask
* @return self
*/
public function setMask(int $mask): self
{
$this->mask = $mask;
Expand All @@ -121,10 +60,21 @@ public function setMask(int $mask): self
/**
* Return set bits count.
* Actually, counts the number of 1 in binary representation of the decimal mask integer.
* @return int
*/
public function getSetBitsCount(): int
{
return substr_count(decbin($this->mask), '1');
}

private function checkBit(int $bit): int
{
if ($bit > self::MAX_BIT || $bit < 0) {
throw new \InvalidArgumentException(sprintf(
'Bit number %d is out of range [0..%d].',
$bit,
self::MAX_BIT
));
}
return $bit;
}
}
Loading