Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
TypeGenerator.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009
010 namespace Zend\Code\Generator;
011
012 use Zend\Code\Generator\Exception\InvalidArgumentException;
013
014 use function in_array;
015 use function ltrim;
016 use function preg_match;
017 use function sprintf;
018 use function strpos;
019 use function strtolower;
020 use function substr;
021
022 final class TypeGenerator implements GeneratorInterface
023 {
024 /**
025 * @var bool
026 */
027 private $isInternalPhpType;
028
029 /**
030 * @var string
031 */
032 private $type;
033
034 /**
035 * @var bool
036 */
037 private $nullable;
038
039 /**
040 * @var string[]
041 *
042 * @link http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
043 */
044 private static $internalPhpTypes = [
045 'void',
046 'int',
047 'float',
048 'string',
049 'bool',
050 'array',
051 'callable',
052 'iterable',
053 'object'
054 ];
055
056 /**
057 * @var string a regex pattern to match valid class names or types
058 */
059 private static $validIdentifierMatcher = '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*'
060 . '(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/';
061
062 /**
063 * @param string $type
064 *
065 * @return TypeGenerator
066 *
067 * @throws InvalidArgumentException
068 */
069 public static function fromTypeString($type)
070 {
071 list($nullable, $trimmedNullable) = self::trimNullable($type);
072 list($wasTrimmed, $trimmedType) = self::trimType($trimmedNullable);
073
074 if (! preg_match(self::$validIdentifierMatcher, $trimmedType)) {
075 throw new InvalidArgumentException(sprintf(
076 'Provided type "%s" is invalid: must conform "%s"',
077 $type,
078 self::$validIdentifierMatcher
079 ));
080 }
081
082 $isInternalPhpType = self::isInternalPhpType($trimmedType);
083
084 if ($wasTrimmed && $isInternalPhpType) {
085 throw new InvalidArgumentException(sprintf(
086 'Provided type "%s" is an internal PHP type, but was provided with a namespace separator prefix',
087 $type
088 ));
089 }
090
091 if ($nullable && $isInternalPhpType && 'void' === strtolower($trimmedType)) {
092 throw new InvalidArgumentException(sprintf('Provided type "%s" cannot be nullable', $type));
093 }
094
095 $instance = new self();
096
097 $instance->type = $trimmedType;
098 $instance->nullable = $nullable;
099 $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 }
170