{"id":21557066,"url":"https://github.com/yaroslavche/bitmask","last_synced_at":"2025-03-06T17:09:39.800Z","repository":{"id":28911718,"uuid":"119703038","full_name":"yaroslavche/BitMask","owner":"yaroslavche","description":"PHP library for working with bitmask","archived":false,"fork":false,"pushed_at":"2024-09-01T07:53:03.000Z","size":196,"stargazers_count":32,"open_issues_count":2,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-27T21:16:32.341Z","etag":null,"topics":["bitmask","bitwise","php","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yaroslavche.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-31T15:10:15.000Z","updated_at":"2024-11-23T00:26:45.000Z","dependencies_parsed_at":"2024-11-24T10:03:05.567Z","dependency_job_id":null,"html_url":"https://github.com/yaroslavche/BitMask","commit_stats":{"total_commits":94,"total_committers":1,"mean_commits":94.0,"dds":0.0,"last_synced_commit":"24c9867e91be62bc0d3c8738e169969efc7df660"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2FBitMask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2FBitMask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2FBitMask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2FBitMask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yaroslavche","download_url":"https://codeload.github.com/yaroslavche/BitMask/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242250929,"owners_count":20096896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitmask","bitwise","php","php8"],"created_at":"2024-11-24T08:10:50.937Z","updated_at":"2025-03-06T17:09:39.781Z","avatar_url":"https://github.com/yaroslavche.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PHP build](https://github.com/yaroslavche/BitMask/actions/workflows/php.yml/badge.svg)](https://github.com/yaroslavche/BitMask/actions/workflows/php.yml)\n[![codecov](https://codecov.io/gh/yaroslavche/bitmask/branch/main/graph/badge.svg)](https://codecov.io/gh/yaroslavche/bitmask)\n[![Infection MSI](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyaroslavche%2FBitMask%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/yaroslavche/BitMask/main)\n[![PHP](http://poser.pugx.org/yaroslavche/bitmask/require/php)](https://packagist.org/packages/yaroslavche/bitmask)\n# BitMask\n\nPHP library for working with bitmask values\n\n## Getting Started\nUsually enough for checking bits: \n```php\ndefine('READ', 1 \u003c\u003c 0);\ndefine('WRITE', 1 \u003c\u003c 1);\ndefine('EXECUTE', 1 \u003c\u003c 2);\n\n$mask = READ | WRITE | EXECUTE;\necho sprintf('mask: %d', $mask); // mask: 7\nif ($mask \u0026 READ) {} // if $mask has a single bit READ\n$mask \u0026= ~EXECUTE; // remove a single bit from the $mask\n$mask |= EXECUTE; // set a single bit to the $mask\n```\n\nBut you can try other way with this package:\n\n```php\nuse BitMask\\BitMask;\n\n// two arguments: integer mask (default: 0) and most significant bit for boundaries (default: null) \n$bitmask = new BitMask(READ | WRITE | EXECUTE);\necho sprintf('mask: %d', $bitmask-\u003eget()); // mask: 7\nif ($bitmask-\u003ehas(READ)) {}\n$bitmask-\u003eremove(EXECUTE);\n$bitmask-\u003eset(EXECUTE);\n```\n\nExists [EnumBitMask](/src/EnumBitMask.php), which allows the same using PHP enum:\n\n```php\nuse BitMask\\EnumBitMask;\n\nenum Permissions\n{\n    case READ;\n    case WRITE;\n    case EXECUTE;\n}\n\n// two arguments: required enum class-string and integer mask (default: 0)\n$bitmask = new EnumBitMask(Permissions::class, 0b111);\necho sprintf('mask: %d', $bitmask-\u003eget()); // mask: 7\nif ($bitmask-\u003ehas(Permissions::READ)) {}\n$bitmask-\u003eremove(Permissions::EXECUTE);\n$bitmask-\u003eset(Permissions::EXECUTE);\n\n$bitmask-\u003eset(Unknown::Case); // throws an exception, only Permissions cases available\n```\n\n`EnumBitMask` have a factory methods:\n```php\n# Create a bit mask using one or multiple enum cases\n$bitmask = EnumBitMask::create(Permissions::class, Permissions::EXECUTE);\n\n# Create a bit mask using all enum cases\n$bitmask = EnumBitMask::all(Permissions::class);\n\n# Create a bit mask with no flags on (equivalent to create with no additional flags)\n$bitmask = EnumBitMask::none(Permissions::class);\n\n# Create a bit mask without specific flags\n$bitmask = EnumBitMask::without(Permissions::class, Permissions::EXECUTE);\n```\n\nExists [Bits](/src/Util/Bits.php) helper with static methods:\n\n```php\nuse BitMask\\Util\\Bits;\n\n$mask = 7; // 1 \u003c\u003c 0 | 1 \u003c\u003c 1 | 1 \u003c\u003c 2\n$integerMostSignificantBit = Bits::getMostSignificantBit($mask); // int 2\n$arraySetBitsIndexes = Bits::getSetBitsIndexes($mask); // array:3 [0, 1, 2]\n$arraySetBits = Bits::getSetBits($mask); // array:3 [1, 2, 4]\n$string = Bits::toString($mask); // string \"111\"\n$integerBit = Bits::indexToBit(16); // int 65536\n$integerIndex = Bits::bitToIndex(65536); // int 16\n$boolIsSingleBit = Bits::isSingleBit(8); // true\n```\n\n## Installing\n\nInstall package via [composer](https://getcomposer.org/) \n```bash\ncomposer require yaroslavche/bitmask\n```\n\n## Contributing\n\nFeel free to fork or contribute =)\n\n#### CI build\n```shell\n$ composer ci:pack\n```\n\n#### Tests\n##### PHPUnit\n```shell\n$ composer phpunit\n$ ./vendor/bin/phpunit\n```\n##### Infection\n```shell\n$ composer infection\n$ ./vendor/bin/infection --min-msi=100 --min-covered-msi=100\n```\n#### Benchmarks\n```shell\n$ composer phpbench\n$ ./vendor/bin/phpbench run benchmarks --report=default\n```\n#### Static analyzer and code style\n##### PHPStan\n```shell\n$ composer phpstan\n$ ./vendor/bin/phpstan analyse src/ -c phpstan.neon --level=9 --no-progress -vvv --memory-limit=1024M\n```\n\n##### Psalm\n```shell\n$ composer psalm\n$ ./vendor/bin/psalm\n```\n##### PHP-CS\n###### Code style check\n```shell\n$ composer phpcs-check\n$ ./vendor/bin/php-cs-fixer check --diff\n\n```\n###### Code style fix\n```shell\n$ composer phpcs-fix\n$ ./vendor/bin/php-cs-fixer fix\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaroslavche%2Fbitmask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaroslavche%2Fbitmask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaroslavche%2Fbitmask/lists"}