fruffl icon

ADT/Def

fruffl | 02/19/13 03:09:42 PM UTC | 0 ⭐ | 320 👁️ | Never ⏰ | []
PHP |

6.07 KB

|

None

|

0 👍

/

0 👎

<?PHP
    NAMESPACE ILLI\Core\Std;
    USE ILLI\Core\Std\Exception\ArgumentExpectedException;
    USE ILLI\Core\Std\Def\__const_ADTClass;
    USE ILLI\Core\Std\Def\__const_ADVClass;
    USE ILLI\Core\Std\Def\__const_Type;
    USE ILLI\Core\Std\Def\ADTInstance;
    USE ILLI\Core\Std\Def\ADVInstance;
    
    /**
     * builder for Abstract Data Types (ADT) and ADT-based type-value-pairs (ADV)
     */
    FINAL CLASS Def EXTENDS \ILLI\Core\Std\StaticClass
    {
        #:ADT:
            #:mapping:
            /**
             * ADT class-mapping
             *
             * :gcType<string>
             *  a valid __const_Type
             *
             * :adtClass<string>
             *  - ILLI\Core\Std\Def\ADT*
             *
             * @var     array [{:gcType} => {:adtClass}]
             * @see     ::type()
             * @see     ILLI\Core\Std\Def\__const_ADTClass
             * @see     ILLI\Core\Std\Def\__const_Type
             * @note    ILLI\Core\Std\Def\__const_ADTClass::SPL_INSTANCE will be created by ::type()
             */
            private static $__adt = 
            [
                __const_Type::SPL_ARRAY         => __const_ADTClass::SPL_ARRAY,
                __const_Type::SPL_BOOLEAN       => __const_ADTClass::SPL_BOOLEAN,
                __const_Type::SPL_CALLABLE      => __const_ADTClass::SPL_CALLABLE,
                __const_Type::SPL_CLASS         => __const_ADTClass::SPL_CLASS,
                __const_Type::SPL_CLOSURE       => __const_ADTClass::SPL_CLOSURE,
                __const_Type::SPL_DIRECTORY     => __const_ADTClass::SPL_DIRECTORY,
                __const_Type::SPL_DOUBLE        => __const_ADTClass::SPL_DOUBLE,
                __const_Type::SPL_FILE          => __const_ADTClass::SPL_FILE,
                __const_Type::SPL_FLOAT         => __const_ADTClass::SPL_DOUBLE,
                __const_Type::SPL_FUNCTION      => __const_ADTClass::SPL_FUNCTION,
                __const_Type::SPL_INTERFACE     => __const_ADTClass::SPL_INTERFACE,
                __const_Type::SPL_INTEGER       => __const_ADTClass::SPL_LONG,
                __const_Type::SPL_LONG          => __const_ADTClass::SPL_LONG,
                __const_Type::SPL_METHOD        => __const_ADTClass::SPL_METHOD,
                __const_Type::SPL_NULL          => __const_ADTClass::SPL_NULL,
                __const_Type::SPL_OBJECT        => __const_ADTClass::SPL_OBJECT,
                __const_Type::SPL_RESOURCE      => __const_ADTClass::SPL_RESOURCE,
                __const_Type::SPL_STRING        => __const_ADTClass::SPL_STRING,
                __const_Type::SPL_TRAIT         => __const_ADTClass::SPL_TRAIT,
                __const_Type::SPL_TUPLE         => __const_ADTClass::SPL_TUPLE
            ];
            #::
            
            #:builder:
            /**
             * create ADT
             *
             * :offset<long>
             *  zero-based index
             *
             * :gcType<string>
             *   a valid __const_Type or class-/interface-name
             *
             * @param   string  $__type     {:gcType}
             * @param   array   $__type     [{:offset} => {:gcType}] 
             * @return  ILLI\Core\Std\Def\Type
             * @see     ::$__adt
             * @see     ILLI\Core\Std\Def\ADTInstance
             * @note    unlisted types: interpreting {:gcType} as class/interface and define the abstract-data-type as ADTInstance
             */
            public static function type($__type)
            {
                if(is_array($__type))
                    return new ADTInstance($__type);
                
                if(!is_string($__type))
                    throw new ArgumentExpectedException([
                        'target'    => __METHOD__,
                        'expected'  => 'string ILLI\Core\Std\Def\__const_Type::SPL*|string className|array [className:1, …, className:N]',
                        'detected'  => getType($v = $__type),
                        'value'     => is_object($v)
                                    ? get_class($v)
                                    : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
                    ]);
                
                $n = &self::$__adt[$__type];
                
                return isset($n)
                    ? new $n
                    : new ADTInstance([$__type]);
            }
            #::
        #::
        
        #:ADV:
            #:mapping:
            /**
             * ADV class-mapping
             *
             * :gcType<string>
             *  a valid __const_Type
             *
             * :advClass<string>
             *  - ILLI\Core\Std\Def\ADV*
             *
             * @var     array [{:type} => {:advClass}]
             * @see     ::value()
             * @see     ILLI\Core\Std\Def\__const_ADVClass
             * @see     ILLI\Core\Std\Def\__const_Type
             * @note    you can not define a NULL-value-pair
             * @note    ILLI\Core\Std\Def\__const_ADVClass::SPL_INSTANCE will be created by ::value()
             */
            private static $__adv = 
            [
                __const_Type::SPL_ARRAY         => __const_ADVClass::SPL_ARRAY,
                __const_Type::SPL_BOOLEAN       => __const_ADVClass::SPL_BOOLEAN,
                __const_Type::SPL_CALLABLE      => __const_ADVClass::SPL_CALLABLE,
                __const_Type::SPL_CLASS         => __const_ADVClass::SPL_CLASS,
                __const_Type::SPL_CLOSURE       => __const_ADVClass::SPL_CLOSURE,
                __const_Type::SPL_DIRECTORY     => __const_ADVClass::SPL_DIRECTORY,
                __const_Type::SPL_DOUBLE        => __const_ADVClass::SPL_DOUBLE,
                __const_Type::SPL_FILE          => __const_ADVClass::SPL_FILE,
                __const_Type::SPL_FLOAT         => __const_ADVClass::SPL_DOUBLE,
                __const_Type::SPL_FUNCTION      => __const_ADVClass::SPL_FUNCTION,
                __const_Type::SPL_INTERFACE     => __const_ADVClass::SPL_INTERFACE,
                __const_Type::SPL_INTEGER       => __const_ADVClass::SPL_LONG,
                __const_Type::SPL_LONG          => __const_ADVClass::SPL_LONG,
                __const_Type::SPL_METHOD        => __const_ADVClass::SPL_METHOD,
                __const_Type::SPL_OBJECT        => __const_ADVClass::SPL_OBJECT,
                __const_Type::SPL_RESOURCE      => __const_ADVClass::SPL_RESOURCE,
                __const_Type::SPL_STRING        => __const_ADVClass::SPL_STRING,
                __const_Type::SPL_TRAIT         => __const_ADVClass::SPL_TRAIT,
                __const_Type::SPL_TUPLE         => __const_ADVClass::SPL_TUPLE
            ];
            #::
            
            #:builder:
            /**
             * create ADV
             *
             * :gcType<string>
             *   a valid __const_Type or class-/interface-name
             *
             * @param   string $__type {:gcType}
             * @return  ILLI\Core\Std\Def\ADV
             * @see     ::$__adv
             * @see     ILLI\Core\Std\Def\ADVInstance
             * @note    unlisted types: interpreting {:gcType} as class/interface and define the type-value-pair as ADVInstance
             */
            public static function value($__type)
            {
                if(!is_string($__type))
                    throw new ArgumentExpectedException([
                        'target'    => __METHOD__,
                        'expected'  => 'string ILLI\Core\Std\Def\__const_Type::SPL*',
                        'detected'  => getType($v = $__type),
                        'value'     => is_object($v)
                                    ? get_class($v)
                                    : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
                    ]);
                
                $n = &self::$__adv[$__type];
                
                return isset($n)
                    ? new $n($__type)
                    : new ADVInstance($__type);
            }
            #::
        #::
    }

Comments