‪TYPO3CMS  10.4
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  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  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 
103  public static function ‪isLiteral(string $type): bool
104  {
105  return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
106  }
107 
114  public static function ‪isSimpleType(string $type): bool
115  {
116  return in_array(self::normalizeType($type), ['array', 'string', 'float', 'integer', 'boolean'], true);
117  }
118 
125  public static function ‪isCoreType($type): bool
126  {
127  return is_subclass_of($type, TypeInterface::class);
128  }
129 
136  public static function ‪isCollectionType(string $type): bool
137  {
138  if (in_array($type, self::$collectionTypes, true)) {
139  return true;
140  }
141 
142  if (class_exists($type) === true || interface_exists($type) === true) {
143  foreach (self::$collectionTypes as $collectionType) {
144  if (is_subclass_of($type, $collectionType) === true) {
145  return true;
146  }
147  }
148  }
149 
150  return false;
151  }
152 
159  public static function ‪isValidTypeForMultiValueComparison($value): bool
160  {
161  return is_iterable($value);
162  }
163 
171  public static function ‪hex2bin(string $hexadecimalData): string
172  {
173  trigger_error(
174  'Method ' . __METHOD__ . ' will be removed in TYPO3 11.0, use native hex2bin instead.',
175  E_USER_DEPRECATED
176  );
177 
178  $binaryData = '';
179  $length = strlen($hexadecimalData);
180  for ($i = 0; $i < $length; $i += 2) {
181  $binaryData .= pack('C', hexdec(substr($hexadecimalData, $i, 2)));
182  }
183  return $binaryData;
184  }
185 }
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\hex2bin
‪static string hex2bin(string $hexadecimalData)
Definition: TypeHandlingUtility.php:170
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isLiteral
‪static bool isLiteral(string $type)
Definition: TypeHandlingUtility.php:102
‪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:26
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isSimpleType
‪static bool isSimpleType(string $type)
Definition: TypeHandlingUtility.php:113
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCollectionType
‪static bool isCollectionType(string $type)
Definition: TypeHandlingUtility.php:135
‪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\isCoreType
‪static bool isCoreType($type)
Definition: TypeHandlingUtility.php:124
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isValidTypeForMultiValueComparison
‪static bool isValidTypeForMultiValueComparison($value)
Definition: TypeHandlingUtility.php:158
‪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\Core\Type\TypeInterface
Definition: TypeInterface.php:24