Advertisement
fruffl

ADT/Def

Feb 19th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.07 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core\Std;
  3.     USE ILLI\Core\Std\Exception\ArgumentExpectedException;
  4.     USE ILLI\Core\Std\Def\__const_ADTClass;
  5.     USE ILLI\Core\Std\Def\__const_ADVClass;
  6.     USE ILLI\Core\Std\Def\__const_Type;
  7.     USE ILLI\Core\Std\Def\ADTInstance;
  8.     USE ILLI\Core\Std\Def\ADVInstance;
  9.    
  10.     /**
  11.      * builder for Abstract Data Types (ADT) and ADT-based type-value-pairs (ADV)
  12.      */
  13.     FINAL CLASS Def EXTENDS \ILLI\Core\Std\StaticClass
  14.     {
  15.         #:ADT:
  16.             #:mapping:
  17.             /**
  18.              * ADT class-mapping
  19.              *
  20.              * :gcType<string>
  21.              *  a valid __const_Type
  22.              *
  23.              * :adtClass<string>
  24.              *  - ILLI\Core\Std\Def\ADT*
  25.              *
  26.              * @var     array [{:gcType} => {:adtClass}]
  27.              * @see     ::type()
  28.              * @see     ILLI\Core\Std\Def\__const_ADTClass
  29.              * @see     ILLI\Core\Std\Def\__const_Type
  30.              * @note    ILLI\Core\Std\Def\__const_ADTClass::SPL_INSTANCE will be created by ::type()
  31.              */
  32.             private static $__adt =
  33.             [
  34.                 __const_Type::SPL_ARRAY         => __const_ADTClass::SPL_ARRAY,
  35.                 __const_Type::SPL_BOOLEAN       => __const_ADTClass::SPL_BOOLEAN,
  36.                 __const_Type::SPL_CALLABLE      => __const_ADTClass::SPL_CALLABLE,
  37.                 __const_Type::SPL_CLASS         => __const_ADTClass::SPL_CLASS,
  38.                 __const_Type::SPL_CLOSURE       => __const_ADTClass::SPL_CLOSURE,
  39.                 __const_Type::SPL_DIRECTORY     => __const_ADTClass::SPL_DIRECTORY,
  40.                 __const_Type::SPL_DOUBLE        => __const_ADTClass::SPL_DOUBLE,
  41.                 __const_Type::SPL_FILE          => __const_ADTClass::SPL_FILE,
  42.                 __const_Type::SPL_FLOAT         => __const_ADTClass::SPL_DOUBLE,
  43.                 __const_Type::SPL_FUNCTION      => __const_ADTClass::SPL_FUNCTION,
  44.                 __const_Type::SPL_INTERFACE     => __const_ADTClass::SPL_INTERFACE,
  45.                 __const_Type::SPL_INTEGER       => __const_ADTClass::SPL_LONG,
  46.                 __const_Type::SPL_LONG          => __const_ADTClass::SPL_LONG,
  47.                 __const_Type::SPL_METHOD        => __const_ADTClass::SPL_METHOD,
  48.                 __const_Type::SPL_NULL          => __const_ADTClass::SPL_NULL,
  49.                 __const_Type::SPL_OBJECT        => __const_ADTClass::SPL_OBJECT,
  50.                 __const_Type::SPL_RESOURCE      => __const_ADTClass::SPL_RESOURCE,
  51.                 __const_Type::SPL_STRING        => __const_ADTClass::SPL_STRING,
  52.                 __const_Type::SPL_TRAIT         => __const_ADTClass::SPL_TRAIT,
  53.                 __const_Type::SPL_TUPLE         => __const_ADTClass::SPL_TUPLE
  54.             ];
  55.             #::
  56.            
  57.             #:builder:
  58.             /**
  59.              * create ADT
  60.              *
  61.              * :offset<long>
  62.              *  zero-based index
  63.              *
  64.              * :gcType<string>
  65.              *   a valid __const_Type or class-/interface-name
  66.              *
  67.              * @param   string  $__type     {:gcType}
  68.              * @param   array   $__type     [{:offset} => {:gcType}]
  69.              * @return  ILLI\Core\Std\Def\Type
  70.              * @see     ::$__adt
  71.              * @see     ILLI\Core\Std\Def\ADTInstance
  72.              * @note    unlisted types: interpreting {:gcType} as class/interface and define the abstract-data-type as ADTInstance
  73.              */
  74.             public static function type($__type)
  75.             {
  76.                 if(is_array($__type))
  77.                     return new ADTInstance($__type);
  78.                
  79.                 if(!is_string($__type))
  80.                     throw new ArgumentExpectedException([
  81.                         'target'    => __METHOD__,
  82.                         'expected'  => 'string ILLI\Core\Std\Def\__const_Type::SPL*|string className|array [className:1, …, className:N]',
  83.                         'detected'  => getType($v = $__type),
  84.                         'value'     => is_object($v)
  85.                                     ? get_class($v)
  86.                                     : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
  87.                     ]);
  88.                
  89.                 $n = &self::$__adt[$__type];
  90.                
  91.                 return isset($n)
  92.                     ? new $n
  93.                     : new ADTInstance([$__type]);
  94.             }
  95.             #::
  96.         #::
  97.        
  98.         #:ADV:
  99.             #:mapping:
  100.             /**
  101.              * ADV class-mapping
  102.              *
  103.              * :gcType<string>
  104.              *  a valid __const_Type
  105.              *
  106.              * :advClass<string>
  107.              *  - ILLI\Core\Std\Def\ADV*
  108.              *
  109.              * @var     array [{:type} => {:advClass}]
  110.              * @see     ::value()
  111.              * @see     ILLI\Core\Std\Def\__const_ADVClass
  112.              * @see     ILLI\Core\Std\Def\__const_Type
  113.              * @note    you can not define a NULL-value-pair
  114.              * @note    ILLI\Core\Std\Def\__const_ADVClass::SPL_INSTANCE will be created by ::value()
  115.              */
  116.             private static $__adv =
  117.             [
  118.                 __const_Type::SPL_ARRAY         => __const_ADVClass::SPL_ARRAY,
  119.                 __const_Type::SPL_BOOLEAN       => __const_ADVClass::SPL_BOOLEAN,
  120.                 __const_Type::SPL_CALLABLE      => __const_ADVClass::SPL_CALLABLE,
  121.                 __const_Type::SPL_CLASS         => __const_ADVClass::SPL_CLASS,
  122.                 __const_Type::SPL_CLOSURE       => __const_ADVClass::SPL_CLOSURE,
  123.                 __const_Type::SPL_DIRECTORY     => __const_ADVClass::SPL_DIRECTORY,
  124.                 __const_Type::SPL_DOUBLE        => __const_ADVClass::SPL_DOUBLE,
  125.                 __const_Type::SPL_FILE          => __const_ADVClass::SPL_FILE,
  126.                 __const_Type::SPL_FLOAT         => __const_ADVClass::SPL_DOUBLE,
  127.                 __const_Type::SPL_FUNCTION      => __const_ADVClass::SPL_FUNCTION,
  128.                 __const_Type::SPL_INTERFACE     => __const_ADVClass::SPL_INTERFACE,
  129.                 __const_Type::SPL_INTEGER       => __const_ADVClass::SPL_LONG,
  130.                 __const_Type::SPL_LONG          => __const_ADVClass::SPL_LONG,
  131.                 __const_Type::SPL_METHOD        => __const_ADVClass::SPL_METHOD,
  132.                 __const_Type::SPL_OBJECT        => __const_ADVClass::SPL_OBJECT,
  133.                 __const_Type::SPL_RESOURCE      => __const_ADVClass::SPL_RESOURCE,
  134.                 __const_Type::SPL_STRING        => __const_ADVClass::SPL_STRING,
  135.                 __const_Type::SPL_TRAIT         => __const_ADVClass::SPL_TRAIT,
  136.                 __const_Type::SPL_TUPLE         => __const_ADVClass::SPL_TUPLE
  137.             ];
  138.             #::
  139.            
  140.             #:builder:
  141.             /**
  142.              * create ADV
  143.              *
  144.              * :gcType<string>
  145.              *   a valid __const_Type or class-/interface-name
  146.              *
  147.              * @param   string $__type {:gcType}
  148.              * @return  ILLI\Core\Std\Def\ADV
  149.              * @see     ::$__adv
  150.              * @see     ILLI\Core\Std\Def\ADVInstance
  151.              * @note    unlisted types: interpreting {:gcType} as class/interface and define the type-value-pair as ADVInstance
  152.              */
  153.             public static function value($__type)
  154.             {
  155.                 if(!is_string($__type))
  156.                     throw new ArgumentExpectedException([
  157.                         'target'    => __METHOD__,
  158.                         'expected'  => 'string ILLI\Core\Std\Def\__const_Type::SPL*',
  159.                         'detected'  => getType($v = $__type),
  160.                         'value'     => is_object($v)
  161.                                     ? get_class($v)
  162.                                     : ((is_string($v) && (class_exists($v) || interface_exists($v))) || is_scalar($v)) ? $v : $t
  163.                     ]);
  164.                
  165.                 $n = &self::$__adv[$__type];
  166.                
  167.                 return isset($n)
  168.                     ? new $n($__type)
  169.                     : new ADVInstance($__type);
  170.             }
  171.             #::
  172.         #::
  173.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement