当前位置: 首页>>代码示例>>PHP>>正文


PHP Type::fromDecl方法代码示例

本文整理汇总了PHP中Type::fromDecl方法的典型用法代码示例。如果您正苦于以下问题:PHP Type::fromDecl方法的具体用法?PHP Type::fromDecl怎么用?PHP Type::fromDecl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Type的用法示例。


在下文中一共展示了Type::fromDecl方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: fromDecl

 /**
  * @param string $decl
  *
  * @return Type The type
  */
 public static function fromDecl($decl)
 {
     if ($decl instanceof Type) {
         return $decl;
     } elseif (!is_string($decl)) {
         throw new \LogicException("Should never happen");
     } elseif (empty($decl)) {
         throw new \RuntimeException("Empty declaration found");
     }
     if ($decl[0] === '\\') {
         $decl = substr($decl, 1);
     } elseif ($decl[0] === '?') {
         $decl = substr($decl, 1);
         $type = Type::fromDecl($decl);
         return (new Type(Type::TYPE_UNION, [$type, new Type(Type::TYPE_NULL)]))->simplify();
     }
     switch (strtolower($decl)) {
         case 'boolean':
         case 'bool':
         case 'false':
         case 'true':
             return new Type(Type::TYPE_BOOLEAN);
         case 'integer':
         case 'int':
             return new Type(Type::TYPE_LONG);
         case 'double':
         case 'real':
         case 'float':
             return new Type(Type::TYPE_DOUBLE);
         case 'string':
             return new Type(Type::TYPE_STRING);
         case 'array':
             return new Type(Type::TYPE_ARRAY);
         case 'callable':
             return new Type(Type::TYPE_CALLABLE);
         case 'null':
             return new Type(Type::TYPE_NULL);
         case 'numeric':
             return Type::fromDecl('int|float');
     }
     // TODO: parse | and & and ()
     if (strpos($decl, '|') !== false || strpos($decl, '&') !== false || strpos($decl, '(') !== false) {
         return self::parseCompexDecl($decl)->simplify();
     }
     if (substr($decl, -2) === '[]') {
         $type = Type::fromDecl(substr($decl, 0, -2));
         return new Type(Type::TYPE_ARRAY, [$type]);
     }
     $regex = '(^([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*\\\\)*[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$)';
     if (!preg_match($regex, $decl)) {
         throw new \RuntimeException("Unknown type declaration found: {$decl}");
     }
     return new Type(Type::TYPE_OBJECT, [], $decl);
 }
开发者ID:alprs,项目名称:Tuli,代码行数:59,代码来源:Type.php

示例2: fromDecl

 public static function fromDecl($decl)
 {
     if ($decl instanceof Type) {
         return $decl;
     } elseif (!is_string($decl)) {
         throw new \LogicException("Should never happen");
     } elseif (empty($decl)) {
         throw new \RuntimeException("Empty declaration found");
     }
     if ($decl[0] === '\\') {
         $decl = substr($decl, 1);
     }
     switch (strtolower($decl)) {
         case 'boolean':
         case 'bool':
             return new Type(Type::TYPE_BOOLEAN);
         case 'integer':
         case 'int':
             return new Type(Type::TYPE_LONG);
         case 'double':
         case 'real':
         case 'float':
             return new Type(Type::TYPE_DOUBLE);
         case 'string':
             return new Type(Type::TYPE_STRING);
         case 'array':
             return new Type(Type::TYPE_ARRAY);
         case 'callable':
             return new Type(Type::TYPE_CALLABLE);
     }
     if (strpos($decl, '|') !== false) {
         $parts = explode('|', $decl);
         $allowedTypes = 0;
         $userTypes = [];
         $subTypes = [];
         foreach ($parts as $part) {
             $type = Type::fromDecl($part);
             $allowedTypes |= $type->type;
             $userTypes = array_merge($type->userTypes, $userTypes);
             $subTypes = array_merge($type->subTypes, $subTypes);
         }
         return new Type($allowedTypes, $subTypes, $userTypes);
     }
     if (substr($decl, -2) === '[]') {
         $type = Type::fromDecl(substr($decl, 0, -2));
         return new Type(Type::TYPE_ARRAY, [$type]);
     }
     if (substr($decl, -2) === '()') {
         // because some people use array() as a type declaration. sigh
         return Type::fromDecl(substr($decl, 0, -2));
     }
     $regex = '(^([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*\\\\)*[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$)';
     if (!preg_match($regex, $decl)) {
         throw new \RuntimeException("Unknown type declaration found: {$decl}");
     }
     return new Type(Type::TYPE_USER, [], [$decl]);
 }
开发者ID:Tjoosten,项目名称:Tuli,代码行数:57,代码来源:Type.php

示例3: __construct

 public function __construct(State $state)
 {
     $this->state = $state;
     $this->callableUnion = Type::fromDecl("string|array|object");
 }
开发者ID:ircmaxell,项目名称:php-types,代码行数:5,代码来源:TypeResolver.php

示例4: testDecl

 /**
  * @dataProvider provideTestDecl
  */
 public function testDecl($decl, $result)
 {
     $type = Type::fromDecl($decl);
     $this->assertEquals($result, $type);
     $this->assertEquals($decl, (string) $type);
 }
开发者ID:alprs,项目名称:Tuli,代码行数:9,代码来源:TypeTest.php

示例5: __construct

 public function __construct(array $components)
 {
     $this->components = $components;
     $this->callableUnion = Type::fromDecl("string|array|object");
 }
开发者ID:alprs,项目名称:Tuli,代码行数:5,代码来源:TypeResolver.php


注:本文中的Type::fromDecl方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。