Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- class SystemException extends Exception
- {
- public function __construct($msg = NULL, $code = 0, ObjectException $lasterror = NULL)
- {
- $message = '';
- $message .= sprintf('0x%1s:', str_pad(dechex($code), 8, 0, STR_PAD_LEFT))."\t";
- $message .= ((!empty($msg)) ? $msg : 'no exception message');
- if(NULL != $lasterror)
- $message .= ":\n".$lasterror->getMessage();
- $message .= "\n";
- parent::__construct($message, $code);
- }
- }
- class ObjectException extends SystemException{}
- class ArgumentException extends ObjectException
- {
- const ERR_IS_NULL = 501;
- const ERR_NO_STRING = 502;
- const ERR_NO_INT = 503;
- private static $errors = array
- (
- 501 => 'Value cannot be NULL',
- 502 => 'Value is not a string',
- 503 => 'Value is not an integer'
- );
- public function __construct($code, ObjectException $lasterror = NULL)
- {parent::__construct(self::$errors[$code], $code, $lasterror);}
- }
- class FormatException extends ObjectException
- {
- const ERR_CANNOT_PARSE = 601;
- private static $errors = array
- (
- 601 => 'Cannot parse value'
- );
- public function __construct($code, ObjectException $lasterror = NULL)
- {parent::__construct(self::$errors[$code], $code, $lasterror);}
- }
- class ObjectArrayException extends ObjectException
- {
- const ERR_NO_REGISTER = 701;
- private static $errors = array
- (
- 701 => 'Unable to register value'
- );
- public function __construct($code, ObjectException $lasterror = NULL)
- {parent::__construct(self::$errors[$code], $code, $lasterror);}
- }
- class System {}
- class Object extends System
- {
- const Blank = NULL;
- public static function equals(Object $object, Object $object2 = null)
- {return ((NULL === $object2) ? ($this === $object) : ($object2 === $object));}
- private $__buffer = self::Blank;
- public function __construct($value){$this->__buffer = $value;}
- public function __toString(){return $this->__buffer;}
- }
- class String extends Object
- {
- const Blank = '';
- public function __construct($value)
- {
- if(NULL === $value){throw new ArgumentException(ArgumentException::ERR_IS_NULL); return;}
- if(!is_string($value)){throw new ArgumentException(ArgumentException::ERR_NO_STRING); return;}
- parent::__construct($value);
- }
- public function __toString()
- {return parent::__toString();}
- }
- class Boolean extends Object
- {
- private static $falseString = 'false';
- private static $trueString = 'true';
- public static function parse(String $value)
- {
- if(NULL === $value)
- {
- throw new ArgumentException(ArgumentException::ERR_IS_NULL);
- return FALSE;
- }
- $value = strtolower(trim($value));
- $result = ((bool) ($value == self::$falseString || $value == self::$trueString));
- if(FALSE === $result)
- {
- throw new FormatException(FormatException::ERR_CANNOT_PARSE);
- return FALSE;
- }
- return TRUE;
- }
- public function __toString()
- {return parent::__toString();}
- }
- INTERFACE ObjectArray_Interface EXTENDS COUNTABLE, ITERATOR{}
- INTERFACE StringArray_Interface EXTENDS ObjectArray_Interface{}
- class ObjectArray implements ObjectArray_Interface
- {
- private $__stack = array();
- private $__index = 0;
- public function count(){return $this->size();}
- public function current(){return current($this->__stack);}
- public function key(){return key($this->__stack);}
- public function next(){next($this->__stack); $this->__index++;}
- public function rewind(){reset($this->__stack); $this->__index = 0;}
- public function valid(){return $this->__index < $this->size();}
- public function size(){return sizeOf($this->__stack);}
- public function set($index, $value){$this->__stack[$index] = $value; return $this;}
- public function add($value){$this->__stack[] = $value; return $this;}
- public function get($index){$this->__stack[$key] = $value; return $this;}
- }
- class StringArray extends ObjectArray implements StringArray_Interface
- {
- public function __construct(array $array)
- {
- foreach($array as $string)
- try{parent::add(($string instanceOf String) ? $string : new String($string));}
- catch(ArgumentException $e){throw new ObjectArrayException(ObjectArrayException::ERR_NO_REGISTER, $e);}
- }
- }
- class Unit
- {
- public static function object()
- {
- $object1 = new Object;
- $object2 = new Object;
- var_dump($object1->equals($object1));
- var_dump($object1->equals($object2));
- var_dump(Object::equals($object1, $object2));
- var_dump(Object::equals($object1));
- }
- function boolean()
- {
- try
- {
- $units = new StringArray(array
- (
- String::Blank,
- "True",
- "False",
- "true",
- "false",
- " true ",
- "0",
- "1",
- "-1",
- "string"
- ));
- foreach($units as $unit)
- {
- try
- {
- var_dump(array($unit, Boolean::parse($unit)));
- }
- catch(FormatException $e)
- {
- switch($e->getCode()):
- case FormatException::ERR_CANNOT_PARSE:
- var_dump(array($unit, $e->getMessage()));
- break;
- endswitch;
- }
- }
- }
- catch(ObjectArrayException $e)
- {
- print $e->getMessage();
- }
- }
- }
- header('Content-Type: text/plain');
- Unit::boolean();
- /*
- var_dump($a->equals($b));
- var_dump(Object::equals($a, $b));
- var_dump(Object::equals($a, $a));
- var_dump(Object::equals($a));
- */
Advertisement
Add Comment
Please, Sign In to add comment