Skip to content

Commit

Permalink
Usage guide.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliance committed Nov 10, 2016
1 parent 3eae48e commit 8395539
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,55 @@ And just run installation command
$ composer.phar install
```

Usage
---

See usage in [sample](./example/example.php) file.

```
Aliance/InfiniteBitmask $ php -f example/example.php
Check user for all flags:
Premium: no
Wizard already shown: no
Paid ever: no
Referral: no
Banned: no
Already notified: no
–––––––––––––––––––––––––––––––––––
Check user for all flags:
Premium: no
Wizard already shown: no
Paid ever: yes
Referral: no
Banned: yes
Already notified: yes
–––––––––––––––––––––––––––––––––––
array(3) {
[0]=>
int(8)
[1]=>
int(1)
[2]=>
int(1)
}
Check user for all flags:
Premium: no
Wizard already shown: no
Paid ever: yes
Referral: no
Banned: yes
Already notified: no
–––––––––––––––––––––––––––––––––––
array(3) {
[0]=>
int(8)
[1]=>
int(1)
[2]=>
int(0)
}
```

Tests
---

Expand Down
67 changes: 67 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* For example, we have big project with users stored in some storage.
* The main entity for us – users.
* Users could have the dozens of flags (true/false states). For example:
* - is premium user;
* - has wizard already shown;
* - has paid ever;
* - is referral user, bought from somewhere;
* - ... etc ...
* - is banned;
* - ... etc ...
* - has already notified today...
*
* We can create dozens of columns with type BOOLEAN for each flags
* or just one column with array of integers (simple bitmasks).
*/

define('IS_PREMIUM', 1);
define('HAS_WIZARD_ALREADY_SHOWN', 2);
define('HAS_PAID_EVER', 3);
define('IS_REFERRAL', 4);
// ...
define('IS_BANNED', 63); // bitmask number greater than 62*1
// ...
define('HAS_ALREADY_NOTIFIED', 126); // bitmask number greater than 62*2

// some users from storage
$user = [
'bitmasks' => [], // default empty bitmasks
];

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

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

checkFlags($Bitmask);

// set user some flags
$Bitmask->setBit(HAS_PAID_EVER);
$Bitmask->setBit(IS_BANNED);
$Bitmask->setBit(HAS_ALREADY_NOTIFIED);

checkFlags($Bitmask);

var_dump($Bitmask->getMaskSlices());

// unset daily flag
$Bitmask->unsetBit(HAS_ALREADY_NOTIFIED);

checkFlags($Bitmask);

var_dump($Bitmask->getMaskSlices());

function checkFlags(\Aliance\InfiniteBitmask\InfiniteBitmask $Bitmask)
{
echo 'Check user for all flags:', PHP_EOL;
echo 'Premium: ', $Bitmask->issetBit(IS_PREMIUM) ? 'yes' : 'no', PHP_EOL;
echo 'Wizard already shown: ', $Bitmask->issetBit(HAS_WIZARD_ALREADY_SHOWN) ? 'yes' : 'no', PHP_EOL;
echo 'Paid ever: ', $Bitmask->issetBit(HAS_PAID_EVER) ? 'yes' : 'no', PHP_EOL;
echo 'Referral: ', $Bitmask->issetBit(IS_REFERRAL) ? 'yes' : 'no', PHP_EOL;
echo 'Banned: ', $Bitmask->issetBit(IS_BANNED) ? 'yes' : 'no', PHP_EOL;
echo 'Already notified: ', $Bitmask->issetBit(HAS_ALREADY_NOTIFIED) ? 'yes' : 'no', PHP_EOL;
echo str_repeat('', 35), PHP_EOL;
}

0 comments on commit 8395539

Please sign in to comment.