TYPO3  7.6
TypeHandlingUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Utility;
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|DateTime|Tx_[a-zA-Z0-9_]+|[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('array', 'ArrayObject', 'SplObjectStorage', \TYPO3\CMS\Extbase\Persistence\ObjectStorage::class);
34 
43  public static function parseType($type)
44  {
45  $matches = array();
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 array(
55  'type' => $type,
56  'elementType' => $elementType
57  );
58  } else {
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  }
62 
72  public static function normalizeType($type)
73  {
74  switch ($type) {
75  case 'int':
76  $type = 'integer';
77  break;
78  case 'bool':
79  $type = 'boolean';
80  break;
81  case 'double':
82  $type = 'float';
83  break;
84  }
85  return $type;
86  }
87 
94  public static function isLiteral($type)
95  {
96  return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
97  }
98 
105  public static function isSimpleType($type)
106  {
107  return in_array(self::normalizeType($type), array('array', 'string', 'float', 'integer', 'boolean'), true);
108  }
109 
116  public static function isCoreType($type)
117  {
118  return is_subclass_of($type, \TYPO3\CMS\Core\Type\TypeInterface::class);
119  }
120 
127  public static function isCollectionType($type)
128  {
129  if (in_array($type, self::$collectionTypes, true)) {
130  return true;
131  }
132 
133  if (class_exists($type) === true || interface_exists($type) === true) {
134  foreach (self::$collectionTypes as $collectionType) {
135  if (is_subclass_of($type, $collectionType) === true) {
136  return true;
137  }
138  }
139  }
140 
141  return false;
142  }
143 
150  public static function isValidTypeForMultiValueComparison($value)
151  {
152  return is_array($value) || $value instanceof \Traversable;
153  }
154 
161  public static function hex2bin($hexadecimalData)
162  {
163  $binaryData = '';
164  $length = strlen($hexadecimalData);
165  for ($i = 0; $i < $length; $i += 2) {
166  $binaryData .= pack('C', hexdec(substr($hexadecimalData, $i, 2)));
167  }
168  return $binaryData;
169  }
170 }