PHP Classes

File: Bitmask.php

Recommend this page to a friend!
  Classes of Till Wehowski   Flag Mask   Bitmask.php   Download  
File: Bitmask.php
Role: Class source
Content type: text/plain
Description: Bitmask Flags Class
Class: Flag Mask
Manage groups of bit values in bitmasks
Author: By
Last change:
Date: 10 years ago
Size: 1,673 bytes
 

Contents

Class file image Download
<?php
/*################################################################################*
#
# (c)Copyright Till Wehowski, http://Webfan.de
# License: Do What The Fuck You Want To Public License
*/


namespace webdof\Serialize\Bit;
use
webdof\Serialize;


abstract class
Bitmask
{
  protected
$flags;

  function
__construct()
    {
      
$this->clear();
    }

  public function
save()
    {
      return
$this->flags;
    }

  public function
load($flags)
    {
      
$this->flags = (int)$flags;
       return
$this->get($this->flags, TRUE);
    }

  public function
clear()
    {
     
$this->load(0);
    }


  protected function
get($x, $set = TRUE)
    {
     
$r = array();
     
$n = 1;
     
$c = 0;
      while (
$x > 0 ) {
           if (
$x & 1 == 1 ) {
               
$r[$c] = $n;
            }
        
$n *= 2 ;
        
$x >>= 1 ;
        
$c++;
       }
      return
$r;
    }

  public function
is($flag)
  {
    return ((
$this->flags & $flag) === $flag);
  }

  public function
set($flag, $value)
  {
    if(
is_bool($value)) {
            
$value=($value===TRUE) ? 1 : 0;
           }elseif(
is_numeric($value)){
                    
$value = ($value >= 1) ? 1 : 0;
                }
    if(
$value < 1)unset($value);
    if(isset(
$value) && $value !== 1)
            
trigger_error( 'Data Type Error in '.get_class($this)
                        .
' '.__METHOD__.' '.basename(__FILE__).' line '.__LINE__ , E_USER_FATAL );

    if(isset(
$value) && $value === 1)
    {
     
$this->flags |= $flag;
    }
    else
    {
     
$this->flags &= ~$flag;
    }

  }




}