a62c1445de3c30ea37cd7b3d7e8e968e6fbd06b0
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / TypeHandlingService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * PHP type handling functions
32 */
33 class TypeHandlingService implements \TYPO3\CMS\Core\SingletonInterface {
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_]+|[A-Z][a-zA-Z0-9\\\\_]+|array|ArrayObject|SplObjectStorage)(?:<(?P<elementType>[a-zA-Z0-9\\\\_]+)>)?/';
39
40 /**
41 * A type pattern to detect literal types.
42 */
43 const LITERAL_TYPE_PATTERN = '/^(?:integer|int|float|double|boolean|bool|string)$/';
44
45 /**
46 * Adds (defines) a specific property and its type.
47 *
48 * @param string $type Type of the property (see PARSE_TYPE_PATTERN)
49 * @throws \InvalidArgumentException
50 * @return array An array with information about the type
51 */
52 public function parseType($type) {
53 $matches = array();
54 if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
55 $type = self::normalizeType($matches['type']);
56 $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
57 if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'SplObjectStorage', 'TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
58 throw new \InvalidArgumentException(
59 'Type "' . $type . '" must not have an element type hint (' . $elementType . ').',
60 1309255650
61 );
62 }
63 return array(
64 'type' => $type,
65 'elementType' => $elementType
66 );
67 } else {
68 throw new \InvalidArgumentException(
69 'Invalid type encountered: ' . var_export($type, TRUE),
70 1309255651
71 );
72 }
73 }
74
75 /**
76 * Normalize data types so they match the PHP type names:
77 * int -> integer
78 * float -> double
79 * bool -> boolean
80 *
81 * @param string $type Data type to unify
82 * @return string unified data type
83 */
84 public function normalizeType($type) {
85 switch ($type) {
86 case 'int':
87 $type = 'integer';
88 break;
89 case 'bool':
90 $type = 'boolean';
91 break;
92 case 'double':
93 $type = 'float';
94 break;
95 }
96 $type = ltrim($type, '\\');
97 return $type;
98 }
99
100 /**
101 * Returns TRUE if the $type is a literal.
102 *
103 * @param string $type
104 * @return boolean
105 */
106 public function isLiteral($type) {
107 return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
108 }
109
110 /**
111 * Returns TRUE if the $type is a simple type.
112 *
113 * @param string $type
114 * @return boolean
115 */
116 public function isSimpleType($type) {
117 return in_array(self::normalizeType($type), array('array', 'string', 'float', 'integer', 'boolean'), TRUE);
118 }
119 }
120
121 ?>