fruffl icon

DataType-Value-Pair

fruffl | 02/19/13 06:23:44 PM UTC | 0 ⭐ | 255 👁️ | Never ⏰ | []
PHP |

1.09 KB

|

None

|

0 👍

/

0 👎

<?PHP
    NAMESPACE ILLI\Core\Std\Def;
    USE ILLI\Core\Std\Exception\ArgumentExpectedException;
    USE ILLI\Core\Std\Def;
    
    CLASS ADV
    {
        private static  $__gc       = [];
        protected   $__value    = NULL;
        
        public function __construct($__splType)
        {
            $c = get_called_class();
            $s = &self::$__gc[$c];
            
            isset($s) ?: $s = Def::type($__splType);
        }
        
        public function validate($__value)
        {
            return $this->getADT()->validate($__value);
        }
        
        public function set($__value)
        {
            if(TRUE === $this->validate($__value))
            {
                $this->__data = $__value;
                return $this;
            }
            
            throw new ArgumentExpectedException([
                'target'    => get_called_class(),
                'expected'  => $this->getADT()->__toString(),
                'detected'  => $t = getType($v = $__value),
                'value'     => is_object($v)
                            ? get_class($v)
                            : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
            ]);
        }
        
        public function get()
        {
            return $this->__data;
        }
        
        public function getADT()
        {
            return self::$__gc[get_called_class()];
        }
    }

Comments