-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
67 lines (54 loc) · 2.06 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
require_once realpath(__DIR__ . '/../vendor/autoload.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', 0);
define('HAS_WIZARD_ALREADY_SHOWN', 1);
define('HAS_PAID_EVER', 2);
define('IS_REFERRAL', 3);
// ...
define('IS_BANNED', 64); // bitmask number greater than 64*1-1
// ...
define('HAS_ALREADY_NOTIFIED', 128); // bitmask number greater than 64*2-1
// some users from storage
$user = [
'bitmasks' => [], // default empty bitmasks
];
// 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;
}