TYPO3 CMS  TYPO3_6-2
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|DateTime|Tx_[a-zA-Z0-9_]+|[A-Z][a-zA-Z0-9\\\\_]+|object|array|ArrayObject|SplObjectStorage|TYPO3\\\\CMS\\\\Extbase\\\\Persistence\\\\ObjectStorage|Tx_Extbase_Persistence_ObjectStorage)(?:<\\\\?(?P<elementType>[a-zA-Z0-9\\\\_]+)>)?/';
24 
28  const LITERAL_TYPE_PATTERN = '/^(?:integer|int|float|double|boolean|bool|string)$/';
29 
33  static protected $collectionTypes = array('array', 'ArrayObject', 'SplObjectStorage', 'TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage');
34 
43  static public function parseType($type) {
44  $matches = array();
45  if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
46  $type = self::normalizeType($matches['type']);
47  $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
48 
49  if ($elementType !== NULL && !self::isCollectionType($type)) {
50  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);
51  }
52 
53  return array(
54  'type' => $type,
55  'elementType' => $elementType
56  );
57  } else {
58  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);
59  }
60  }
61 
71  static public function normalizeType($type) {
72  switch ($type) {
73  case 'int':
74  $type = 'integer';
75  break;
76  case 'bool':
77  $type = 'boolean';
78  break;
79  case 'double':
80  $type = 'float';
81  break;
82  }
83  return $type;
84  }
85 
92  static public function isLiteral($type) {
93  return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
94  }
95 
102  static public function isSimpleType($type) {
103  return in_array(self::normalizeType($type), array('array', 'string', 'float', 'integer', 'boolean'), TRUE);
104  }
105 
112  static public function isCoreType($type) {
113  return is_subclass_of($type, 'TYPO3\\CMS\\Core\\Type\\TypeInterface');
114  }
115 
122  static public function isCollectionType($type) {
123  if (in_array($type, self::$collectionTypes, TRUE)) {
124  return TRUE;
125  }
126 
127  if (class_exists($type) === TRUE || interface_exists($type) === TRUE) {
128  foreach (self::$collectionTypes as $collectionType) {
129  if (is_subclass_of($type, $collectionType) === TRUE) {
130  return TRUE;
131  }
132  }
133  }
134 
135  return FALSE;
136  }
137 
144  static public function hex2bin($hexadecimalData) {
145  $binaryData = '';
146  $length = strlen($hexadecimalData);
147  for ($i = 0; $i < $length; $i += 2) {
148  $binaryData .= pack('C', hexdec(substr($hexadecimalData, $i, 2)));
149  }
150  return $binaryData;
151  }
152 }