Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- require_once 'exception.php';
- /**
- * get-/set
- */
- abstract class Accessible
- {
- private static $get = 'get_';
- private static $set = 'set_';
- private function access($name)
- {
- if(FALSE === property_exists($this, $name))
- throw new MissingFieldException(MissingFieldException::ERROR_MISSING_FIELD, get_class($this), $name);
- if(FALSE === method_exists($this, self::$get.$name)
- && FALSE === method_exists($this, self::$set.$name))
- throw new FieldAccessException(FieldAccessException::ERROR_NOT_ACCESSIBLE, get_class($this), $name);
- return TRUE;
- }
- public function __get($name)
- {
- if(TRUE === $this->access($name)
- && method_exists($this, self::$get.$name))
- return $this->{self::$get.$name}();
- throw new FieldNotReadableException(FieldNotReadableException::ERROR_NOT_READABLE, get_class($this), $name);
- }
- public function __set($name, $value)
- {
- if(TRUE === $this->access($name)
- && method_exists($this, self::$set.$name))
- return $this->{self::$set.$name}($value);
- throw new FieldNotWritableException(FieldNotWritableException::ERROR_NOT_WRITABLE, get_class($this), $name);
- }
- public function __call($name, $args)
- {
- if(FALSE === method_exists($this, $name))
- throw new MissingMethodException(MissingMethodException::ERROR_MISSING_METHOD, get_class($this), $name);
- throw new MethodAccessException(MethodAccessException::ERROR_NOT_ACCESSIBLE, get_class($this), $name);
- }
- }
- /**
- * Flag
- */
- class Flag implements IteratorAggregate, Serializable
- {
- private $__bits = 0;
- public function __construct($mask = FALSE) { $this->__bits = is_string($mask) ? $this->convertBinDec($mask) : $this->convertInteger($mask); }
- public function __toString() { return $this->convertDecBin($this->__bits); }
- public function getIterator() { return new ArrayIterator($this->toArray()); }
- public function serialize() { return $this->convertString($this->__bits); }
- public function unserialize($serialized) { $this->__bits = $this->convertInteger($serialized); }
- public function get($offset) { $mask = 1 << $offset; return ($mask & $this->__bits) == $mask; }
- public function set($offset) { $this->__bits |= 1 << $offset; return $this; }
- public function reset($offset) { $this->__bits &= ~ (1 << $offset); return $this; }
- public function toggle($offset) { $this->__bits ^= 1 << $offset; return $this; }
- public function add($val) { $this->__bits |= $val; return $this; }
- public function remove($val) { $this->__bits &= ~ $val; return $this; }
- public function toString($max = FALSE) { return (!is_integer($max) ? $this->__toString() : str_pad( $this->__toString(), $max, 0, STR_PAD_LEFT)); }
- public function toArray() { return str_split(strrev($this->__toString())); }
- public function getNumeric() { return $this->__bits; }
- public function getHex() { return $this->convertHex($this->__bits); }
- public function getOct() { return $this->convertOct($this->__bits); }
- public function getBin() { return $this->convertBin($this->__bits); }
- private function convertBinDec($value) { return bindec($value); }
- private function convertDecBin($value) { return decbin($value); }
- private function convertInteger($value) { return intval($value); }
- private function convertString($value) { return strval($value); }
- private function convertHex($value) { return $this->convertBase($value, 16); }
- private function convertOct($value) { return $this->convertBase($value, 8); }
- private function convertBin($value) { return $this->convertBase($value, 2); }
- private function convertBase($value, $base) { return base_convert($value, $base, 10); }
- }
- /**
- * variable-value-Struct
- */
- class Variable
- {
- const PERMISSION_READ = 0x00000001;
- const PERMISSION_WRITE_ONCE = 0x00000002;
- const PERMISSION_MODIFY = 0x00000004;
- const PERMISSION_ABSTRACT = 0x00000008;
- const iPERMISSION_READ = 0x00000000;
- const iPERMISSION_WRITE_ONCE = 0x00000001;
- const iPERMISSION_MODIFY = 0x00000002;
- const iPERMISSION_ABSTRACT = 0x00000003;
- const iSTATE_INITVALUE_SET = 0x00000000;
- const iSTATE_INITVALUE_OVERWRITE = 0x00000001;
- private $__PERMISSIONS = NULL;
- private $__STATE = NULL;
- private $__value = NULL;
- private $__modified = FALSE;
- public function __construct($value = NULL, Flag $PERMISSIONS = NULL)
- {
- $this->__value = $value;
- $this->__PERMISSIONS = $PERMISSIONS;
- if(NULL === $this->__PERMISSIONS)
- {
- $this->__PERMISSIONS = new Flag;
- $this->__PERMISSIONS->set(Variable::iPERMISSION_MODIFY);
- }
- $this->__PERMISSIONS->set(Variable::iPERMISSION_READ);
- $this->__STATE = new Flag;
- if(TRUE === $this->isAbstract()
- || TRUE === $this->isProtected()
- || NULL !== $this->__value)
- $this->__STATE->set(Variable::iSTATE_INITVALUE_SET);
- }
- public function set($value)
- {
- if(TRUE === $this->isModifiable())
- {
- $this->__value = $value;
- return $this;
- }
- if(FALSE === $this->isInitValueSet())
- {
- $this->__value = $value;
- $this->__STATE->set(Variable::iSTATE_INITVALUE_SET);
- return $this;
- }
- else
- {
- if(FALSE === $this->isWritable())
- throw new FieldNotWritableException(FieldNotWritableException::ERROR_NOT_MODIFIABLE);
- }
- $this->__STATE->set(Variable::iSTATE_INITVALUE_OVERWRITE);
- $this->__value = $value;
- return $this;
- }
- public function get()
- {
- if(FALSE === $this->isReadable())
- throw new FieldNotReadableException(FieldNotReadableException::ERROR_NOT_READABLE);
- if(FALSE === $this->isAbstractWritten())
- throw new FieldNotReadableException(FieldNotReadableException::ERROR_NOT_READABLE);
- return $this->__value;
- }
- public function isReadable() { return $this->__PERMISSIONS->get(Variable::iPERMISSION_READ); }
- public function isModifiable() { return $this->__PERMISSIONS->get(Variable::iPERMISSION_MODIFY); }
- public function isAbstract() { return $this->__PERMISSIONS->get(Variable::iPERMISSION_ABSTRACT); }
- public function isProtected() { return $this->__PERMISSIONS->get(Variable::iPERMISSION_WRITE_ONCE); }
- public function isInitValueSet() { return $this->__STATE->get(Variable::iSTATE_INITVALUE_SET); }
- public function isInitValueOverwritten() { return $this->__STATE->get(Variable::iSTATE_INITVALUE_OVERWRITE); }
- public function isWritable() { return ((TRUE === $this->isProtectedWritten()) ? FALSE : TRUE); }
- public function isAbstractWritten() { return !(TRUE === $this->isAbstract() && FALSE === $this->isInitValueOverwritten()); }
- public function isProtectedWritten() { return (TRUE === $this->isProtected() && TRUE === $this->isInitValueOverwritten()); }
- }
- class Value
- {
- private $__VALUE = NULL; // value
- private $__WORKING = NULL; // working-value
- private $__ORIGINAL = NULL; // original value
- public function __construct($value = NULL)
- {
- $this->__VALUE = new Variable($value, new Flag(Variable::PERMISSION_MODIFY));
- $this->__WORKING = new Variable($value, new Flag(Variable::PERMISSION_MODIFY));
- $this->__ORIGINAL = new Variable($value, new Flag);
- }
- public function get() { return $this->__WORKING->get(); }
- public function set($value) { $this->__WORKING->set($value); return $this; }
- /**
- * save changes: copy __WORKING to __VALUE
- */
- public function save() { $this->__VALUE->set($this->__WORKING->get()); return $this; }
- /**
- * restore value to last saved version: copy __VALUE to __WORKING
- */
- public function restore() { $this->__WORKING->set($this->__VALUE->get()); return $this; }
- /**
- * restore to default: copy __ORIGINAL to __WORKING and __VALUE
- */
- public function reset() { $this->__WORKING->set($this->__ORIGINAL->get()); $this->__VALUE->set($this->__ORIGINAL->get()); return $this; }
- }
- class System
- {
- public function finalize() { return $this; }
- }
- class SystemObject extends System
- {
- public function equals(Object $object) { return $this; }
- }
- class Type extends SystemObject
- {
- // 1 2 4 8 16 32 64 128 256 512 1024 2048 4096
- const iModifiable = 0x00000000;
- const iPrimitive = 0x00000001;
- const iObjectArray = 0x00000002;
- const iArrayAccess = 0x00000003;
- const iIterator = 0x00000004;
- const iAggregate = 0x00000005;
- const iCountable = 0x00000006;
- const iSerializable = 0x00000007;
- const iGenericTypeDefination = 0x00000008;
- const iNested = 0x00000009;
- private $__PERMISSIONS = NULL;
- private $__CONTEXT = NULL;
- private $__type = '';
- private static $__primitives = array('string' => TRUE, 'integer' => TRUE, 'boolean' => TRUE, 'float' => TRUE);
- public function __construct(SystemObject $object)
- {
- //if(!$object instanceOf Object)
- // ?? throw...
- $this->__type = get_class($object);
- $this->__PERMISSIONS = new Flag;
- $this->__PERMISSIONS->set(Type::iModifiable);
- $this->__CONTEXT = new Flag;
- if(isset(self::$__primitives[strtolower($this->__type)]))
- $this->__CONTEXT->set(Type::iPrimitive);
- if($object instanceOf ObjectArray) $this->__CONTEXT->set(Type::iObjectArray);
- if($object instanceOf ArrayAccess) $this->__CONTEXT->set(Type::iArrayAccess);
- if($object instanceOf Iterator) $this->__CONTEXT->set(Type::iIterator);
- if($object instanceOf IteratorAggregate) $this->__CONTEXT->set(Type::iAggregate);
- if($object instanceOf Countable) $this->__CONTEXT->set(Type::iCountable);
- if($object instanceOf Serializable) $this->__CONTEXT->set(Type::iSerializable);
- if($object instanceOf GenericTypeDefination) $this->__CONTEXT->set(Type::iGenericTypeDefination);
- if($object instanceOf Type) $this->__CONTEXT->set(Type::iNested);
- }
- public function getType() { return $this->__type; }
- public function isModifiable() { return $this->__PERMISSIONS->get(Type::iModifiable); }
- public function isPrimitive() { return $this->__CONTEXT->get(Type::iPrimitive); }
- public function isObjectArray() { return $this->__CONTEXT->get(Type::iObjectArray); }
- public function isArrayAccess() { return $this->__CONTEXT->get(Type::iArrayAccess); }
- public function isIterator() { return $this->__CONTEXT->get(Type::iIterator); }
- public function isAggregate() { return $this->__CONTEXT->get(Type::iAggregate); }
- public function isCountable() { return $this->__CONTEXT->get(Type::iCountable); }
- public function isSerializable() { return $this->__CONTEXT->get(Type::iCountable); }
- public function isGenericTypeDefination() { return $this->__CONTEXT->get(Type::iGenericTypeDefination); }
- public function isNested() { return $this->__CONTEXT->get(Type::iNested); }
- }
- class Object extends SystemObject
- {
- private $__TYPE = NULL;
- private $__VALUE = NULL;
- public function getType() { return $this->__TYPE; }
- public function __construct($value = NULL)
- {
- $this->__TYPE = new Type($this);
- $this->__VALUE = new Value($value);
- }
- public function get() { return $this->__VALUE->get(); }
- public function set($value) { $this->__VALUE->set($value); return $this; }
- public function save() { $this->__VALUE->save(); return $this; }
- public function restore() { $this->__VALUE->restore(); return $this; }
- }
- class String extends Object
- {
- public function toUpper()
- {
- $this->set(strtoupper($this->get()));
- return $this;
- }
- }
- try
- {
- /*
- generic variable
- */
- $v = new Variable;
- $v->set('foo'); // allowed
- $v->set('baz'); // allowed
- var_dump($v->get()); // 'baz'
- /*
- protected variables
- */
- /*
- // initialize 'bar' in constructor
- $v = new Variable('bar', new Flag(Variable::PERMISSION_WRITE_ONCE));
- $v->set('baz'); // exception: can not overwrite bar
- var_dump($v);
- */
- /*
- $v = new Variable(NULL, new Flag(Variable::PERMISSION_WRITE_ONCE));
- $v->set('bar'); // allowed: constructor initiaized as NULL
- $v->set('baz'); // exception: can not overwrite 'bar'
- var_dump($v);
- */
- /*
- abstract variables
- */
- /*
- $v = new Variable(NULL, new Flag(Variable::PERMISSION_ABSTRACT));
- var_dump($v->get()); // exception: nothing set
- var_dump($v);
- */
- /*
- $v = new Variable(NULL, new Flag(Variable::PERMISSION_ABSTRACT));
- $v->set('baz'); // overwrite
- var_dump($v->get()); // 'baz'
- var_dump($v);
- */
- /*
- // abstract 'baz' in constructor
- $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT));
- var_dump($v->get()); // exception: 'baz' not overwritten
- var_dump($v);
- */
- /*
- // abstract 'baz' in constructor
- $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT));
- $v->set('bar');
- var_dump($v->get()); // 'bar'
- var_dump($v);
- */
- /*
- abstract protected variables
- */
- /*
- $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT | Variable::PERMISSION_WRITE_ONCE));
- var_dump($v->get()); // exception: 'baz' not overwritten
- var_dump($v);
- */
- /*
- $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT | Variable::PERMISSION_WRITE_ONCE));
- $v->set('bar');
- var_dump($v->get()); // 'bar'
- var_dump($v);
- */
- /*
- $v = new Variable('baz', new Flag(Variable::PERMISSION_ABSTRACT | Variable::PERMISSION_WRITE_ONCE));
- $v->set('bar');
- $v->set('foo');
- var_dump($v->get()); // exception can not overwrite 'bar'
- var_dump($v);
- */
- //$s = new string('foo');
- //var_dump($s->set('bar')->toUpper()->save());
- /*
- $v = new Variable('foo');
- $v->set('bar'); // exception
- var_dump($v);
- */
- /*
- $v = new Variable('foo', new Flag(Variable::PERMISSION_WRITE));
- $v->set('bar'); // allowed
- $v->set('baz'); // exception
- var_dump($v);
- */
- /*
- $v = new Variable(NULL, new Flag(Variable::PERMISSION_WRITE));
- $v->set('bar'); // allowed
- $v->set('baz'); // exception
- var_dump($v);
- */
- /*
- $v = new Variable('foo', new Flag(Variable::PERMISSION_MODIFY));
- $v->set('bar'); // allowed
- $v->set('baz'); // allowed
- var_dump($v);
- */
- }
- catch(Exception $e)
- {
- print $e;
- }
- /*
- $o = new Type(new Type(new Object()));
- var_dump($o);
- var_dump($s->getType()->isCountable());
- */
- //$f = new Flag(1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 1024);
- //$f->reset(8);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement