Merge "[BUGFIX] Adjust unit tests to changes"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / TypeHandlingService.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Extbase Team
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 Service
30 */
31 class Tx_Extbase_Service_TypeHandlingService implements t3lib_Singleton {
32
33 /**
34 * A property type parse pattern.
35 */
36 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_]+)>)?/';
37
38 /**
39 * A type pattern to detect literal types.
40 */
41 const LITERAL_TYPE_PATTERN = '/^(?:integer|int|float|double|boolean|bool|string)$/';
42
43 /**
44 * Adds (defines) a specific property and its type.
45 *
46 * @param string $type Type of the property (see PARSE_TYPE_PATTERN)
47 * @return array An array with information about the type
48 */
49 public function parseType($type) {
50 $matches = array();
51 if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
52 $type = self::normalizeType($matches['type']);
53 $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
54
55 if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'SplObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
56 throw new InvalidArgumentException('Type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1309255650);
57 }
58
59 return array(
60 'type' => $type,
61 'elementType' => $elementType
62 );
63 } else {
64 throw new InvalidArgumentException('Invalid type encountered: ' . var_export($type, TRUE), 1309255651);
65 }
66 }
67
68 /**
69 * Normalize data types so they match the PHP type names:
70 * int -> integer
71 * float -> double
72 * bool -> boolean
73 *
74 * @param string $type Data type to unify
75 * @return string unified data type
76 */
77 public function normalizeType($type) {
78 switch ($type) {
79 case 'int':
80 $type = 'integer';
81 break;
82 case 'bool':
83 $type = 'boolean';
84 break;
85 case 'double':
86 $type = 'float';
87 break;
88 }
89 return $type;
90 }
91
92 /**
93 * Returns TRUE if the $type is a literal.
94 *
95 * @param string $type
96 * @return boolean
97 */
98 public function isLiteral($type) {
99 return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
100 }
101
102 /**
103 * Returns TRUE if the $type is a simple type.
104 *
105 * @param string $type
106 * @return boolean
107 */
108 public function isSimpleType($type) {
109 return in_array(self::normalizeType($type), array('array', 'string', 'float', 'integer', 'boolean'), TRUE);
110 }
111
112 }
113 ?>