fruffl icon

Instantiable ADT

fruffl | 02/20/13 02:28:02 AM UTC | 0 ⭐ | 267 👁️ | Never ⏰ | []
PHP |

3.03 KB

|

None

|

0 👍

/

0 👎

<?PHP
    NAMESPACE ILLI\Core\Std\Def;
    USE ILLI\Core\Std\Def\__const_Type;
    USE ILLI\Core\Std\Def\ADTInstance\ComponentMethodCallException;
    USE ILLI\Core\Std\Exception\ArgumentExpectedException;
    USE ILLI\Core\Std\Exception\ArgumentLengthZeroException;
    USE ILLI\Core\Std\Exception\ClassOrInterfaceNotFoundException;
    USE ILLI\Core\Util\Spl;
    
    FINAL CLASS ADTInstance EXTENDS \ILLI\Core\Std\Def\ADT
    {
        protected function createHashAddr(array $__defineTypes = [])
        {
            try
            {
                $d = &$__defineTypes;
                
                if([] === $d)
                    throw new ArgumentLengthZeroException;
                
                $d = array_unique($d);
                sort($d);
                
                return Spl::nameWithHash(get_called_class(), implode('|', $d));
            }
            catch(\Exception $E)
            {
                throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_CREATE_HASH_ADDR, ['method' => __METHOD__]);
            }
        }
        
        protected function parseDef(array $__def)
        {
            try
            {
                $r = parent::parseDef($__def);
                
                foreach($r as $k => &$v)
                {
                    $v = ltrim($v, '\\');
                    
                    if(FALSE === class_exists($v)
                    && FALSE === interface_exists($v))
                        throw new ClassOrInterfaceNotFoundException(new ArgumentExpectedException([
                            'target'    => $this->getName().'['.$k.']',
                            'expected'  => 'class|interface',
                            'detected'  => $t = getType($v),
                            'value'     => is_object($v)
                                        ? get_class($v)
                                        : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
                        ]), ['name' => $v]);
                }
                
                if([] === $r)
                    throw new ArgumentLengthZeroException;
                    
                return $r;
            }
            catch(\Exception $E)
            {
                throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_PARSE_DEF, ['method' => __METHOD__]);
            }
        }
        
        public function validate($__value)
        {
            try
            {
                $v = &$__value;
                $t = getType($v);
                
                foreach($this->getGC() as $h)
                {
                    if(__const_Type::SPL_OBJECT === $t
                    && (class_exists($h) || interface_exists($h))
                    && (is_subclass_of($v, $h) || $v instanceOf $h))
                        return TRUE;
                }
                
                return FALSE;
            }
            catch(\Exception $E)
            {
                throw new ComponentMethodCallException($E, ComponentMethodCallException::ERROR_VALIDATE, ['method' => __METHOD__]);
            }
        }
    }
 
 
 
 
 
 
<?PHP
    namespace root;
        class baz {}
        class bar {}
        $d = (new ADV('root\bar'))->set(new bar);
        var_dump($d);
        $d = (new ADV('root\bar'))->set(new baz);
        var_dump($d);
 
/*
object(ILLI\Core\Std\Def\ADV)#6 (1) {
  ["__data":protected]=>
  object(root\bar)#10 (0) {
  }
}
CHADDR  000000001371a8330000000009eeb411:000000001371a8300000000009eeb411
ECADDR  501:500
RVADDR  
    OUTER   000000001371a8300000000009eeb411: [UNSOLVED]    <ILLI\Core\Std\Def\ADV\ComponentMethodCallException:501>
                                Method Error in ILLI\Core\Std\Def\ADV::set().
    FIRED   000000001371a8330000000009eeb411: [UNSOLVED]    <ILLI\Core\Std\Exception\ArgumentExpectedException:500>
                                Expected type <root\bar>ILLI\Core\Std\Def\ADV: <object>root\baz given.

Comments