Advertisement
fruffl

Catchable/Throwable

Feb 20th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.05 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\ADT\ComponentInitializationException;
  5.     USE ILLI\Core\Std\Def\ADT\ComponentMethodCallException;
  6.     USE ILLI\Core\Std\Exception\ArgumentExpectedException;
  7.     USE ILLI\Core\Std\Exception\ArgumentLengthZeroException;
  8.     USE ILLI\Core\Std\Exception\ArgumentInUseException;
  9.     USE ILLI\Core\Std\Exception\NotSupportedException;
  10.     USE ILLI\Core\Std\Spl\Fsb;
  11.     USE ILLI\Core\Util\Spl;
  12.    
  13.     CLASS ADT EXTENDS \ILLI\Core\Std\Spl\Fsb
  14.     {
  15.         /**
  16.          * cache typedef request
  17.          *
  18.          * :hashAddr<string>
  19.          *  instance of get_called_class(): ILLI\Core\Std\Def\ADT*
  20.          *  instance of __CLASS__:      ILLI\Core\Std\Def\ADT<hash>
  21.          *
  22.          * :adtDef<ILLI\Core\Std\Spl\Fsb>
  23.          *  collection of defined __const_Type(s)
  24.          *
  25.          * @var     array [{:hashAddr} => {:adtDef}]
  26.          */
  27.         private static $__gc = [];
  28.        
  29.         /**
  30.          * Instantiate a new Abstract Data Type.
  31.          *
  32.          * define ADT
  33.          *
  34.          * :offset<long>
  35.          *  zero-based index
  36.          *
  37.          * :gcType<string>
  38.          *  a valid __const_Type
  39.          *
  40.          * @param   array $__defineTypes [{:offset} => {:gcType}]
  41.          * @throws  ILLI\Core\Std\Exception\ArgumentLengthZeroException when definition is an empty array
  42.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentInitializationException
  43.          * @see     ILLI\Core\Std\Def\__const_Type
  44.          * @see     ILLI\Core\Std\Def\ADTInstance
  45.          * @see     ILLI\Core\Std\Spl\Fsb
  46.          */
  47.         public function __construct(array $__defineTypes = [])
  48.         {
  49.             try
  50.             {
  51.                 if([] === $__defineTypes)
  52.                     throw new ComponentMethodCallException
  53.                     (
  54.                         ComponentMethodCallException::CTOR_DEFINITION_LENGTH_ZERO,
  55.                         ['method' => __METHOD__],
  56.                         new ArgumentLengthZeroException
  57.                     );
  58.                
  59.                 $n = $this->createHashAddr($__defineTypes);
  60.                 $t = &self::$__gc[$n];     
  61.                
  62.                 parent::setSize(1);
  63.                 parent::offsetSet(0, $n);
  64.                
  65.                 if(FALSE === isset($t))
  66.                     $t = Fsb::fromArray($this->parseDef($__defineTypes));
  67.             }
  68.             catch(\Exception $E)
  69.             {
  70.                 throw new ComponentInitializationException($E, ['class' => get_called_class()]);
  71.             }
  72.         }
  73.        
  74.         /**
  75.          * value validation
  76.          *
  77.          * The type of given value equals with one of the registered gc-types
  78.          *
  79.          * @param   mixed $__value
  80.          * @return  boolean
  81.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_VALIDATE
  82.          * @see     ILLI\Core\Std\Def\__const_Type
  83.          */
  84.         public function validate($__value)
  85.         {
  86.             try
  87.             {
  88.                 $v = &$__value;
  89.                 $t = getType($v);
  90.                
  91.                 foreach($this->getGC() as $h)
  92.                 {
  93.                     if($t === $h
  94.                     ||($t === __const_Type::SPL_OBJECT && (class_exists($h) || interface_exists($h)) && (is_subclass_of($v, $h) || $v instanceOf $h)))
  95.                         return TRUE;
  96.                 }
  97.                
  98.                 return FALSE;
  99.             }
  100.             catch(\Exception $E)
  101.             {
  102.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_VALIDATE, ['method' => __METHOD__]);
  103.             }
  104.         }
  105.        
  106.         /**
  107.          * get ADT-gc hash address
  108.          *
  109.          * @return  string
  110.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_GET_NAME
  111.          * @see     ::$__gc
  112.          */
  113.         public function getName()
  114.         {
  115.             try
  116.             {
  117.                 return parent::offsetGet(0);
  118.             }
  119.             catch(\Exception $E)
  120.             {
  121.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET_NAME, ['method' => __METHOD__]);
  122.             }
  123.         }
  124.        
  125.         /**
  126.          * get ADT-gc
  127.          *
  128.          * @return  ILLI\Core\Std\Spl\Fsb
  129.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_GET_GC
  130.          * @see     ::$__gc
  131.          */
  132.         public function getGC()
  133.         {
  134.             try
  135.             {
  136.                 return self::$__gc[$this->getName()];
  137.             }
  138.             catch(\Exception $E)
  139.             {
  140.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET_GC, ['method' => __METHOD__]);
  141.             }
  142.         }
  143.        
  144.         /**
  145.          * join registered gc-type(s)
  146.          *
  147.          * @return  ILLI\Core\Std\Spl\Fsb
  148.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_TO_STRING
  149.          * @see     ::$__gc
  150.          * @see     ILLI\Core\Std\Def\__const_Type
  151.          */
  152.         public function toString()
  153.         {
  154.             try
  155.             {
  156.                 return implode('|', $this->getGC()->toArray());
  157.             }
  158.             catch(\Exception $E)
  159.             {
  160.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_TO_STRING, ['method' => __METHOD__]);
  161.             }
  162.         }
  163.        
  164.         /**
  165.          * generate gc-hash-address from called-class or from spl_object_hash
  166.          *
  167.          * :offset<long>
  168.          *  zero-based index
  169.          *
  170.          * :gcType<string>
  171.          *  a valid __const_Type
  172.          *
  173.          * @param   array $__defineTypes [{:offset} => {:gcType}]
  174.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_CREATE_HASH_ADDR
  175.          * @return  string
  176.          * @see     ::$__gc
  177.          */
  178.         protected function createHashAddr(array $__defineTypes = [])
  179.         {
  180.             try
  181.             {
  182.                 #~ performanced ADT: cache request by called-class; otherwise by hash
  183.                 return ($c = get_called_class()) === __CLASS__ ? Spl::nameWithHash($c, $this) : $c;
  184.             }
  185.             catch(\Exception $E)
  186.             {
  187.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__]);
  188.             }
  189.         }
  190.        
  191.         /**
  192.          * validate typedef request
  193.          *
  194.          * :offset<long>
  195.          *  zero-based index
  196.          *
  197.          * :gcType<string>
  198.          *  a valid __const_Type
  199.          *
  200.          * @param   array $__defineTypes [{:offset} => {:gcType}]
  201.          * @throws  ILLI\Core\Std\Exception\ArgumentExpectedException when {:gcType} is not of type string
  202.          * @throws  ILLI\Core\Std\Exception\ArgumentLengthZeroException result-array is empty
  203.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_PARSE_DEF_EXPECTED_STRING
  204.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_PARSE_DEF_RESULT_LENGTH_ZERO
  205.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_PARSE_DEF
  206.          */
  207.         protected function parseDef(array $__defineTypes)
  208.         {
  209.             try
  210.             {
  211.                 $r = [];
  212.                
  213.                 foreach($__defineTypes as $k => $v)
  214.                 {
  215.                     if(FALSE === is_string($v))
  216.                         throw new ComponentMethodCallException
  217.                         (
  218.                             ComponentMethodCallException::ERROR_PARSE_DEF_EXPECTED_STRING,
  219.                             ['offset' => $k, 'class' => get_called_class()],
  220.                             new ArgumentExpectedException
  221.                             ([
  222.                                 'target'    => $this->getName().'['.$k.']',
  223.                                 'expected'  => 'string',
  224.                                 'detected'  => $t = getType($v),
  225.                                 'value'     => is_object($v) ? get_class($v) : (is_scalar($v) ? $v : NULL)
  226.                             ])
  227.                         );
  228.                        
  229.                     $r[$k] = $v;
  230.                 }
  231.                
  232.                 if([] === $r)
  233.                     throw new ComponentMethodCallException
  234.                     (
  235.                         ComponentMethodCallException::ERROR_PARSE_DEF_RESULT_LENGTH_ZERO,
  236.                         ['method' => __METHOD__],
  237.                         new ArgumentLengthZeroException
  238.                     );
  239.                
  240.                 return $r;
  241.             }
  242.             catch(ComponentMethodCallException $E)
  243.             {
  244.                 throw $E;
  245.             }
  246.             catch(\Exception $E)
  247.             {
  248.                 throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_PARSE_DEF, ['method' => __METHOD__]);
  249.             }
  250.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement