Advertisement
fruffl

Untitled

Oct 25th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.71 KB | None | 0 0
  1. class Flag implements IteratorAggregate, Serializable
  2. {
  3.     private $__bits = 0;
  4.    
  5.     public function __construct($mask = FALSE)  { $this->__bits = is_string($mask) ? $this->convertBinDec($mask) : $this->convertInteger($mask); } 
  6.     public function __toString()            { return $this->convertDecBin($this->__bits); }
  7.     public function getIterator()           { return new ArrayIterator($this->toArray()); }
  8.     public function serialize()         { return $this->convertString($this->__bits); }
  9.     public function unserialize($serialized)    { $this->__bits = $this->convertInteger($serialized); }
  10.    
  11.     public function get($offset)            { $mask = 1 << $offset; return ($mask & $this->__bits) == $mask; }
  12.     public function set($offset)            { $this->__bits |= 1 << $offset; return $this; }
  13.     public function reset($offset)          { $this->__bits &= ~ (1 << $offset); return $this; }
  14.     public function toggle($offset)         { $this->__bits ^= 1 << $offset; return $this; }
  15.    
  16.     public function add($val)           { $this->__bits |= $val; return $this; }
  17.     public function remove($val)            { $this->__bits &= ~ $val; return $this; }
  18.    
  19.     public function toString($max = FALSE)      { return (!is_integer($max) ? $this->__toString() : str_pad( $this->__toString(), $max, 0, STR_PAD_LEFT)); }
  20.     public function toArray()           { return str_split(strrev($this->__toString())); }
  21.    
  22.     public function getNumeric()            { return $this->__bits; }
  23.     public function getHex()            { return $this->convertHex($this->__bits); }
  24.     public function getOct()            { return $this->convertOct($this->__bits); }
  25.     public function getBin()            { return $this->convertBin($this->__bits); }
  26.    
  27.     private function convertBinDec($value)      { return bindec($value); }
  28.     private function convertDecBin($value)      { return decbin($value); }
  29.     private function convertInteger($value)     { return intval($value); }
  30.     private function convertString($value)      { return strval($value); }
  31.     private function convertHex($value)     { return $this->convertBase($value, 16); }
  32.     private function convertOct($value)     { return $this->convertBase($value, 8); }
  33.     private function convertBin($value)     { return $this->convertBase($value, 2); }
  34.     private function convertBase($value, $base) { return base_convert($value, $base, 10); }
  35. }
  36.  
  37. class Type extends SystemObject
  38. {
  39.     // 1 2 4 8 16 32 64 128 256 512 1024 2048 4096
  40.    
  41.     const       iPrimitive      = 0x00000000;
  42.     const       iModifiable     = 0x00000001;
  43.     const       iObjectArray        = 0x00000008;
  44.     const       iArrayAccess        = 0x00000009;
  45.     const       iIterator       = 0x0000000A;
  46.     const       iAggregate      = 0x0000000B;
  47.     const       iCountable      = 0x0000000C;
  48.     const       iSerializable       = 0x0000000D;
  49.     const       iGenericTypeDefination  = 0x0000000E;
  50.     const       iNested         = 0x0000001F;
  51.    
  52.     private     $__CONTEXT      = NULL;
  53.     private     $__type         = '';
  54.     private static  $__primitives       = array('string' => TRUE, 'integer' => TRUE, 'boolean' => TRUE, 'float' => TRUE);
  55.    
  56.     public function __construct(SystemObject $object)
  57.     {
  58.         //if(!$object instanceOf Object)
  59.         //  ?? throw...
  60.            
  61.         $this->__type = get_class($object);
  62.        
  63.         $this->__CONTEXT = new Flag;
  64.         $this->__CONTEXT->set(Type::iModifiable);
  65.        
  66.         if(isset(self::$__primitives[strtolower($this->__type)]))
  67.             $this->__CONTEXT->set(Type::iPrimitive);
  68.            
  69.         if($object instanceOf ObjectArray)      $this->__CONTEXT->set(Type::iObjectArray);
  70.         if($object instanceOf ArrayAccess)      $this->__CONTEXT->set(Type::iArrayAccess);
  71.         if($object instanceOf Iterator)         $this->__CONTEXT->set(Type::iIterator);
  72.         if($object instanceOf IteratorAggregate)    $this->__CONTEXT->set(Type::iAggregate);
  73.         if($object instanceOf Countable)        $this->__CONTEXT->set(Type::iCountable);
  74.         if($object instanceOf Serializable)     $this->__CONTEXT->set(Type::iSerializable);
  75.         if($object instanceOf GenericTypeDefination)    $this->__CONTEXT->set(Type::iGenericTypeDefination);
  76.         if($object instanceOf Type)         $this->__CONTEXT->set(Type::iNested);
  77.        
  78.     }
  79.    
  80.     public function getType()           { return $this->__type; }
  81.    
  82.     public function isPrimitive()           { return $this->__CONTEXT->get(Type::iPrimitive); }
  83.     public function isModifiable()          { return $this->__CONTEXT->get(Type::iModifiable); }
  84.     public function isObjectArray()         { return $this->__CONTEXT->get(Type::iObjectArray); }
  85.     public function isArrayAccess()         { return $this->__CONTEXT->get(Type::iArrayAccess); }
  86.     public function isIterator()            { return $this->__CONTEXT->get(Type::iIterator); }
  87.     public function isAggregate()           { return $this->__CONTEXT->get(Type::iAggregate); }
  88.     public function isCountable()           { return $this->__CONTEXT->get(Type::iCountable); }
  89.     public function isSerializable()        { return $this->__CONTEXT->get(Type::iCountable); }
  90.     public function isGenericTypeDefination()   { return $this->__CONTEXT->get(Type::iGenericTypeDefination); }
  91.     public function isNested()          { return $this->__CONTEXT->get(Type::iNested); }
  92.    
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement