Advertisement
fruffl

Untitled

Jan 27th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.02 KB | None | 0 0
  1. <?PHP
  2.  
  3. require_once 'exception.php';
  4.  
  5. /**
  6.  * get-/set
  7.  */
  8. abstract class Accessible
  9. {
  10.     private static $get = 'get_';
  11.     private static $set = 'set_';
  12.     private function access($name)
  13.     {
  14.         if(FALSE === property_exists($this, $name))
  15.             throw new MissingFieldException(MissingFieldException::ERROR_MISSING_FIELD, get_class($this), $name);
  16.            
  17.         if(FALSE === method_exists($this, self::$get.$name)
  18.         && FALSE === method_exists($this, self::$set.$name))
  19.             throw new FieldAccessException(FieldAccessException::ERROR_NOT_ACCESSIBLE, get_class($this), $name);
  20.        
  21.         return TRUE;
  22.     }
  23.    
  24.     public function __get($name)
  25.     {
  26.         if(TRUE === $this->access($name)
  27.         && method_exists($this, self::$get.$name))
  28.             return $this->{self::$get.$name}();
  29.        
  30.         throw new FieldNotReadableException(FieldNotReadableException::ERROR_NOT_READABLE, get_class($this), $name);
  31.     }
  32.    
  33.     public function __set($name, $value)
  34.     {
  35.         if(TRUE === $this->access($name)
  36.         && method_exists($this, self::$set.$name))
  37.             return $this->{self::$set.$name}($value);
  38.            
  39.         throw new FieldNotWritableException(FieldNotWritableException::ERROR_NOT_WRITABLE, get_class($this), $name);
  40.     }
  41.    
  42.     public function __call($name, $args)
  43.     {
  44.         if(FALSE === method_exists($this, $name))
  45.             throw new MissingMethodException(MissingMethodException::ERROR_MISSING_METHOD, get_class($this), $name);
  46.        
  47.         throw new MethodAccessException(MethodAccessException::ERROR_NOT_ACCESSIBLE, get_class($this), $name);
  48.     }
  49. }
  50.  
  51. /**
  52.  * Flag
  53.  */
  54. class Flag implements IteratorAggregate, Serializable
  55. {
  56.     private $__bits = 0;
  57.    
  58.     public function __construct($mask = FALSE)  { $this->__bits = is_string($mask) ? $this->convertBinDec($mask) : $this->convertInteger($mask); } 
  59.     public function __toString()            { return $this->convertDecBin($this->__bits); }
  60.     public function getIterator()           { return new ArrayIterator($this->toArray()); }
  61.     public function serialize()         { return $this->convertString($this->__bits); }
  62.     public function unserialize($serialized)    { $this->__bits = $this->convertInteger($serialized); }
  63.    
  64.     public function get($offset)            { $mask = 1 << $offset; return ($mask & $this->__bits) == $mask; }
  65.     public function set($offset)            { $this->__bits |= 1 << $offset; return $this; }
  66.     public function reset($offset)          { $this->__bits &= ~ (1 << $offset); return $this; }
  67.     public function toggle($offset)         { $this->__bits ^= 1 << $offset; return $this; }
  68.    
  69.     public function add($val)           { $this->__bits |= $val; return $this; }
  70.     public function remove($val)            { $this->__bits &= ~ $val; return $this; }
  71.    
  72.     public function toString($max = FALSE)      { return (!is_integer($max) ? $this->__toString() : str_pad( $this->__toString(), $max, 0, STR_PAD_LEFT)); }
  73.     public function toArray()           { return str_split(strrev($this->__toString())); }
  74.    
  75.     public function getNumeric()            { return $this->__bits; }
  76.     public function getHex()            { return $this->convertHex($this->__bits); }
  77.     public function getOct()            { return $this->convertOct($this->__bits); }
  78.     public function getBin()            { return $this->convertBin($this->__bits); }
  79.    
  80.     private function convertBinDec($value)      { return bindec($value); }
  81.     private function convertDecBin($value)      { return decbin($value); }
  82.     private function convertInteger($value)     { return intval($value); }
  83.     private function convertString($value)      { return strval($value); }
  84.     private function convertHex($value)     { return $this->convertBase($value, 16); }
  85.     private function convertOct($value)     { return $this->convertBase($value, 8); }
  86.     private function convertBin($value)     { return $this->convertBase($value, 2); }
  87.     private function convertBase($value, $base) { return base_convert($value, $base, 10); }
  88. }
  89.  
  90. /**
  91.  * variable-value-Struct
  92.  */
  93. class Variable
  94. {
  95.     const PERMISSION_READ           = 0x00000001;
  96.     const PERMISSION_WRITE_ONCE     = 0x00000002;
  97.     const PERMISSION_MODIFY         = 0x00000004;
  98.     const PERMISSION_ABSTRACT       = 0x00000008;
  99.    
  100.     const iPERMISSION_READ          = 0x00000000;
  101.     const iPERMISSION_WRITE_ONCE        = 0x00000001;
  102.     const iPERMISSION_MODIFY        = 0x00000002;
  103.     const iPERMISSION_ABSTRACT      = 0x00000003;
  104.    
  105.     const iSTATE_INITVALUE_SET      = 0x00000000;
  106.     const iSTATE_INITVALUE_OVERWRITE    = 0x00000001;
  107.    
  108.     private $__PERMISSIONS      = NULL;
  109.     private $__STATE        = NULL;
  110.    
  111.     private $__value        = NULL;
  112.     private $__modified     = FALSE;
  113.    
  114.     public function __construct($value = NULL, Flag $PERMISSIONS = NULL)
  115.     {
  116.         $this->__value      = $value;
  117.         $this->__PERMISSIONS    = $PERMISSIONS;
  118.        
  119.         if(NULL === $this->__PERMISSIONS)
  120.         {
  121.             $this->__PERMISSIONS = new Flag;
  122.             $this->__PERMISSIONS->set(Variable::iPERMISSION_MODIFY);
  123.         }
  124.        
  125.         $this->__PERMISSIONS->set(Variable::iPERMISSION_READ);
  126.                
  127.         $this->__STATE = new Flag;
  128.        
  129.         if(TRUE === $this->isAbstract()
  130.         || TRUE === $this->isProtected()
  131.         || NULL !== $this->__value)
  132.             $this->__STATE->set(Variable::iSTATE_INITVALUE_SET);
  133.     }
  134.    
  135.     public function set($value)
  136.     {
  137.         if(TRUE === $this->isModifiable())
  138.         {
  139.             $this->__value = $value;
  140.             return $this;
  141.         }
  142.        
  143.         if(FALSE === $this->isInitValueSet())
  144.         {
  145.             $this->__value = $value;
  146.             $this->__STATE->set(Variable::iSTATE_INITVALUE_SET);
  147.             return $this;
  148.         }
  149.         else
  150.         {
  151.             if(FALSE === $this->isWritable())
  152.                 throw new FieldNotWritableException(FieldNotWritableException::ERROR_NOT_MODIFIABLE);
  153.         }
  154.        
  155.         $this->__STATE->set(Variable::iSTATE_INITVALUE_OVERWRITE);
  156.         $this->__value = $value;
  157.        
  158.         return $this;
  159.     }
  160.    
  161.     public function get()
  162.     {
  163.         if(FALSE === $this->isReadable())
  164.             throw new FieldNotReadableException(FieldNotReadableException::ERROR_NOT_READABLE);
  165.            
  166.         if(FALSE  === $this->isAbstractWritten())
  167.             throw new FieldNotReadableException(FieldNotReadableException::ERROR_NOT_READABLE);
  168.            
  169.         return $this->__value;
  170.     }
  171.    
  172.     public function isReadable()            { return $this->__PERMISSIONS->get(Variable::iPERMISSION_READ); }
  173.     public function isModifiable()          { return $this->__PERMISSIONS->get(Variable::iPERMISSION_MODIFY); }
  174.     public function isAbstract()            { return $this->__PERMISSIONS->get(Variable::iPERMISSION_ABSTRACT); }
  175.     public function isProtected()           { return $this->__PERMISSIONS->get(Variable::iPERMISSION_WRITE_ONCE); }
  176.     public function isInitValueSet()        { return $this->__STATE->get(Variable::iSTATE_INITVALUE_SET); }
  177.     public function isInitValueOverwritten()    { return $this->__STATE->get(Variable::iSTATE_INITVALUE_OVERWRITE); }  
  178.     public function isWritable()            { return ((TRUE === $this->isProtectedWritten()) ? FALSE : TRUE); }
  179.     public function isAbstractWritten()     { return !(TRUE  === $this->isAbstract() && FALSE === $this->isInitValueOverwritten()); }  
  180.     public function isProtectedWritten()        { return (TRUE === $this->isProtected() && TRUE === $this->isInitValueOverwritten()); }
  181. }
  182.  
  183. class Value
  184. {
  185.     private $__VALUE    = NULL; // value
  186.     private $__WORKING  = NULL; // working-value
  187.     private $__ORIGINAL = NULL; // original value
  188.    
  189.     public function __construct($value = NULL)
  190.     {
  191.         $this->__VALUE      = new Variable($value, new Flag(Variable::PERMISSION_MODIFY));
  192.         $this->__WORKING    = new Variable($value, new Flag(Variable::PERMISSION_MODIFY));
  193.         $this->__ORIGINAL   = new Variable($value, new Flag);
  194.     }
  195.    
  196.     public function get()       { return $this->__WORKING->get(); }
  197.    
  198.     public function set($value) { $this->__WORKING->set($value); return $this; }
  199.    
  200.     /**
  201.      * save changes: copy __WORKING to __VALUE
  202.      */
  203.     public function save()      { $this->__VALUE->set($this->__WORKING->get()); return $this; }
  204.    
  205.     /**
  206.      * restore value to last saved version: copy __VALUE to __WORKING
  207.      */
  208.     public function restore()   { $this->__WORKING->set($this->__VALUE->get()); return $this; }
  209.    
  210.     /**
  211.      * restore to default: copy __ORIGINAL to __WORKING and __VALUE
  212.      */
  213.     public function reset()     { $this->__WORKING->set($this->__ORIGINAL->get()); $this->__VALUE->set($this->__ORIGINAL->get()); return $this; }
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. class System
  229. {
  230.     public function finalize()  { return $this; }
  231. }
  232.  
  233. class SystemObject extends System
  234. {
  235.     public function equals(Object $object)  { return $this; }
  236. }
  237.  
  238.  
  239. class Type extends SystemObject
  240. {
  241.     // 1 2 4 8 16 32 64 128 256 512 1024 2048 4096
  242.    
  243.     const       iModifiable     = 0x00000000;
  244.    
  245.     const       iPrimitive      = 0x00000001;
  246.     const       iObjectArray        = 0x00000002;
  247.     const       iArrayAccess        = 0x00000003;
  248.     const       iIterator       = 0x00000004;
  249.     const       iAggregate      = 0x00000005;
  250.     const       iCountable      = 0x00000006;
  251.     const       iSerializable       = 0x00000007;
  252.     const       iGenericTypeDefination  = 0x00000008;
  253.     const       iNested         = 0x00000009;
  254.    
  255.     private     $__PERMISSIONS      = NULL;
  256.     private     $__CONTEXT      = NULL;
  257.    
  258.     private     $__type         = '';
  259.     private static  $__primitives       = array('string' => TRUE, 'integer' => TRUE, 'boolean' => TRUE, 'float' => TRUE);
  260.    
  261.     public function __construct(SystemObject $object)
  262.     {
  263.         //if(!$object instanceOf Object)
  264.         //  ?? throw...
  265.            
  266.         $this->__type = get_class($object);
  267.        
  268.         $this->__PERMISSIONS = new Flag;
  269.         $this->__PERMISSIONS->set(Type::iModifiable);
  270.        
  271.         $this->__CONTEXT = new Flag;
  272.         if(isset(self::$__primitives[strtolower($this->__type)]))
  273.             $this->__CONTEXT->set(Type::iPrimitive);
  274.            
  275.         if($object instanceOf ObjectArray)      $this->__CONTEXT->set(Type::iObjectArray);
  276.         if($object instanceOf ArrayAccess)      $this->__CONTEXT->set(Type::iArrayAccess);
  277.         if($object instanceOf Iterator)         $this->__CONTEXT->set(Type::iIterator);
  278.         if($object instanceOf IteratorAggregate)    $this->__CONTEXT->set(Type::iAggregate);
  279.         if($object instanceOf Countable)        $this->__CONTEXT->set(Type::iCountable);
  280.         if($object instanceOf Serializable)     $this->__CONTEXT->set(Type::iSerializable);
  281.         if($object instanceOf GenericTypeDefination)    $this->__CONTEXT->set(Type::iGenericTypeDefination);
  282.         if($object instanceOf Type)         $this->__CONTEXT->set(Type::iNested);
  283.        
  284.     }
  285.    
  286.     public function getType()           { return $this->__type; }
  287.    
  288.     public function isModifiable()          { return $this->__PERMISSIONS->get(Type::iModifiable); }
  289.    
  290.     public function isPrimitive()           { return $this->__CONTEXT->get(Type::iPrimitive); }
  291.     public function isObjectArray()         { return $this->__CONTEXT->get(Type::iObjectArray); }
  292.     public function isArrayAccess()         { return $this->__CONTEXT->get(Type::iArrayAccess); }
  293.     public function isIterator()            { return $this->__CONTEXT->get(Type::iIterator); }
  294.     public function isAggregate()           { return $this->__CONTEXT->get(Type::iAggregate); }
  295.     public function isCountable()           { return $this->__CONTEXT->get(Type::iCountable); }
  296.     public function isSerializable()        { return $this->__CONTEXT->get(Type::iCountable); }
  297.     public function isGenericTypeDefination()   { return $this->__CONTEXT->get(Type::iGenericTypeDefination); }
  298.     public function isNested()          { return $this->__CONTEXT->get(Type::iNested); }
  299.    
  300.    
  301. }
  302.  
  303. class Object extends SystemObject
  304. {
  305.     private $__TYPE     = NULL;
  306.     private $__VALUE    = NULL;
  307.    
  308.     public function getType()   { return $this->__TYPE; }
  309.    
  310.     public function __construct($value = NULL)
  311.     {
  312.         $this->__TYPE   = new Type($this);
  313.         $this->__VALUE  = new Value($value);
  314.     }
  315.    
  316.     public function get()       { return $this->__VALUE->get(); }
  317.     public function set($value) { $this->__VALUE->set($value); return $this; }
  318.     public function save()      { $this->__VALUE->save(); return $this; }
  319.     public function restore()   { $this->__VALUE->restore(); return $this; }
  320. }
  321.  
  322.  
  323. class String extends Object
  324. {
  325.     public function toUpper()
  326.     {
  327.         $this->set(strtoupper($this->get()));
  328.         return $this;
  329.     }
  330. }
  331.  
  332. try
  333. {
  334.    
  335.     /*
  336.         generic variable
  337.     */
  338.     $v = new Variable;
  339.     $v->set('foo'); // allowed
  340.     $v->set('baz'); // allowed
  341.     var_dump($v->get()); // 'baz'
  342.    
  343.    
  344.     /*
  345.         protected variables
  346.     */
  347.    
  348.     /*
  349.     // initialize 'bar' in constructor
  350.     $v = new Variable('bar', new Flag(Variable::PERMISSION_WRITE_ONCE));
  351.     $v->set('baz'); // exception: can not overwrite bar
  352.     var_dump($v);
  353.     */
  354.    
  355.     /*
  356.     $v = new Variable(NULL, new Flag(Variable::PERMISSION_WRITE_ONCE));
  357.     $v->set('bar'); // allowed: constructor initiaized as NULL
  358.     $v->set('baz'); // exception: can not overwrite 'bar'
  359.     var_dump($v);
  360.     */
  361.    
  362.     /*
  363.         abstract variables
  364.     */
  365.    
  366.     /*
  367.     $v = new Variable(NULL, new Flag(Variable::PERMISSION_ABSTRACT));
  368.     var_dump($v->get()); // exception: nothing set
  369.     var_dump($v);
  370.     */
  371.    
  372.     /*
  373.     $v = new Variable(NULL, new Flag(Variable::PERMISSION_ABSTRACT));
  374.     $v->set('baz'); // overwrite
  375.     var_dump($v->get()); // 'baz'
  376.     var_dump($v);
  377.     */
  378.        
  379.     /*
  380.     // abstract 'baz' in constructor
  381.     $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT));
  382.     var_dump($v->get()); // exception: 'baz' not overwritten
  383.     var_dump($v);
  384.     */
  385.    
  386.     /*
  387.     // abstract 'baz' in constructor
  388.     $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT));
  389.     $v->set('bar');
  390.     var_dump($v->get()); // 'bar'
  391.     var_dump($v);
  392.     */
  393.    
  394.     /*
  395.         abstract protected variables
  396.     */
  397.    
  398.     /*
  399.     $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT | Variable::PERMISSION_WRITE_ONCE));
  400.     var_dump($v->get()); // exception: 'baz' not overwritten
  401.     var_dump($v);
  402.     */
  403.    
  404.     /*
  405.     $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT | Variable::PERMISSION_WRITE_ONCE));
  406.     $v->set('bar');
  407.     var_dump($v->get()); // 'bar'
  408.     var_dump($v);
  409.     */
  410.    
  411.     /*
  412.     $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT | Variable::PERMISSION_WRITE_ONCE));
  413.     $v->set('bar');
  414.     $v->set('foo');
  415.     var_dump($v->get()); // exception can not overwrite 'bar'
  416.     var_dump($v);
  417.     */
  418.    
  419.    
  420.    
  421.    
  422.    
  423.    
  424.    
  425.    
  426.     //$s = new string('foo');
  427.     //var_dump($s->set('bar')->toUpper()->save());
  428.    
  429.     /*
  430.     $v  = new Variable('foo');
  431.     $v->set('bar'); // exception
  432.     var_dump($v);
  433.     */
  434.    
  435.     /*
  436.     $v  = new Variable('foo', new Flag(Variable::PERMISSION_WRITE));
  437.     $v->set('bar'); // allowed
  438.     $v->set('baz'); // exception
  439.     var_dump($v);
  440.     */
  441.    
  442.     /*
  443.     $v  = new Variable(NULL, new Flag(Variable::PERMISSION_WRITE));
  444.     $v->set('bar'); // allowed
  445.     $v->set('baz'); // exception
  446.     var_dump($v);
  447.     */
  448.    
  449.     /*
  450.     $v  = new Variable('foo', new Flag(Variable::PERMISSION_MODIFY));
  451.     $v->set('bar'); // allowed
  452.     $v->set('baz'); // allowed
  453.     var_dump($v);
  454.     */
  455.    
  456. }
  457. catch(Exception $e)
  458. {
  459.     print $e;
  460. }
  461.  
  462. /*
  463. $o = new Type(new Type(new Object()));
  464. var_dump($o);
  465.  
  466. var_dump($s->getType()->isCountable());
  467. */
  468. //$f = new Flag(1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 1024);
  469. //$f->reset(8);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement