Advertisement
fruffl

Late-State Exception

Feb 22nd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.15 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core\Std\Def;
  3.     USE ILLI\Core\Std\Def;
  4.     USE ILLI\Core\Std\Def\ADV\ComponentInitializationException;
  5.     USE ILLI\Core\Std\Def\ADV\ComponentMethodCallException;
  6.     USE ILLI\Core\Std\Exception\ArgumentExpectedException;
  7.     USE ILLI\Core\Std\Exception\ClassNotFoundException;
  8.     USE ILLI\Core\Std\Invoke;
  9.     USE ILLI\Core\Std\Spl\Fsb;
  10.     USE ILLI\Core\Util\Spl;
  11.    
  12.     CLASS ADV
  13.     {
  14.         #:props:
  15.         /**
  16.          * cache ADT
  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.          * :ADT<ILLI\Core\Std\Def\ADT>
  23.          *  the ADT for the type-value-pair
  24.          *
  25.          * @var     array [{:hashAddr} => ILLI\Core\Std\Spl\Fsb[long offset => {:ADT}]]
  26.          */
  27.         private static  $__gc       = [];
  28.        
  29.         /**
  30.          * gc hash address
  31.          *
  32.          * @var     mixed
  33.          * @see     ::$__gc
  34.          */
  35.         protected   $__name     = NULL;
  36.        
  37.         /**
  38.          * stored value, type defined by ADT
  39.          *
  40.          * @var     mixed
  41.          * @see     ::$__gc
  42.          */
  43.         protected   $__data     = NULL;
  44.         #::
  45.        
  46.         /**
  47.          * Instantiate a new ADT-Value-Pair.
  48.          *
  49.          * The ADV stores data. The expected data-type is defined by ADT-definition(s).
  50.          *
  51.          * (An generic <long> forbids all except integers. Multi-type-def <string|array> forbids all
  52.          * except strings and arrays.)
  53.          *
  54.          * :offset<long>
  55.          *  zero-based index
  56.          *
  57.          * :gcType<string>
  58.          *  a valid __const_Type
  59.          *
  60.          * @param   string  $__defineType   {:gcType}
  61.          * @param   array   $__defineType   [{:offset} => {:gcType}]
  62.          * @throws  ILLI\Core\Std\Exception\ArgumentExpectedException when given spl-type is not of type string
  63.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentInitializationException
  64.          * @see     ILLI\Core\Std\Def\ADT::__construct()
  65.          */
  66.         public function __construct($__defineType)
  67.         {
  68.             try
  69.             {
  70.                 $d = (array) $__defineType;
  71.                 $n = $this->__name = $this->createHashAddr($d);
  72.                 $t = &self::$__gc[$n];
  73.                
  74.                 if(TRUE === isset($t))
  75.                     return;
  76.                    
  77.                 $t = Fsb::fromArray(Def::types($d), TRUE);
  78.             }
  79.             catch(\Exception $E)
  80.             {
  81.                 $c = get_called_class();
  82.                 $e = $c.'\ComponentInitializationException';
  83.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  84.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  85.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  86.                     ? new ComponentInitializationException($E, ['class' => get_called_class()])
  87.                     : new $e($E, ['class' => get_called_class()]);
  88.             }
  89.         }
  90.        
  91.         /**
  92.          * Destroy anonymous ATV definitions
  93.          *
  94.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_DTOR
  95.          * @void
  96.          */
  97.         public function __destruct()
  98.         {
  99.             try
  100.             {
  101.                 if(($c = get_called_class()) === __CLASS__)
  102.                     unset(self::$__gc[$this->getName()]);
  103.             }
  104.             catch(\Exception $E)
  105.             {
  106.                 $c = get_called_class();
  107.                 $e = $c.'\ComponentMethodCallException';
  108.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  109.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  110.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  111.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_DTOR, ['method' => __METHOD__])
  112.                     : new $e($E, $e::ERROR_DTOR, ['method' => __METHOD__]);
  113.             }
  114.         }
  115.        
  116.         /**
  117.          * value validation
  118.          *
  119.          * use ADT to validate the value
  120.          *
  121.          * @param   mixed $__value
  122.          * @return  boolean
  123.          * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
  124.          * @see     ILLI\Core\Std\Def\ADT::validate()
  125.          */
  126.         public function validate($__value)
  127.         {
  128.             try
  129.             {
  130.                 return $this->getGC()->evaluate('validate', [$__value]);
  131.             }
  132.             catch(\Exception $E)
  133.             {
  134.                 $c = get_called_class();
  135.                 $e = $c.'\ComponentMethodCallException';
  136.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  137.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  138.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  139.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_VALIDATE, ['method' => __METHOD__])
  140.                     : new $e($E, $e::ERROR_VALIDATE, ['method' => __METHOD__]);
  141.             }
  142.         }
  143.        
  144.         /**
  145.          * store data when type equals with ADT
  146.          *
  147.          * @param   mixed $__value
  148.          * @return  this
  149.          * @throws  ILLI\Core\Std\Exception\ArgumentExpectedException on validation-fail
  150.          * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
  151.          * @see     ::validate()
  152.          */
  153.         public function set($__value)
  154.         {
  155.             $c = get_called_class();
  156.             $e = $c.'\ComponentMethodCallException';
  157.                
  158.             try
  159.             {
  160.                 if(TRUE === $this->validate($__value))
  161.                 {
  162.                     $this->__data = $__value;
  163.                     return $this;
  164.                 }
  165.                
  166.                 $E = new ArgumentExpectedException
  167.                 ([
  168.                     'target'    => get_called_class(),
  169.                     'expected'  => implode('|', array_unique($this->getGC()->invoke('toString'))),
  170.                     'detected'  => $t = getType($v = $__value),
  171.                     'value'     => is_object($v) ? get_class($v) : (is_scalar($v) ? $v : NULL)
  172.                 ]);
  173.                
  174.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  175.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  176.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  177.                     ? new ComponentMethodCallException($E, ['method' => __METHOD__], ComponentMethodCallException::ERROR_SET_VALUE_TYPE_EXPECTED)
  178.                     : new $e($E, ['method' => __METHOD__], $e::ERROR_SET_VALUE_TYPE_EXPECTED);
  179.                
  180.             }
  181.             catch(ComponentMethodCallException $E)
  182.             {
  183.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  184.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  185.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  186.                     ? new ComponentMethodCallException($E->getPrevious(), ['method' => __METHOD__])
  187.                     : new $e($E->getPrevious(), ['method' => __METHOD__]);
  188.             }
  189.             catch(\Exception $E)
  190.             {
  191.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  192.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  193.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  194.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_SET, ['method' => __METHOD__])
  195.                     : new $e($E, $e::ERROR_SET, ['method' => __METHOD__]);
  196.             }
  197.         }
  198.        
  199.         /**
  200.          * get the stored data or NULL when nothing was set
  201.          *
  202.          * @return  mixed|NULL
  203.          * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
  204.          */
  205.         public function get()
  206.         {
  207.            
  208.             try
  209.             {
  210.                 return $this->__data;
  211.             }
  212.             catch(\Exception $E)
  213.             {
  214.                 $c = get_called_class();
  215.                 $e = $c.'\ComponentMethodCallException';
  216.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  217.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  218.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  219.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET, ['method' => __METHOD__])
  220.                     : new $e($E, $e::ERROR_GET, ['method' => __METHOD__]);
  221.             }
  222.         }
  223.        
  224.         /**
  225.          * get ADT definition
  226.          *
  227.          * @return  ILLI\Core\Std\Spl\Fsb
  228.          * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
  229.          */
  230.         public function getGC()
  231.         {
  232.             try
  233.             {
  234.                 return self::$__gc[$this->getName()];
  235.             }
  236.             catch(\Exception $E)
  237.             {
  238.                 $c = get_called_class();
  239.                 $e = $c.'\ComponentMethodCallException';
  240.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  241.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  242.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  243.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET_GC, ['method' => __METHOD__])
  244.                     : new $e($E, $e::ERROR_GET_GC, ['method' => __METHOD__]);
  245.             }
  246.         }
  247.        
  248.         /**
  249.          * get gc hash address
  250.          *
  251.          * @return  string
  252.          * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_GET_NAME
  253.          * @see     ::$__gc
  254.          */
  255.         public function getName()
  256.         {
  257.             try
  258.             {
  259.                 return $this->__name;
  260.             }
  261.             catch(\Exception $E)
  262.             {
  263.                 $c = get_called_class();
  264.                 $e = $c.'\ComponentMethodCallException';
  265.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  266.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  267.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  268.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET_NAME, ['method' => __METHOD__])
  269.                     : new $e($E, $e::ERROR_GET_NAME, ['method' => __METHOD__]);
  270.             }
  271.         }
  272.        
  273.         protected function createHashAddr(array $__defineTypes = [])
  274.         {
  275.             try
  276.             {
  277.                 #~ performanced ADT: cache request by called-class; otherwise by hash
  278.                 return ($c = get_called_class()) === __CLASS__ ? Spl::nameWithHash($c, $this) : $c;
  279.             }
  280.             catch(\Exception $E)
  281.             {
  282.                 $c = get_called_class();
  283.                 $e = $c.'\ComponentMethodCallException';
  284.                 #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
  285.                 #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
  286.                 throw ($c === __CLASS__ || FALSE === class_exists($e))
  287.                     ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__])
  288.                     : new $e($E, $e::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__]);
  289.             }
  290.         }
  291.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement