| 
<?php
/**
 * This script demonstarates how to use bitwise operations
 * on enum objects.
 */
 
 require '../class/Co3/CodeGen/Enum.php';
 use Co3\CodeGen\Enum as Enum;
 //Using Enums with bitflags
 //create the Enum Role
 Enum::create(
 'Role',
 array(
 'Administrator',
 'Editor',
 'Programmer',
 'Manager',
 'Monkey'
 ),
 true //If the third parameter is true all genrated keys are powers of 2
 );
 
 //combine roles keys with |. The result is an integer
 $bobosRoles =
 Role::get(Role::Programmer)->getKey()
 | Role::get(Role::Monkey)->getKey()
 | Role::get(Role::Administrator)->getKey();
 
 //test if a bit is set
 foreach(Role::toArray() as $role){
 if($role->getKey() & $bobosRoles){
 echo "Bobo is a {$role} <br/>";
 } else {
 echo "Bobo is no {$role}<br/>";
 }
 }
 
 /*Output:
 Bobo is a Administrator
 Bobo is no Editor
 Bobo is a Programmer
 Bobo is no Manager
 Bobo is a Monkey
 */
 
 //create a enum with custom keys in a namespace
 Enum::create(
 'Social\Contact',
 array(
 1=>'Personal',
 3=>'Friend',//3 = 1|2
 5=>'Family',//5 = 1|4
 13=>'Parent',//13 = 5|8
 16=>'Business',
 48=>'CoWorker',//48 = 6|32
 )
 );
 
 $michael =
 Social\Contact::get('CoWorker')->getKey()
 | Social\Contact::get('Friend')->getKey();
 
 foreach(Social\Contact::toArray() as $contactType){
 if( ($contactType->getKey() & $michael) == $contactType->getKey()){
 echo "Michael is a '{$contactType}' contact <br/>";
 } else {
 echo "Michael is no '{$contactType}' contact <br/>";
 }
 }
 /*Output:
 Michael is a 'Personal' contact
 Michael is a 'Friend' contact
 Michael is no 'Family' contact
 Michael is no 'Parent' contact
 Michael is a 'Business' contact
 Michael is a 'CoWorker' contact
 */
 
 ?>
 |