Advertisement
fruffl

DataType-Value-Pair

Feb 19th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core\Std\Def;
  3.     USE ILLI\Core\Std\Exception\ArgumentExpectedException;
  4.     USE ILLI\Core\Std\Def;
  5.    
  6.     CLASS ADV
  7.     {
  8.         private static  $__gc       = [];
  9.         protected   $__value    = NULL;
  10.        
  11.         public function __construct($__splType)
  12.         {
  13.             $c = get_called_class();
  14.             $s = &self::$__gc[$c];
  15.            
  16.             isset($s) ?: $s = Def::type($__splType);
  17.         }
  18.        
  19.         public function validate($__value)
  20.         {
  21.             return $this->getADT()->validate($__value);
  22.         }
  23.        
  24.         public function set($__value)
  25.         {
  26.             if(TRUE === $this->validate($__value))
  27.             {
  28.                 $this->__data = $__value;
  29.                 return $this;
  30.             }
  31.            
  32.             throw new ArgumentExpectedException([
  33.                 'target'    => get_called_class(),
  34.                 'expected'  => $this->getADT()->__toString(),
  35.                 'detected'  => $t = getType($v = $__value),
  36.                 'value'     => is_object($v)
  37.                             ? get_class($v)
  38.                             : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
  39.             ]);
  40.         }
  41.        
  42.         public function get()
  43.         {
  44.             return $this->__data;
  45.         }
  46.        
  47.         public function getADT()
  48.         {
  49.             return self::$__gc[get_called_class()];
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement