‪TYPO3CMS  9.5
TypeHandlingUtility.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the TYPO3 Flow framework. *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
19 {
23  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\\\\]+)>)?/';
24 
28  const ‪LITERAL_TYPE_PATTERN = '/^(?:integer|int|float|double|boolean|bool|string)$/';
29 
33  protected static ‪$collectionTypes = ['array', 'ArrayObject', 'SplObjectStorage', \TYPO3\CMS\Extbase\Persistence\ObjectStorage::class];
34 
43  public static function ‪parseType($type)
44  {
45  $matches = [];
46  if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
47  $type = ‪self::normalizeType($matches['type']);
48  $elementType = isset($matches['elementType']) ? ‪self::normalizeType($matches['elementType']) : null;
49 
50  if ($elementType !== null && !self::isCollectionType($type)) {
51  throw new \TYPO3\CMS\Extbase\Utility\Exception\InvalidTypeException('Found an invalid element type declaration in %s. Type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1264093642);
52  }
53 
54  return [
55  'type' => $type,
56  'elementType' => $elementType
57  ];
58  }
59  throw new \TYPO3\CMS\Extbase\Utility\Exception\InvalidTypeException('Found an invalid element type declaration in %s. A type "' . var_export($type, true) . '" does not exist.', 1264093630);
60  }
61 
71  public static function ‪normalizeType($type)
72  {
73  switch ($type) {
74  case 'int':
75  $type = 'integer';
76  break;
77  case 'bool':
78  $type = 'boolean';
79  break;
80  case 'double':
81  $type = 'float';
82  break;
83  }
84  return $type;
85  }
86 
93  public static function ‪isLiteral($type)
94  {
95  return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
96  }
97 
104  public static function ‪isSimpleType($type)
105  {
106  return in_array(self::normalizeType($type), ['array', 'string', 'float', 'integer', 'boolean'], true);
107  }
108 
115  public static function ‪isCoreType($type)
116  {
117  return is_subclass_of($type, \‪TYPO3\CMS\Core\Type\TypeInterface::class);
118  }
119 
126  public static function ‪isCollectionType($type)
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 
149  public static function ‪isValidTypeForMultiValueComparison($value)
150  {
151  return is_array($value) || $value instanceof \Traversable;
152  }
153 
160  public static function ‪hex2bin($hexadecimalData)
161  {
162  $binaryData = '';
163  $length = strlen($hexadecimalData);
164  for ($i = 0; $i < $length; $i += 2) {
165  $binaryData .= pack('C', hexdec(substr($hexadecimalData, $i, 2)));
166  }
167  return $binaryData;
168  }
169 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\$collectionTypes
‪static array $collectionTypes
Definition: TypeHandlingUtility.php:32
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isSimpleType
‪static bool isSimpleType($type)
Definition: TypeHandlingUtility.php:103
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:19
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\hex2bin
‪static string hex2bin($hexadecimalData)
Definition: TypeHandlingUtility.php:159
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\normalizeType
‪static string normalizeType($type)
Definition: TypeHandlingUtility.php:70
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCoreType
‪static bool isCoreType($type)
Definition: TypeHandlingUtility.php:114
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCollectionType
‪static bool isCollectionType($type)
Definition: TypeHandlingUtility.php:125
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isValidTypeForMultiValueComparison
‪static bool isValidTypeForMultiValueComparison($value)
Definition: TypeHandlingUtility.php:148
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\PARSE_TYPE_PATTERN
‪const PARSE_TYPE_PATTERN
Definition: TypeHandlingUtility.php:23
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\LITERAL_TYPE_PATTERN
‪const LITERAL_TYPE_PATTERN
Definition: TypeHandlingUtility.php:28
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\parseType
‪static array parseType($type)
Definition: TypeHandlingUtility.php:42
‪TYPO3\CMS\Extbase\Utility
Definition: DebuggerUtility.php:2
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isLiteral
‪static bool isLiteral($type)
Definition: TypeHandlingUtility.php:92