[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Generator/ -> TypeGenerator.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Code\Generator;
  11  
  12  use Zend\Code\Generator\Exception\InvalidArgumentException;
  13  
  14  use function in_array;
  15  use function ltrim;
  16  use function preg_match;
  17  use function sprintf;
  18  use function strpos;
  19  use function strtolower;
  20  use function substr;
  21  
  22  final class TypeGenerator implements GeneratorInterface
  23  {
  24      /**
  25       * @var bool
  26       */
  27      private $isInternalPhpType;
  28  
  29      /**
  30       * @var string
  31       */
  32      private $type;
  33  
  34      /**
  35       * @var bool
  36       */
  37      private $nullable;
  38  
  39      /**
  40       * @var string[]
  41       *
  42       * @link http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
  43       */
  44      private static $internalPhpTypes = [
  45          'void',
  46          'int',
  47          'float',
  48          'string',
  49          'bool',
  50          'array',
  51          'callable',
  52          'iterable',
  53          'object'
  54      ];
  55  
  56      /**
  57       * @var string a regex pattern to match valid class names or types
  58       */
  59      private static $validIdentifierMatcher = '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*'
  60          . '(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/';
  61  
  62      /**
  63       * @param string $type
  64       *
  65       * @return TypeGenerator
  66       *
  67       * @throws InvalidArgumentException
  68       */
  69      public static function fromTypeString($type)
  70      {
  71          list($nullable, $trimmedNullable) = self::trimNullable($type);
  72          list($wasTrimmed, $trimmedType) = self::trimType($trimmedNullable);
  73  
  74          if (! preg_match(self::$validIdentifierMatcher, $trimmedType)) {
  75              throw new InvalidArgumentException(sprintf(
  76                  'Provided type "%s" is invalid: must conform "%s"',
  77                  $type,
  78                  self::$validIdentifierMatcher
  79              ));
  80          }
  81  
  82          $isInternalPhpType = self::isInternalPhpType($trimmedType);
  83  
  84          if ($wasTrimmed && $isInternalPhpType) {
  85              throw new InvalidArgumentException(sprintf(
  86                  'Provided type "%s" is an internal PHP type, but was provided with a namespace separator prefix',
  87                  $type
  88              ));
  89          }
  90  
  91          if ($nullable && $isInternalPhpType && 'void' === strtolower($trimmedType)) {
  92              throw new InvalidArgumentException(sprintf('Provided type "%s" cannot be nullable', $type));
  93          }
  94  
  95          $instance = new self();
  96  
  97          $instance->type              = $trimmedType;
  98          $instance->nullable          = $nullable;
  99          $instance->isInternalPhpType = $isInternalPhpType;
 100  
 101          return $instance;
 102      }
 103  
 104      private function __construct()
 105      {
 106      }
 107  
 108      /**
 109       * {@inheritDoc}
 110       */
 111      public function generate()
 112      {
 113          $nullable = $this->nullable ? '?' : '';
 114  
 115          if ($this->isInternalPhpType) {
 116              return $nullable . strtolower($this->type);
 117          }
 118  
 119          return $nullable . '\\' . $this->type;
 120      }
 121  
 122      /**
 123       * @return string the cleaned type string
 124       */
 125      public function __toString()
 126      {
 127          return ltrim($this->generate(), '?\\');
 128      }
 129  
 130      /**
 131       * @param string $type
 132       *
 133       * @return bool[]|string[] ordered tuple, first key represents whether the type is nullable, second is the
 134       *                         trimmed string
 135       */
 136      private static function trimNullable($type)
 137      {
 138          if (0 === strpos($type, '?')) {
 139              return [true, substr($type, 1)];
 140          }
 141  
 142          return [false, $type];
 143      }
 144  
 145      /**
 146       * @param string $type
 147       *
 148       * @return bool[]|string[] ordered tuple, first key represents whether the values was trimmed, second is the
 149       *                         trimmed string
 150       */
 151      private static function trimType($type)
 152      {
 153          if (0 === strpos($type, '\\')) {
 154              return [true, substr($type, 1)];
 155          }
 156  
 157          return [false, $type];
 158      }
 159  
 160      /**
 161       * @param string $type
 162       *
 163       * @return bool
 164       */
 165      private static function isInternalPhpType($type)
 166      {
 167          return in_array(strtolower($type), self::$internalPhpTypes, true);
 168      }
 169  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1