Advertisement
fruffl

Instantiable ADT

Feb 19th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core\Std\Def;
  3.     USE ILLI\Core\Std\Def\__const_Type;
  4.     USE ILLI\Core\Std\Def\ADTInstance\ComponentMethodCallException;
  5.     USE ILLI\Core\Std\Exception\ArgumentExpectedException;
  6.     USE ILLI\Core\Std\Exception\ArgumentLengthZeroException;
  7.     USE ILLI\Core\Std\Exception\ClassOrInterfaceNotFoundException;
  8.     USE ILLI\Core\Util\Spl;
  9.    
  10.     FINAL CLASS ADTInstance EXTENDS \ILLI\Core\Std\Def\ADT
  11.     {
  12.         protected function createHashAddr(array $__defineTypes = [])
  13.         {
  14.             try
  15.             {
  16.                 $d = &$__defineTypes;
  17.                
  18.                 if([] === $d)
  19.                     throw new ArgumentLengthZeroException;
  20.                
  21.                 $d = array_unique($d);
  22.                 sort($d);
  23.                
  24.                 return Spl::nameWithHash(get_called_class(), implode('|', $d));
  25.             }
  26.             catch(\Exception $E)
  27.             {
  28.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__]);
  29.             }
  30.         }
  31.        
  32.         protected function parseDef(array $__def)
  33.         {
  34.             try
  35.             {
  36.                 $r = parent::parseDef($__def);
  37.                
  38.                 foreach($r as $k => &$v)
  39.                 {
  40.                     $v = ltrim($v, '\\');
  41.                    
  42.                     if(FALSE === class_exists($v)
  43.                     && FALSE === interface_exists($v))
  44.                         throw new ClassOrInterfaceNotFoundException(new ArgumentExpectedException([
  45.                             'target'    => $this->getName().'['.$k.']',
  46.                             'expected'  => 'class|interface',
  47.                             'detected'  => $t = getType($v),
  48.                             'value'     => is_object($v)
  49.                                         ? get_class($v)
  50.                                         : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
  51.                         ]), ['name' => $v]);
  52.                 }
  53.                
  54.                 if([] === $r)
  55.                     throw new ArgumentLengthZeroException;
  56.                    
  57.                 return $r;
  58.             }
  59.             catch(\Exception $E)
  60.             {
  61.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_PARSE_DEF, ['method' => __METHOD__]);
  62.             }
  63.         }
  64.        
  65.         public function validate($__value)
  66.         {
  67.             try
  68.             {
  69.                 $v = &$__value;
  70.                 $t = getType($v);
  71.                
  72.                 foreach($this->getGC() as $h)
  73.                 {
  74.                     if(__const_Type::SPL_OBJECT === $t
  75.                     && (class_exists($h) || interface_exists($h))
  76.                     && (is_subclass_of($v, $h) || $v instanceOf $h))
  77.                         return TRUE;
  78.                 }
  79.                
  80.                 return FALSE;
  81.             }
  82.             catch(\Exception $E)
  83.             {
  84.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_VALIDATE, ['method' => __METHOD__]);
  85.             }
  86.         }
  87.     }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. <?PHP
  95.     namespace root;
  96.         class baz {}
  97.         class bar {}
  98.         $d = (new ADV('root\bar'))->set(new bar);
  99.         var_dump($d);
  100.         $d = (new ADV('root\bar'))->set(new baz);
  101.         var_dump($d);
  102.  
  103. /*
  104. object(ILLI\Core\Std\Def\ADV)#6 (1) {
  105.   ["__data":protected]=>
  106.   object(root\bar)#10 (0) {
  107.   }
  108. }
  109. CHADDR  000000001371a8330000000009eeb411:000000001371a8300000000009eeb411
  110. ECADDR  501:500
  111. RVADDR 
  112.     OUTER   000000001371a8300000000009eeb411: [UNSOLVED]    <ILLI\Core\Std\Def\ADV\ComponentMethodCallException:501>
  113.                                 Method Error in ILLI\Core\Std\Def\ADV::set().
  114.     FIRED   000000001371a8330000000009eeb411: [UNSOLVED]    <ILLI\Core\Std\Exception\ArgumentExpectedException:500>
  115.                                 Expected type <root\bar>ILLI\Core\Std\Def\ADV: <object>root\baz given.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement