PHPackages                             jmcneese/bitmasked - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jmcneese/bitmasked

AbandonedArchivedCakephp-plugin[Database &amp; ORM](/categories/database)

jmcneese/bitmasked
==================

Bitmasked is a plugin for CakePHP for row-level filtering of models via bitmasks.

811.2k1[1 PRs](https://github.com/jmcneese/bitmasked/pulls)PHP

Since Apr 23Pushed 9y ago4 watchersCompare

[ Source](https://github.com/jmcneese/bitmasked)[ Packagist](https://packagist.org/packages/jmcneese/bitmasked)[ RSS](/packages/jmcneese-bitmasked/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Bitmasked Plugin
================

[](#bitmasked-plugin)

Row-level filtering of models via bitmasks.

Requirements
------------

[](#requirements)

- PHP5.2+ (Fully featured with 5.3+)
- CakePHP 2.0+

Installation
------------

[](#installation)

### Manual

[](#manual)

1. Download this:
2. Unzip that download.
3. Copy the resulting folder to app/plugins
4. Rename the folder you just copied to `bitmasked`

### GIT Submodule

[](#git-submodule)

From your repository root directory type:

```
git submodule add git://github.com/jmcneese/bitmasked.git app/Plugin/Bitmasked
git submodule update --init

```

### GIT Clone

[](#git-clone)

In your plugin directory type

```
git clone git://github.com/jmcneese/bitmasked.git Bitmasked
```

Usage
-----

[](#usage)

1. Import the SQL in config/schema/bitmasked\_bits.sql into your application’s database
2. Activate the behavior in whichever model(s) you desire, via:```

    public $actsAs = array(
    	'Bitmasked.Bitmasked'
    );

    ```

Options
-------

[](#options)

There are several configurable options that can be passed into the behavior:

- *disabled* — If you want the behavior to start as disabled, i.e. not automatically filter rows based on bitmask, set this to true. Default: false
- *default* — This is the default bit that is saved for new records for this model, unless otherwise specified in save data. Default: 1
- *bits* — An array of flags (string) that map to bits (integer) to use for this particular model. Default: array(‘ALL’ =&gt; 1)
- *mask* — The bitmask to use when finding records on this model. There are several possible settings here:
    - Implicit (integer). This is when the bitmask is static across all finds.
    - Flag (string). This is a string representation of a flag you have defined in the ‘bits’ option.
    - Flags (array). An array of flags in string form.
    - Callback (mixed). Either a string referring to a function name in global scope, a method in the attached model or an anonymous function (PHP5.3 only).

Examples
--------

[](#examples)

Lets say we run a grocery store and want to easily query all the items that a user is allowed to even view. For this example we’ll use items that have age restrictions.

First off, lets section off our groups into common restrictions. This is easy to do by adjusting our `bitmask` setting on the behavior for our model.

```

public $actsAs = array(
	'Bitmasked.Bitmasked' => array(
		'bits' => array(
			'ALL' => 1,
			'18+' => 2,
			'21+' => 4,
			'55+' => 8
		)
	)
);

```

We’ve given each group an easy to remember name that we can reference later on in our queries. With that out of the way we can go ahead and create our items.

```

 // Item for everyone.
$this->Item->create();
$this->Item->save(array(
	'name' => 'Ice Cream',
	'bits' => array(
		'ALL',
		'18+',
		'21+',
		'55+'
	)
));

// Item for 18+ year olds
$this->Item->create();
$this->Item->save(array(
	'name' => 'Cigarettes',
	'bits' => array(
		'18+',
		'21+',
		'55+'
	)
));

// Item for 21+ year olds
$this->Item->create();
$this->Item->save(array(
	'name' => 'Alcohol',
	'bits' => array(
		'21+',
		'55+'
	)
));

// Item for 55+ year olds
$this->Item->create();
$this->Item->save(array(
	'name' => 'Lawn',
	'bits' => array(
		'55+'
	)
));

```

Notice how for the items we saved the lowest matching group and all the above ones. Just because you’re 28 doesn’t mean you don’t want ice cream, right?

With those rows created we can easily get at only the rows that are applicable to be shown. Here’s an example of finding everything that can be seen by an 18 year old.

```

$items = $this->Item->find(
	'all',
	array(
		'conditions' => array(),
		'bitmask' => array(
			'18+'
		)
	)
);

```

In our `$items` array we should have the “Ice Cream” and “Cigarettes” records available. Notice how we made use of the name of the group (`18+`) instead of the actual bitmask value.

Instead of saving the items with all of their available groups, you can save them with their lowest group and instead query for all groups that the person is allowed; the same example would look like this

```

 // Item for everyone. Note we only save "ALL"
$this->Item->create();
$this->Item->save(array(
	'name' => 'Ice Cream',
	'bits' => array(
		'ALL'
	)
));

// Query for an 18yr old. Notice how we include "ALL"
$items = $this->Item->find(
	'all',
	array(
		'conditions' => array(),
		'bitmask' => array(
			'ALL',
			'18+'
		)
	)
);

```

Todo
----

[](#todo)

- Add behavior method to reconfigure, e.g. change configuration options.

License
-------

[](#license)

Copyright © 2009-2012 Joshua M. McNeese, Curtis J. Beeson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/59d2777f8c20cea49840f628d5b571f4d035e7145284c02799d4dafd8d6e156a?d=identicon)[jmcneese](/maintainers/jmcneese)

---

Top Contributors

[![jmcneese](https://avatars.githubusercontent.com/u/49743?v=4)](https://github.com/jmcneese "jmcneese (24 commits)")[![mrmorris](https://avatars.githubusercontent.com/u/146842?v=4)](https://github.com/mrmorris "mrmorris (2 commits)")[![joebeeson](https://avatars.githubusercontent.com/u/25078?v=4)](https://github.com/joebeeson "joebeeson (1 commits)")

### Embed Badge

![Health badge](/badges/jmcneese-bitmasked/health.svg)

```
[![Health](https://phpackages.com/badges/jmcneese-bitmasked/health.svg)](https://phpackages.com/packages/jmcneese-bitmasked)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
