‪TYPO3CMS  ‪main
TypeHandlingUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
29 {
33  public const ‪PARSE_TYPE_PATTERN = '/^\\\\?(?P<type>integer|int|float|double|boolean|bool|string|DateTimeImmutable|DateTime|[A-Z][a-zA-Z0-9\\\\]+|object|resource|array|ArrayObject|SplObjectStorage|TYPO3\\\\CMS\\\\Extbase\\\\Persistence\\\\ObjectStorage)(?:<\\\\?(?P<elementType>[a-zA-Z0-9\\\\]+)>)?/';
34 
38  public const ‪LITERAL_TYPE_PATTERN = '/^(?:integer|int|float|double|boolean|bool|string)$/';
39 
43  protected static ‪$collectionTypes = ['array', \ArrayObject::class, \SplObjectStorage::class, ObjectStorage::class];
44 
53  public static function ‪parseType(string $type): array
54  {
55  $matches = [];
56  if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
57  $type = ‪self::normalizeType($matches['type']);
58  $elementType = isset($matches['elementType']) ? ‪self::normalizeType($matches['elementType']) : null;
59 
60  if ($elementType !== null && !self::isCollectionType($type)) {
61  throw new ‪InvalidTypeException('Found an invalid element type declaration in %s. Type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1264093642);
62  }
63 
64  return [
65  'type' => $type,
66  'elementType' => $elementType,
67  ];
68  }
69  throw new ‪InvalidTypeException('Found an invalid element type declaration in %s. A type "' . var_export($type, true) . '" does not exist.', 1264093630);
70  }
71 
81  public static function ‪normalizeType(string $type): string
82  {
83  switch ($type) {
84  case 'int':
85  $type = 'integer';
86  break;
87  case 'bool':
88  $type = 'boolean';
89  break;
90  case 'double':
91  $type = 'float';
92  break;
93  }
94  return $type;
95  }
96 
100  public static function ‪isLiteral(string $type): bool
101  {
102  return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
103  }
104 
108  public static function ‪isSimpleType(string $type): bool
109  {
110  return in_array(self::normalizeType($type), ['array', 'string', 'float', 'integer', 'boolean'], true);
111  }
112 
118  public static function ‪isCoreType($type): bool
119  {
120  return is_subclass_of($type, TypeInterface::class);
121  }
122 
126  public static function ‪isCollectionType(string $type): bool
127  {
128  if (in_array($type, self::$collectionTypes, true)) {
129  return true;
130  }
131 
132  if (class_exists($type) === true || interface_exists($type) === true) {
133  foreach (self::$collectionTypes as $collectionType) {
134  if (is_subclass_of($type, $collectionType) === true) {
135  return true;
136  }
137  }
138  }
139 
140  return false;
141  }
142 
148  public static function ‪isValidTypeForMultiValueComparison($value): bool
149  {
150  return is_iterable($value);
151  }
152 }
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isSimpleType
‪static isSimpleType(string $type)
Definition: TypeHandlingUtility.php:107
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCoreType
‪static isCoreType($type)
Definition: TypeHandlingUtility.php:117
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\normalizeType
‪static string normalizeType(string $type)
Definition: TypeHandlingUtility.php:80
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\$collectionTypes
‪static array $collectionTypes
Definition: TypeHandlingUtility.php:42
‪TYPO3\CMS\Extbase\Utility\Exception\InvalidTypeException
Definition: InvalidTypeException.php:25
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:34
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:29
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\parseType
‪static array parseType(string $type)
Definition: TypeHandlingUtility.php:52
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isValidTypeForMultiValueComparison
‪static isValidTypeForMultiValueComparison($value)
Definition: TypeHandlingUtility.php:147
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\PARSE_TYPE_PATTERN
‪const PARSE_TYPE_PATTERN
Definition: TypeHandlingUtility.php:33
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\LITERAL_TYPE_PATTERN
‪const LITERAL_TYPE_PATTERN
Definition: TypeHandlingUtility.php:38
‪TYPO3\CMS\Extbase\Utility
Definition: DebuggerUtility.php:18
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isLiteral
‪static isLiteral(string $type)
Definition: TypeHandlingUtility.php:99
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCollectionType
‪static isCollectionType(string $type)
Definition: TypeHandlingUtility.php:125
‪TYPO3\CMS\Core\Type\TypeInterface
Definition: TypeInterface.php:24