Advertisement
fruffl

Untitled

Jan 27th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 32.25 KB | None | 0 0
  1. <?PHP
  2. //error_reporting(E_ALL|E_STRICT);
  3.  
  4. require_once 'exception.php';
  5. //header('Content-Type: text/plain');
  6.  
  7.  
  8. abstract class  System
  9. {
  10.         const           GC_COLLECT_CYCLES                           = 'gc_collect_cycles';
  11.         public          function finalize()                         { if(isset($__func_exists[self::GC_COLLECT_CYCLES])) gc_collect_cycles(); return $this; }
  12.         public          function isReference(Object $object)                    { /* how to check this? */ }
  13.     final   public          function getHashCode()                          { return $this->internalHash()->getHashCode(); }
  14.         public          function __construct()                          { $this->setHashCode()->function_register(self::GC_COLLECT_CYCLES);}
  15.         public          function __destruct()                           { $this->finalize(); }
  16.         private         function setHashCode()                          { $this->__hash = new Hash(); return $this; }
  17.         private         function function_register($name)                   { if(!isset(self::$__func_checked[$name])){ self::$__func_checked[$name] = (bool) function_exists($name); if(TRUE === self::$__func_checked[$name]) self::$__func_exists[$name] = self::$__func_checked[$name];} }
  18.         private         $__hash                                 = NULL;
  19.         private     static  $__func_checked                             = array();
  20.         private     static  $__func_exists                              = array();
  21.         protected       function internalHash()                         { return $this->__hash; }
  22. }
  23.  
  24. final class     Hash
  25. {
  26.         public          function getHashCode()                          { return new Integer($this->__hashCode); }
  27.         public          function __construct()                          { $this->__hashCode = mt_rand(); }
  28.         private         $__hashCode                             = NULL;
  29. }
  30.  
  31.  
  32. interface   Type_Interface{}
  33.  
  34. final class Type
  35. extends     System
  36. implements  Type_Interface
  37. {
  38.         public          function isArrayObject()                        { return $this->__isArrayObject; }
  39.         public          function isArrayAccess()                        { return $this->__isArrayAccess; }
  40.         public          function isIterator()                           { return $this->__isIterator; }
  41.         public          function isCountable()                          { return $this->__isCountable; }
  42.         public          function isPrimitive()                          { return $this->__isPrimitive; }
  43.         public          function getType()                          { return $this->__type; }
  44.         public          function isLocked()                         { return $this->__isLocked; }
  45.         public          function lock()                             { $this->__isLocked = TRUE; return $this; }
  46.         public          function lockWithTrace()                        { $this->lock()->__locktrace = debug_backtrace(); return $this; }
  47.         public          function isModifiable()                         { if(TRUE === $this->isLocked()) throw new UnauthorizedAccessException(UnauthorizedAccessException::ERROR_LOCKED_FOR_MOD); return TRUE; }
  48.         public          function getLockTrace()                         { return $this->__locktrace; }
  49.         public          function __construct(SystemObject $value)
  50.                     {
  51.                         parent::__construct();
  52.                         $this->__type                           = get_class($value);
  53.                         $this->__isPrimitive                        = isset(self::$__primitives[strtolower($this->__type)]);
  54.                         $this->__isArrayObject                      = ($value instanceOf ObjectArray);
  55.                         $this->__isCountable                        = ($value instanceOf COUNTABLE);
  56.                         $this->__isIterator                     = ($value instanceOf ITERATOR);
  57.                         $this->__isArrayAccess                      = ($value instanceOf ARRAYACCESS);
  58.                     }
  59.         private     static  $__primitives                               = array('string' => TRUE, 'integer' => TRUE, 'boolean' => TRUE, 'float' => TRUE);
  60.         private         $__type                                 = '';
  61.         private         $__isArrayObject                            = NULL;
  62.         private         $__isArrayAccess                            = NULL;
  63.         private         $__isPrimitive                              = NULL;
  64.         private         $__isCountable                              = NULL;
  65.         private         $__isIterator                               = NULL;
  66.         private         $__isLocked                                 = FALSE;
  67.         private         $__locktrace                                = NULL;
  68. }
  69.  
  70. interface Object_Interface
  71. {
  72.         public          function restore();
  73.         public          function apply();
  74. }
  75.  
  76. abstract class  SystemObject
  77. extends     System
  78. {
  79.         const           Blank                                   = NULL;
  80.     final   public          function isArray()                          { return new Boolean($this->internalTypeIsArray()); }
  81.     final   public          function isIterator()                           { return new Boolean($this->internalTypeIsIterator()); }
  82.     final   public          function isCountable()                          { return new Boolean($this->internalTypeIsCountable()); }
  83.     final   public          function isPrimitive()                          { return new Boolean($this->internalTypeIsPrimitive()); }
  84.     final   public          function isModifiable()                         { return new Boolean($this->internalTypeIsModifiable()); }
  85.     final   public          function getType()                          { return new String($this->internalTypeGetType()); }
  86.     final   public          function lock()                             { $this->internalType()->lock(); return $this; }
  87.     final   public          function lockWithTrace()                        { $this->internalType()->lockWithTrace(); return $this; }
  88.     final   public          function toString()                         { return var_export($this)."\n"; }
  89.         public          function __construct()                          { parent::__construct(); $this->__type = new Type($this); }
  90.     final   private         function internalType()                         { return $this->__type; }
  91.         private         $__type                                 = NULL;
  92.     final   protected       function internalTypeIsArray()                      { return $this->internalType()->isArray(); }
  93.     final   protected       function internalTypeIsIterator()                   { return $this->internalType()->isIterator(); }
  94.     final   protected       function internalTypeIsCountable()                  { return $this->internalType()->isCountable(); }
  95.     final   protected       function internalTypeIsPrimitive()                  { return $this->internalType()->isPrimitive(); }
  96.     final   protected       function internalTypeIsModifiable()                 { return $this->internalType()->isModifiable(); }
  97.     final   protected       function internalTypeGetType()                      { return $this->internalType()->getType(); }
  98.     final   protected       function _equals(SystemObject $object)                  { return new Boolean($this === $object); }
  99. }
  100.  
  101. abstract class Object extends SystemObject implements Object_Interface
  102. {
  103.         const           Blank                                   = NULL;
  104.     final   public          function copy()                             { $copy = $this->getTemp(); $copy->set($this->__buffer); return $copy; }
  105.     final   public          function duplicate()                            { $copy = $this->getTemp(); $copy->set($this->__buffer)->setCopy($this->__copy); return $copy; }
  106.     final   public          function get()                              { return $this->__copy; }
  107.     final   public          function restore()                          { $this->setCopy($this->__buffer); return $this; }
  108.     final   public          function registerMember(Hash $hash)                 { $this->__usedBy[] = $hash->getHashCode()->get(); }
  109.         public          function set($value)                            { try{ if(FALSE === $this->internalTypeIsModifiable()) return $this; $this->__buffer = $value; return $this->setCopy($this->__buffer); } catch(UnauthorizedAccessException $e){throw new ObjectHandlerWriteException(ObjectHandlerWriteException::ERROR_CAN_NOT_WRITE, $e);} }
  110.     final   public          function apply()                            { try{ if($this->internalTypeIsModifiable()) return $this->set($this->__copy); } catch(UnauthorizedAccessException $e){throw new ObjectHandlerWriteException(ObjectHandlerWriteException::ERROR_CAN_NOT_WRITE, $e);} }
  111.         public          function __construct($value)                        { parent::__construct(); $this->set($value); }
  112.         private         $__copy                                 = self::Blank;
  113.         private         $__buffer                               = self::Blank;
  114.         private         $__usedBy                               = array();
  115.     final   protected       function getTemp()                          { $c = get_class($this); return new $c($this->get()); }
  116.     final   protected       function setCopy($value)                        { $this->__copy = $value; return $this; }
  117.     final   protected       function _compareTo(Object $object)                 { return new Boolean($this->get() === $object->get()); }
  118. }
  119.  
  120. interface String_Interface
  121. {
  122.         public          function toUpper();
  123.         public          function toLower();
  124.         public          function length();
  125.         public          function compareTo(String $string, Boolean $casesensitive = NULL);
  126.         public          function trim(String $replace = NULL);
  127.         public          function trimStart(String $replace = NULL);
  128.         public          function trimEnd(String $replace = NULL);
  129.         public          function joinLeft(String $string, String $separator = NULL);
  130.         public          function joinRight(String $string, String $separator = NULL);
  131.         public          function insert(Integer $start, String $string);
  132.         public          function padLeft(Integer $chars, String $string = NULL);
  133.         public          function padRight(Integer $chars, String $string = NULL);
  134.         public          function contains(String $string, Boolean $casesensitive = NULL);
  135.         public          function endsWith(String $string, Boolean $casesensitive = NULL);
  136.         public          function startsWith(String $string, Boolean $casesensitive = NULL);
  137.         public          function indexOf(String $string, Boolean $casesensitive = NULL);
  138.         public          function lastIndexOf(String $string, Boolean $casesensitive = NULL);
  139.         public          function replace(String $search, String $replace = NULL);
  140.         public          function split(String $string = NULL);
  141.         public          function remove(Integer $start, Integer $length = NULL);
  142. }
  143.  
  144. class String extends Object implements String_Interface
  145. {
  146.         const           Blank = '';
  147.         public          function equals(String $string)                     { return parent::_equals($string); }
  148.         public          function isUtf8()                           { $value = $this->get(); return new Boolean((bool) (utf8_encode(utf8_decode($value)) == $value)); }
  149.         public          function isEmpty()                          { $value = $this->get(); return new Boolean((bool) empty($value)); }
  150.         public          function isWhitespace()                         { $value = trim($this->get(), ' '); return new Boolean((bool) empty($value)); }
  151.         public          function length()                           { return new Integer(strlen($this->get())); }
  152.         public          function toUpper()                          { return $this->setCopy(strtoupper($this->get())); }
  153.         public          function toLower()                          { return $this->setCopy(strtolower($this->get())); }
  154.         public          function toBoolean()                            { return new Boolean((bool) $this->get()); }
  155.         public          function set($value)
  156.                     {
  157.                         if(NULL === $value)     throw new ArgumentNullException;
  158.                         if(!is_string($value))  throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_STRING);
  159.                         parent::set((string) $value);
  160.                         return $this;
  161.                     }
  162.         public          function compareTo(String $string, Boolean $casesensitive = NULL)
  163.                     {
  164.                         return new Boolean(($casesensitive === NULL || $casesensitive->get() === FALSE)
  165.                             ? ($this->get() === $string->get())
  166.                             : ($this->getTemp()->toLower()->get() === $string->getTemp()->toLower()->get()));
  167.                     }
  168.         public          function trim(String $replace = NULL)
  169.                     {
  170.                         return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
  171.                             ? trim($this->get())
  172.                             : trim($this->get(), $replace->get()));
  173.                     }
  174.         public          function trimStart(String $replace = NULL)
  175.                     {
  176.                         return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
  177.                             ? ltrim($this->get())
  178.                             : ltrim($this->get(), $replace->get()));
  179.                     }
  180.         public          function trimEnd(String $replace = NULL)
  181.                     {
  182.                         return $this->setCopy(($replace === NULL || TRUE === $replace->isEmpty())
  183.                             ? rtrim($this->get())
  184.                             : rtrim($this->get(), $replace->get()));
  185.                     }
  186.         public          function join(String $string)
  187.                     {
  188.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  189.                         return $this->setCopy(implode($string->get(), preg_split('/(?<!^)(?!$)/u', $this->get())));
  190.                     }
  191.         public          function joinLeft(String $string, String $separator = NULL)
  192.                     {
  193.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  194.                         return $this->setCopy(($separator === NULL)
  195.                             ? ($string->get().$this->get())
  196.                             : ($string->get().$separator->get().$this->get()));
  197.                     }
  198.         public          function joinRight(String $string, String $separator = NULL)
  199.                     {
  200.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  201.                         return $this->setCopy(($separator === NULL)
  202.                             ? ($this->get().$string->get())
  203.                             : ($this->get().$separator->get().$string->get()));
  204.                     }
  205.         public          function insert(Integer $start, String $string)
  206.                     {
  207.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  208.                        
  209.                         try{if($start->get() < 0) throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);}
  210.                         catch(ArgumentOutOfRangeException $e){ throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);}
  211.                        
  212.                         $value = $this->get();
  213.                         return $this->setCopy(substr($value, 0, $start->get() ).$string->get().substr($value, $start->get(), $this->length()->get()));
  214.                     }
  215.         /**
  216.          * @todo str_pad is fuzzy. rewrite this
  217.         */
  218.         public          function padLeft(Integer $chars, String $string = NULL)
  219.                     {
  220.                         try{if($chars->get() < 0) throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);}
  221.                         catch(ArgumentOutOfRangeException $e){ throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);}
  222.                        
  223.                         return $this->setCopy(($string === NULL || $string->isEmpty()->get() === TRUE)
  224.                             ? (str_pad($this->get(), $chars->get(), ' ', STR_PAD_LEFT))
  225.                             : (str_pad($this->get(), $chars->get(), $string->get(), STR_PAD_LEFT)));
  226.                     }
  227.         /**
  228.          * @todo str_pad is fuzzy. rewrite this
  229.         */
  230.         public          function padRight(Integer $chars, String $string = NULL)
  231.                     {
  232.                         try{if($chars->get() < 0) throw new ArgumentOutOfRangeException(ArgumentOutOfRangeException::ERROR_OUT_OF_STRING_INDEX);}
  233.                         catch(ArgumentOutOfRangeException $e){ throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_NOT_POSITIVE_INTEGER_OR_ZERO, $e);}
  234.                        
  235.                         return $this->setCopy(($string === NULL || $string->isEmpty()->get() === TRUE)
  236.                             ? (str_pad($this->get(), $chars->get(), ' ', STR_PAD_RIGHT))
  237.                             : (str_pad($this->get(), $chars->get(), $string->get(), STR_PAD_RIGHT)));
  238.                     }
  239.         public          function contains(String $string, Boolean $casesensitive = NULL)
  240.                     {
  241.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  242.                         return new Boolean((bool)(($casesensitive === NULL || $casesensitive->get() === FALSE)
  243.                             ? (preg_match('#'.$string->get().'#msie', $this->get()))
  244.                             : (preg_match('#'.$string->get().'#mse', $this->get()))));
  245.                     }
  246.         public          function endsWith(String $string, Boolean $casesensitive = NULL)
  247.                     {
  248.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  249.                         return new Boolean((bool)(($casesensitive === NULL || $casesensitive->get() === FALSE)
  250.                             ? ($string->get() == substr($this->get(), $this->length()->get() - $string->length()->get() ))
  251.                             : ($string->getTemp()->toLower()->get() == substr($this->getTemp()->toLower()->get(), $this->length()->get() - $string->length()->get() ))));
  252.                     }
  253.         public          function startsWith(String $string, Boolean $casesensitive = NULL)
  254.                     {
  255.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  256.                         return new Boolean((bool)(($casesensitive === NULL || $casesensitive->get() === FALSE)
  257.                             ? ($string->get() == substr($this->get(), 0, $string->length()->get()))
  258.                             : ($string->getTemp()->toLower()->get() == substr($this->getTemp()->toLower()->get(), 0, $string->length()->get()))));
  259.                     }
  260.         /**
  261.          * @todo returns IntegerArray. how to map the results?
  262.         */
  263.         public          function indexOf(String $string, Boolean $casesensitive = NULL)
  264.                     {
  265.                         throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED);
  266.                     }
  267.         public          function lastIndexOf(String $string, Boolean $casesensitive = NULL)
  268.                     {
  269.                         if(TRUE === $string->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  270.                        
  271.                         $result = ($casesensitive === NULL || $casesensitive->get() === FALSE)
  272.                             ? (strrpos($this->get(), $string->get()))
  273.                             : (strrpos($this->getTemp()->toLower()->get(), $string->getTemp()->toLower()->get()));
  274.                        
  275.                         try{ return new Integer($result); }
  276.                         catch(DatatypeExpectedException $e){ return new Boolean($result); }
  277.                     }
  278.         public          function replace(String $search, String $replace = NULL)
  279.                     {
  280.                         if(TRUE === $search->isEmpty()->get()) throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  281.                         return $this->setCopy(($replace === NULL || $replace->isEmpty() === TRUE)
  282.                             ? (str_replace($search->get(), '', $this->get()))
  283.                             : (str_replace($search->get(), $replace->get(), $this->get())));
  284.                     }
  285.         /**
  286.          * @todo check split-pattern
  287.         */
  288.         public          function split(String $string = NULL)
  289.                     {
  290.                         return new StringArray(($string === NULL || $string->isEmpty()->get() === TRUE)
  291.                             ? (preg_split('/(?<!^)(?!$)/u', $this->get()))
  292.                             : (explode($string->get(), $this->get())));
  293.                     }
  294.         /**
  295.          * @todo returns IntegerArray. how to map the results?
  296.         */
  297.         public          function remove(Integer $start, Integer $length = NULL)
  298.                     {
  299.                         throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED);
  300.                     }
  301.         public          function __construct($value = self::Blank)
  302.                     {
  303.                         try{
  304.                             try{parent::__construct($value);}
  305.                             catch(DatatypeExpectedException $e){throw new DatatypeExpectedException($e->getCode());}
  306.                             catch(ArgumentNullException $e){throw new ArgumentNullException($e->getCode());}
  307.                         }catch(SystemException $e){throw new ObjectHandlerException(ObjectHandlerException::ERROR_CAN_NOT_CREATE, $e);}
  308.                        
  309.                     }
  310. }
  311.  
  312. interface Guid_Interface{}
  313. class Guid extends Object implements Guid_Interface
  314. {
  315.         public function set($value)
  316.         {
  317.             try
  318.             {
  319.                 $l = strlen($value);
  320.                
  321.                 if(NULL === $value) throw new ArgumentNullException(ArgumentNullException::ERROR_IS_NULL);
  322.                 if(!is_string($value))  throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_STRING);
  323.                 if(empty($value))   throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  324.                 if($l < 32)     throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_TO_SHORT);
  325.                 if($l > 32 && $l < 36)  throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_LENGTH);
  326.                 if($l > 36)     throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_TO_LONG);
  327.                 if($l === 36 && FALSE === (bool) preg_match("#^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$#", $value))
  328.                             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_NO_PREGMATCH);
  329.                 if(FALSE === (bool) preg_match("#^[a-f0-9\-]+$#", $value))
  330.                             throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_ILLEGAL_CHAR);
  331.                
  332.                 if($l === 36)       return $this->format36(new String($value));
  333.                             return $this->format32(new String($value));
  334.             }
  335.             catch(ArgumentNullException $e){ return $this->format32(new String(md5($this->getHashCode()->get()))); }
  336.             catch(SystemException $e){ throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_GUID, $e); }
  337.         }
  338.        
  339.         private function format32(String $value)
  340.         {
  341.             $int = new Integer; $cat = new String;
  342.             return $this->format36($value
  343.                         ->insert($int->set(8), $cat->set('-'))
  344.                         ->insert($int->set(13), $cat)
  345.                         ->insert($int->set(18), $cat)
  346.                         ->insert($int->set(23), $cat));
  347.         }
  348.        
  349.         private function format36(String $value)
  350.         {
  351.             return parent::set($value->get())->finalize()->lock();
  352.         }
  353.        
  354.         public function __construct($value = NULL)
  355.         {
  356.             try
  357.             {
  358.                 try{parent::__construct($value);}
  359.                 //catch(DatatypeExpectedException $e){throw new DatatypeExpectedException($e->getCode(), $e);}
  360.                 catch(ArgumentNullException $e){throw new ArgumentNullException($e->getCode());}
  361.             }catch(SystemException $e){throw new ObjectHandlerException(ObjectHandlerException::ERROR_CAN_NOT_CREATE, $e);}
  362.         }
  363. }
  364.  
  365. interface Integer_Interface{}
  366. class Integer extends Object implements Integer_Interface
  367. {
  368.         const Blank = 0;
  369.         public function set($value)
  370.         {
  371.             if(NULL === $value) throw new ArgumentNullException;
  372.             if(!is_integer($value)) throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_INTEGER);
  373.             parent::set($value);
  374.             return $this;
  375.         }
  376.         public function equals(Integer $object){return parent::_equals($object);}
  377.         public function compareTo(Integer $object){return parent::_compareTo($object);}
  378.         public function __construct($value = self::Blank){parent::__construct($value);}
  379. }
  380.  
  381. interface Boolean_Interface
  382. {
  383.     public function toggle();
  384. }
  385.  
  386. class Boolean extends Object implements Boolean_Interface
  387. {
  388.         const Blank = FALSE;
  389.         public          function set($value)
  390.                     {
  391.                         if(is_bool($value)) return parent::set($value);
  392.                         try{ if(TRUE === $this->parse(($value instanceOf String) ? $value : new String($value))) parent::set((bool) $value); return $this; }
  393.                         catch(ArgumentNullException $e) { throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_BOOLEAN, $e); }
  394.                         catch(FormatException $e)   { throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_BOOLEAN, $e); }
  395.                     }
  396.         public          function equals(Boolean $object)                    { return parent::_equals($object); }
  397.         public          function compareTo(Boolean $object)                 { return parent::_compareTo($object); }
  398.         public      static  function parse(String $value)
  399.                     {
  400.                         if(NULL === $value) throw new ArgumentNullException;
  401.                         $v = $value->toLower()->trim()->apply()->get();
  402.                         if(FALSE === ((bool) ($v == self::$falseString || $v == self::$trueString))) throw new FormatException(FormatException::ERR_CANNOT_PARSE);
  403.                         return TRUE;
  404.                     }  
  405.         public          function toggle()                           { $this->set(!$this->get()); return $this; }
  406.         public          function __construct($value = self::Blank)              { parent::__construct($value); }
  407.         private     static  $falseString                                = 'false';
  408.         private     static  $trueString                             = 'true';
  409. }
  410.  
  411. INTERFACE ObjectArray_Interface EXTENDS Object_Interface, COUNTABLE, ITERATOR, ARRAYACCESS{}
  412.  
  413. abstract class ObjectArray extends SystemObject implements ObjectArray_Interface
  414. {
  415.     // ARRAYACCESS
  416.     final   public          function offsetSet($offset, $value)                 { $this->__stack[$offset] = $value; }
  417.     final   public          function offsetExists($offset)                      { return isset($this->__stack[$offset]); }
  418.     final   public          function offsetUnset($offset)                       { unset($this->__stack[$offset]); }
  419.     final   public          function offsetGet($offset)                     { return isset($this->__stack[$offset]) ? $this->__stack[$offset] : null; }
  420.     // COUNTABLE
  421.     final   public          function count()                            { return sizeOf($this->__stack); }
  422.     // ITERATOR
  423.     final   public          function current()                          { return current($this->__stack); }
  424.     final   public          function key()                              { return key($this->__stack); }
  425.     final   public          function next()                             { next($this->__stack); $this->__index++; }
  426.     final   public          function rewind()                           { reset($this->__stack); $this->__index = 0; }
  427.     final   public          function valid()                            { return $this->__index < $this->count(); }
  428.    
  429.     // export
  430.         public          function toArray()                          { return $this->__stack; }
  431.    
  432.     // generic
  433.         public          function restore()                          { foreach($this as $key => $value) $value->restore(); return $this; }
  434.         public          function apply()                            { foreach($this as $key => $value) $value->apply(); return $this; }
  435.         public          function __construct()                          { parent::__construct(); }
  436.     final   protected       function _compareTo(Object $object)                 { throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED); }
  437.         protected       $__stack                                = array();
  438.         private         $__index                                = 0;
  439.        
  440.     // setter/getter   
  441.     final   protected       function _push(Object $value)                       { $this->__stack[] = $value; return $this; }
  442.     final   protected       function _update(Object $index, Object $value)              { if(!array_key_exists($index->get(), $this->__stack)) throw new ObjectArrayException(ObjectArrayException::ERR_NO_INDEX); $this->__stack[$index->get()] = $value; return $this; }
  443.     final   protected       function _get(Object $index)                        { if(!array_key_exists($index->get(), $this->__stack)) throw new ObjectArrayException(ObjectArrayException::ERR_NO_INDEX); return $this->__stack[$index->get()]; }
  444.         public          function exists(Object $object)                     { foreach($this as $key => $value) { $result = $value->equals($object); if($result->get() === TRUE) return $result; } }
  445. }
  446. INTERFACE StringArray_Interface EXTENDS String_Interface
  447. {
  448.         public          function update(Integer $index, String $value);
  449.         public          function get(Integer $index);
  450. }
  451. class StringArray extends ObjectArray implements StringArray_Interface
  452. {
  453.         public          function push(String $value)                        { return parent::_push($value); }
  454.         public          function get(Integer $index)                        { return parent::_get($index); }
  455.         public          function update(Integer $index, String $value)              { return parent::_update($index, $value); }
  456.         public          function toUpper()                          { foreach($this as $key => $value) $value->toUpper(); return $this; }
  457.         public          function toLower()                          { foreach($this as $key => $value) $value->toLower(); return $this; }
  458.         public          function trim(String $replace = NULL)                   { foreach($this as $key => $value) $value->trim($replace); return $this; }
  459.         public          function trimStart(String $replace = NULL)              { foreach($this as $key => $value) $value->trimStart(); return $this; }
  460.         public          function trimEnd(String $replace = NULL)                { foreach($this as $key => $value) $value->trimEnd(); return $this; }
  461.         public          function join(String $string)                       { foreach($this as $key => $value) $value->join($string); return $this; }
  462.         public          function joinLeft(String $string, String $separator = NULL)     { foreach($this as $key => $value) $value->joinLeft($string, $separator); return $this; }
  463.         public          function joinRight(String $string, String $separator = NULL)        { foreach($this as $key => $value) $value->joinRight($string, $separator); return $this; }
  464.         public          function insert(Integer $start, String $string)             { foreach($this as $key => $value) $value->insert($start, $string); return $this; }
  465.         public          function padLeft(Integer $chars, String $string = NULL)         { foreach($this as $key => $value) $value->padLeft($chars, $string); return $this; }
  466.         public          function padRight(Integer $chars, String $string = NULL)        { foreach($this as $key => $value) $value->padRight($chars, $string); return $this; }
  467.         public          function startsWith(String $string, Boolean $casesensitive = NULL)  { foreach($this as $key => $value) $value->startsWith($string, $casesensitive); return $this; }
  468.         public          function endsWith(String $string, Boolean $casesensitive = NULL)    { foreach($this as $key => $value) $value->endsWith($string, $casesensitive); return $this; }
  469.         public          function contains(String $string, Boolean $casesensitive = NULL)    { foreach($this as $key => $value) $value->contains($string, $casesensitive); return $this; }
  470.         public          function indexOf(String $string, Boolean $casesensitive = NULL)     { foreach($this as $key => $value) $value->indexOf($string, $casesensitive); return $this; }
  471.         public          function lastIndexOf(String $string, Boolean $casesensitive = NULL) { foreach($this as $key => $value) $value->lastIndexOf($string, $casesensitive); return $this; }
  472.         public          function replace(String $search, String $replace = NULL)        { foreach($this as $key => $value) $value->replace($search, $replace); return $this; }
  473.         public          function remove(Integer $start, Integer $length = NULL)         { foreach($this as $key => $value) $value->remove($search, $replace); return $this; }
  474.         public          function split(String $string = NULL)                   { throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED); }
  475.         public          function implode(String $string = NULL)                 { $l = array(); foreach($this as $key => $value) $l[] = $value->get(); return new String(($string === NULL || $string->isEmpty()->get() === TRUE) ? implode('', $l) : implode($string->get(), $l)); }
  476.         public          function length()                           { return new Integer($this->size()); }                 
  477.         public          function maxLength()                            { $l = array(); foreach($this as $key => $value) $l[] = $value->length()->get(); return new Integer(max($l)); }                    
  478.         public          function minLength()                            { $l = array(); foreach($this as $key => $value) $l[] = $value->length()->get(); return new Integer(min($l)); }                
  479.         public          function totalLength(Boolean $asArray = NULL)               { if($asArray === NULL || $asArray->get() === FALSE){ $c = 0; foreach($this as $key => $value) $c += $value->length()->get(); return new Integer($c); } $c = array(); foreach($this as $key => $value) $c[] = $value->length()->get(); return new IntegerArray($c); }
  480.         public          function findAllByMinLength(Integer $minLength)             { $l = array(); foreach($this as $key => $value) if($value->length()->get() >= $minLength->get()) $l[] = $value; return new StringArray($l);}
  481.         public          function findAllByMaxLength(Integer $maxLength)             { $l = array(); foreach($this as $key => $value) if($value->length()->get() <= $maxLength->get()) $l[] = $value; return new StringArray($l);}
  482.         public          function compareTo(String $string, Boolean $casesensitive = NULL)   { throw new ImplementationException(ImplementationException::ERROR_METHOD_NOT_IMPLEMENTED); }
  483.         public          function __construct(array $array)
  484.                     {
  485.                         parent::__construct();
  486.                         foreach($array as $string)
  487.                             try{$this->push(($string instanceOf String) ? $string : new String($string));}
  488.                             catch(ArgumentException $e){throw new ObjectArrayException(ObjectArrayException::ERR_NO_REGISTER, $e);}
  489.                     }
  490. }
  491.  
  492. INTERFACE IntegerArray_Interface EXTENDS ObjectArray_Interface, Integer_Interface
  493. {
  494.     public function push(Integer $value);
  495. }
  496.  
  497. class IntegerArray extends ObjectArray implements IntegerArray_Interface
  498. {
  499.     public function push(Integer $value){parent::_push($value);}
  500.     public function summary()
  501.     {
  502.         $summary = 0;
  503.         foreach($this as $key => $value) $summary += $value->get();
  504.         return new Integer($summary);
  505.     }
  506.     public function __construct(array $array){
  507.         foreach($array as $integer)
  508.             try{$this->push(($integer instanceOf Integer) ? $integer : new Integer($integer));}
  509.             catch(ArgumentException $e){throw new ObjectArrayException(ObjectArrayException::ERR_NO_REGISTER, $e);}}
  510. }
  511.  
  512.  
  513.  
  514.  
  515.  
  516. header('Content-Type: text/plain');
  517. require_once 'units.php';
  518.  
  519. $foo = new StringArray(array('foobar' => 'barfoo', 'foob', 'bar', 'foo'));
  520.  
  521. var_dump($foo[0]);
  522.  
  523. var_dump
  524. (
  525.     $foo
  526.     ->findAllByMaxLength(new Integer(3))
  527.     ->join(new String('---'))
  528.     ->joinLeft(new String("\n"))
  529.     ->implode(new String(';'))
  530.     ->joinLeft(new String("\n<<<EOF"))
  531.     ->joinRight(new String(";\nEOF;"))
  532.     ->get()
  533. );
  534. /*
  535. //$foo = new String('foobar'); var_dump($foo->join(new String('----'))->get()); // string(26) "f----o----o----b----a----r"
  536.  
  537. $foo = new StringArray(array('foobar', 'foob', 'bar', 'foo'));
  538. var_dump
  539. (
  540.     $foo
  541.     ->findByMaxLength(new Integer(3))
  542.     ->join(new String('---'))
  543.     ->joinLeft(new String("\n"))
  544.     ->implode(new String(';'))
  545.     ->joinLeft(new String("\n<<<EOF"))
  546.     ->joinRight(new String(";\nEOF;"))
  547.     ->get()
  548. );
  549. //var_dump($foo->join(new String('----'))->get()); // string(26) "f----o----o----b----a----r"
  550. die();
  551. */
  552. //StringErrorUnit::ConstructNoString();
  553. //StringErrorUnit::SetNoString();
  554. //StringErrorUnit::joinLeftEmpty();
  555. //StringErrorUnit::joinRightEmpty();
  556. //StringErrorUnit::InsertEmpty();
  557. //StringErrorUnit::InsertIndex();
  558. //StringErrorUnit::PadLeftCharLength();
  559. //StringErrorUnit::PadRightCharLength();
  560. //StringErrorUnit::ContainsEmpty();
  561. //StringErrorUnit::StartsWithEmpty();
  562. //StringErrorUnit::EndsWithEmpty();
  563. //StringErrorUnit::IndexofEmpty();
  564. //StringErrorUnit::LastIndexOfEmpty();
  565. //StringErrorUnit::ReplaceEmpty();
  566. //StringErrorUnit::TrimEmpty();
  567. //StringUnit::String();
  568. //StringUnit::trimStart();
  569. /*
  570. try
  571. {
  572.  
  573.     //for ($j=0, $max = 10000, $s = ''; $j<$max; $j++)
  574.         $guid = new Guid('1231212311231233211233212312345');
  575.         var_dump($guid);
  576. }
  577. catch(SystemException $e)
  578. {
  579.     print $e;
  580. }
  581.  
  582. */
  583. //print $foo->replace(new String('te'))->toString();
  584. //print $foo->split()->toString();
  585.  
  586. /*
  587. for($i = 0; $i <= 100; $i++)
  588. {
  589.     Unit::boolean();
  590.     Unit::string();
  591.     Unit::integer();
  592.     Unit::integerArray();
  593.     Unit::stringArray();
  594.     Unit::booleanParse();
  595.     unit::stringTest();
  596. }
  597. */
  598. /*
  599. $foo = new String('test');
  600. var_dump($foo);
  601. */
  602. /*
  603. for ($j=0, $max = 10000; $j<$max; $j++)
  604. {
  605.     $foo = new String('test');
  606.     $foo->toUpper()->PadLeft(new Integer($foo->length()->get() + 17))->joinRight(new String(':'))->insert(new Integer(5), new String('@'));
  607.     //unset($foo);
  608. }  
  609. /*
  610. var_dump($foo->getType());
  611. foreach($foo as $bar) var_dump($bar->get());
  612.  
  613. var_dump($foo);
  614. */
  615. //die('die:'.__FILE__.', Line:'.(__LINE__ + 46356));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement