fruffl icon

Late-State Exception

fruffl | 02/22/13 07:40:22 PM UTC | 0 ⭐ | 265 👁️ | Never ⏰ | []
PHP |

10.15 KB

|

None

|

0 👍

/

0 👎

<?PHP
    NAMESPACE ILLI\Core\Std\Def;
    USE ILLI\Core\Std\Def;
    USE ILLI\Core\Std\Def\ADV\ComponentInitializationException;
    USE ILLI\Core\Std\Def\ADV\ComponentMethodCallException;
    USE ILLI\Core\Std\Exception\ArgumentExpectedException;
    USE ILLI\Core\Std\Exception\ClassNotFoundException;
    USE ILLI\Core\Std\Invoke;
    USE ILLI\Core\Std\Spl\Fsb;
    USE ILLI\Core\Util\Spl;
    
    CLASS ADV
    {
        #:props:
        /**
         * cache ADT
         *
         * :hashAddr<string>
         *  instance of get_called_class(): ILLI\Core\Std\Def\ADT*
         *  instance of __CLASS__:      ILLI\Core\Std\Def\ADT<hash>
         *
         * :ADT<ILLI\Core\Std\Def\ADT>
         *  the ADT for the type-value-pair
         *
         * @var     array [{:hashAddr} => ILLI\Core\Std\Spl\Fsb[long offset => {:ADT}]]
         */
        private static  $__gc       = [];
        
        /**
         * gc hash address
         *
         * @var     mixed
         * @see     ::$__gc
         */
        protected   $__name     = NULL;
        
        /**
         * stored value, type defined by ADT
         *
         * @var     mixed
         * @see     ::$__gc
         */
        protected   $__data     = NULL;
        #::
        
        /**
         * Instantiate a new ADT-Value-Pair.
         * 
         * The ADV stores data. The expected data-type is defined by ADT-definition(s).
         *
         * (An generic <long> forbids all except integers. Multi-type-def <string|array> forbids all
         * except strings and arrays.)
         *
         * :offset<long>
         *  zero-based index
         *
         * :gcType<string>
         *  a valid __const_Type
         *
         * @param   string  $__defineType   {:gcType}
         * @param   array   $__defineType   [{:offset} => {:gcType}]
         * @throws  ILLI\Core\Std\Exception\ArgumentExpectedException when given spl-type is not of type string
         * @catchable   ILLI\Core\Std\Def\ADT\ComponentInitializationException
         * @see     ILLI\Core\Std\Def\ADT::__construct()
         */
        public function __construct($__defineType)
        {
            try
            {
                $d = (array) $__defineType;
                $n = $this->__name = $this->createHashAddr($d);
                $t = &self::$__gc[$n];
                
                if(TRUE === isset($t))
                    return;
                    
                $t = Fsb::fromArray(Def::types($d), TRUE);
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentInitializationException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentInitializationException($E, ['class' => get_called_class()])
                    : new $e($E, ['class' => get_called_class()]);
            }
        }
        
        /**
         * Destroy anonymous ATV definitions
         *
         * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_DTOR
         * @void
         */
        public function __destruct()
        {
            try
            {
                if(($c = get_called_class()) === __CLASS__)
                    unset(self::$__gc[$this->getName()]);
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentMethodCallException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_DTOR, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_DTOR, ['method' => __METHOD__]);
            }
        }
        
        /**
         * value validation
         *
         * use ADT to validate the value
         *
         * @param   mixed $__value
         * @return  boolean
         * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
         * @see     ILLI\Core\Std\Def\ADT::validate()
         */
        public function validate($__value)
        {
            try
            {
                return $this->getGC()->evaluate('validate', [$__value]);
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentMethodCallException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_VALIDATE, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_VALIDATE, ['method' => __METHOD__]);
            }
        }
        
        /**
         * store data when type equals with ADT
         *
         * @param   mixed $__value
         * @return  this
         * @throws  ILLI\Core\Std\Exception\ArgumentExpectedException on validation-fail
         * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
         * @see     ::validate()
         */
        public function set($__value)
        {
            $c = get_called_class();
            $e = $c.'\ComponentMethodCallException';
                
            try
            {
                if(TRUE === $this->validate($__value))
                {
                    $this->__data = $__value;
                    return $this;
                }
                
                $E = new ArgumentExpectedException
                ([
                    'target'    => get_called_class(),
                    'expected'  => implode('|', array_unique($this->getGC()->invoke('toString'))),
                    'detected'  => $t = getType($v = $__value),
                    'value'     => is_object($v) ? get_class($v) : (is_scalar($v) ? $v : NULL)
                ]);
                
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ['method' => __METHOD__], ComponentMethodCallException::ERROR_SET_VALUE_TYPE_EXPECTED)
                    : new $e($E, ['method' => __METHOD__], $e::ERROR_SET_VALUE_TYPE_EXPECTED);
                
            }
            catch(ComponentMethodCallException $E)
            {
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E->getPrevious(), ['method' => __METHOD__])
                    : new $e($E->getPrevious(), ['method' => __METHOD__]);
            }
            catch(\Exception $E)
            {
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_SET, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_SET, ['method' => __METHOD__]);
            }
        }
        
        /**
         * get the stored data or NULL when nothing was set
         *
         * @return  mixed|NULL
         * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
         */
        public function get()
        {
            
            try
            {
                return $this->__data;
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentMethodCallException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_GET, ['method' => __METHOD__]);
            }
        }
        
        /**
         * get ADT definition
         *
         * @return  ILLI\Core\Std\Spl\Fsb
         * @catchable   ILLI\Core\Std\Exception\ADV\ComponentMethodCallException
         */
        public function getGC()
        {
            try
            {
                return self::$__gc[$this->getName()];
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentMethodCallException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET_GC, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_GET_GC, ['method' => __METHOD__]);
            }
        }
        
        /**
         * get gc hash address
         *
         * @return  string
         * @catchable   ILLI\Core\Std\Def\ADT\ComponentMethodCallException ComponentMethodCallException::ERROR_GET_NAME
         * @see     ::$__gc
         */
        public function getName()
        {
            try
            {
                return $this->__name;
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentMethodCallException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_GET_NAME, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_GET_NAME, ['method' => __METHOD__]);
            }
        }
        
        protected function createHashAddr(array $__defineTypes = [])
        {
            try
            {
                #~ performanced ADT: cache request by called-class; otherwise by hash
                return ($c = get_called_class()) === __CLASS__ ? Spl::nameWithHash($c, $this) : $c;
            }
            catch(\Exception $E)
            {
                $c = get_called_class();
                $e = $c.'\ComponentMethodCallException';
                #! context-based exception: from namespace ILLI\Core\Std\Def\ADV\ to namspace ILLI\Core\Std\Def\ADV*\:
                #! wrap anonymous ADT exception into namespace of defined ADT exception without re-implementation of this method in child-class
                throw ($c === __CLASS__ || FALSE === class_exists($e))
                    ? new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__])
                    : new $e($E, $e::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__]);
            }
        }
    }

Comments