Raised DBAL version from 1.1.5 to 1.1.6
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / TypeHandling.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Christian Müller <christian@kitsunet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * PHP type handling functions
27 *
28 * @package Extbase
29 * @subpackage Utility
30 * @version $ID:$
31 * @api
32 */
33 class Tx_Extbase_Utility_TypeHandling {
34
35 /**
36 * A property type parse pattern.
37 */
38 const PARSE_TYPE_PATTERN = '/^\\\\?(?P<type>integer|int|float|double|boolean|bool|string|DateTime|Tx_[a-zA-Z0-9_]+|array|ArrayObject|SplObjectStorage)(?:<(?P<elementType>[a-zA-Z0-9_]+)>)?/';
39
40 /**
41 * Adds (defines) a specific property and its type.
42 *
43 * @param string $type Type of the property (see PARSE_TYPE_PATTERN)
44 * @return array An array with information about the type
45 */
46 static public function parseType($type) {
47 $matches = array();
48 if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
49 $type = self::normalizeType($matches['type']);
50 $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
51
52 if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'SplObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
53 throw new InvalidArgumentException('Type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1264093642);
54 }
55
56 return array(
57 'type' => $type,
58 'elementType' => $elementType
59 );
60 } else {
61 throw new InvalidArgumentException('Invalid type encountered: ' . var_export($type, TRUE), 1264093630);
62 }
63 }
64
65 /**
66 * Normalize data types so they match the PHP type names:
67 * int -> integer
68 * float -> double
69 * bool -> boolean
70 *
71 * @param string $type Data type to unify
72 * @return string unified data type
73 */
74 static public function normalizeType($type) {
75 switch ($type) {
76 case 'int':
77 $type = 'integer';
78 break;
79 case 'bool':
80 $type = 'boolean';
81 break;
82 case 'double':
83 $type = 'float';
84 break;
85 }
86 return $type;
87 }
88
89 }
90 ?>