fruffl

TypeHinting Objects

Sep 12th, 2011
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.26 KB | None | 0 0
  1. <?PHP
  2.  
  3. class SystemException extends Exception
  4. {
  5.     public function __construct($msg = NULL, $code = 0, ObjectException $lasterror = NULL)
  6.     {
  7.        
  8.         $message  = '';
  9.         $message .= sprintf('0x%1s:', str_pad(dechex($code), 8, 0, STR_PAD_LEFT))."\t";
  10.         $message .= ((!empty($msg)) ? $msg : 'no exception message');
  11.        
  12.         if(NULL != $lasterror)
  13.             $message .= ":\n".$lasterror->getMessage();
  14.        
  15.         $message .= "\n";
  16.        
  17.         parent::__construct($message, $code);
  18.     }
  19. }
  20.  
  21. class ObjectException extends SystemException{}
  22. class ArgumentException extends ObjectException
  23. {
  24.     const ERR_IS_NULL   = 501;
  25.     const ERR_NO_STRING = 502;
  26.     const ERR_NO_INT    = 503;
  27.    
  28.     private static $errors = array
  29.     (
  30.         501 => 'Value cannot be NULL',
  31.         502 => 'Value is not a string',
  32.         503 => 'Value is not an integer'
  33.     );
  34.    
  35.     public function __construct($code, ObjectException $lasterror = NULL)
  36.         {parent::__construct(self::$errors[$code], $code, $lasterror);}
  37. }
  38.  
  39. class FormatException extends ObjectException
  40. {
  41.     const ERR_CANNOT_PARSE = 601;
  42.    
  43.     private static $errors = array
  44.     (
  45.         601 => 'Cannot parse value'
  46.     );
  47.    
  48.     public function __construct($code, ObjectException $lasterror = NULL)
  49.         {parent::__construct(self::$errors[$code], $code, $lasterror);}
  50. }
  51.  
  52. class ObjectArrayException extends ObjectException
  53. {
  54.     const ERR_NO_REGISTER = 701;
  55.    
  56.     private static $errors = array
  57.     (
  58.         701 => 'Unable to register value'
  59.     );
  60.    
  61.     public function __construct($code, ObjectException $lasterror = NULL)
  62.         {parent::__construct(self::$errors[$code], $code, $lasterror);}
  63. }
  64.  
  65.  
  66. class System {}
  67.  
  68.  
  69. class Object extends System
  70. {
  71.     const Blank = NULL;
  72.     public static function equals(Object $object, Object $object2 = null)
  73.         {return ((NULL === $object2) ? ($this === $object) : ($object2 === $object));}
  74.        
  75.     private $__buffer = self::Blank;
  76.     public function __construct($value){$this->__buffer = $value;}
  77.     public function __toString(){return $this->__buffer;}
  78. }
  79.  
  80. class String extends Object
  81. {
  82.     const Blank = '';
  83.     public function __construct($value)
  84.     {
  85.         if(NULL === $value){throw new ArgumentException(ArgumentException::ERR_IS_NULL); return;}          
  86.         if(!is_string($value)){throw new ArgumentException(ArgumentException::ERR_NO_STRING); return;}
  87.            
  88.         parent::__construct($value);
  89.     }
  90.    
  91.     public function __toString()
  92.         {return parent::__toString();}
  93. }
  94.  
  95. class Boolean extends Object
  96. {
  97.     private static $falseString = 'false';
  98.     private static $trueString  = 'true';
  99.    
  100.     public static function parse(String $value)
  101.     {
  102.         if(NULL === $value)
  103.         {
  104.             throw new ArgumentException(ArgumentException::ERR_IS_NULL);
  105.             return FALSE;
  106.         }
  107.            
  108.         $value = strtolower(trim($value));
  109.         $result = ((bool) ($value == self::$falseString || $value == self::$trueString));
  110.        
  111.         if(FALSE === $result)
  112.         {
  113.             throw new FormatException(FormatException::ERR_CANNOT_PARSE);
  114.             return FALSE;
  115.         }
  116.        
  117.         return TRUE;
  118.     }
  119.    
  120.     public function __toString()
  121.         {return parent::__toString();}
  122. }
  123.  
  124.  
  125. INTERFACE ObjectArray_Interface EXTENDS COUNTABLE, ITERATOR{}
  126. INTERFACE StringArray_Interface EXTENDS ObjectArray_Interface{}
  127.  
  128. class ObjectArray implements ObjectArray_Interface
  129. {
  130.     private $__stack = array();
  131.     private $__index = 0;
  132.     public function count(){return $this->size();}
  133.     public function current(){return current($this->__stack);}
  134.     public function key(){return key($this->__stack);}
  135.     public function next(){next($this->__stack); $this->__index++;}
  136.     public function rewind(){reset($this->__stack); $this->__index = 0;}
  137.     public function valid(){return $this->__index < $this->size();}
  138.    
  139.     public function size(){return sizeOf($this->__stack);}
  140.     public function set($index, $value){$this->__stack[$index] = $value; return $this;}
  141.     public function add($value){$this->__stack[] = $value; return $this;}
  142.     public function get($index){$this->__stack[$key] = $value; return $this;}
  143. }
  144.  
  145. class StringArray extends ObjectArray implements StringArray_Interface
  146. {
  147.     public function __construct(array $array)
  148.     {
  149.         foreach($array as $string)
  150.             try{parent::add(($string instanceOf String) ? $string : new String($string));}
  151.             catch(ArgumentException $e){throw new ObjectArrayException(ObjectArrayException::ERR_NO_REGISTER, $e);}
  152.     }
  153. }
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. class Unit
  162. {
  163.     public static function object()
  164.     {
  165.         $object1 = new Object;
  166.         $object2 = new Object;
  167.         var_dump($object1->equals($object1));
  168.         var_dump($object1->equals($object2));
  169.         var_dump(Object::equals($object1, $object2));
  170.         var_dump(Object::equals($object1));
  171.     }
  172.  
  173.     function boolean()
  174.     {
  175.         try
  176.         {
  177.             $units = new StringArray(array
  178.             (
  179.                 String::Blank,
  180.                 "True",
  181.                 "False",
  182.                 "true",
  183.                 "false",
  184.                 "    true    ",
  185.                 "0",
  186.                 "1",
  187.                 "-1",
  188.                 "string"
  189.             ));
  190.            
  191.             foreach($units as $unit)
  192.             {
  193.                 try
  194.                 {
  195.                     var_dump(array($unit, Boolean::parse($unit)));
  196.                 }
  197.                 catch(FormatException $e)
  198.                 {
  199.                     switch($e->getCode()):
  200.                         case FormatException::ERR_CANNOT_PARSE:
  201.                             var_dump(array($unit, $e->getMessage()));
  202.                             break;
  203.                     endswitch;
  204.                 }
  205.             }
  206.         }
  207.         catch(ObjectArrayException $e)
  208.         {
  209.             print $e->getMessage();
  210.         }
  211.        
  212.     }
  213. }
  214.  
  215.  
  216.  
  217. header('Content-Type: text/plain');
  218. Unit::boolean();
  219. /*
  220. var_dump($a->equals($b));
  221. var_dump(Object::equals($a, $b));
  222. var_dump(Object::equals($a, $a));
  223. var_dump(Object::equals($a));
  224. */
Advertisement
Add Comment
Please, Sign In to add comment