[TASK] Namespace classes
[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) 2011 Extbase Team
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * PHP type handling functions
28 *
29 * @package Extbase
30 * @subpackage Service
31 */
32 class TypeHandlingService implements \TYPO3\CMS\Core\SingletonInterface {
33
34 /**
35 * A property type parse pattern.
36 */
37 const PARSE_TYPE_PATTERN = '/^\\\\?(?P<type>integer|int|float|double|boolean|bool|string|DateTime|Tx_[a-zA-Z0-9_]+|[a-zA-Z0-9\\\\_]+|array|ArrayObject|SplObjectStorage)(?:<(?P<elementType>[a-zA-Z0-9\\\\_]+)>)?/';
38 /**
39 * A type pattern to detect literal types.
40 */
41 const LITERAL_TYPE_PATTERN = '/^(?:integer|int|float|double|boolean|bool|string)$/';
42 /**
43 * Adds (defines) a specific property and its type.
44 *
45 * @param string $type Type of the property (see PARSE_TYPE_PATTERN)
46 * @return array An array with information about the type
47 */
48 public function parseType($type) {
49 $matches = array();
50 if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
51 $type = self::normalizeType($matches['type']);
52 $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
53 if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'SplObjectStorage', 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\ObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
54 throw new \InvalidArgumentException(((('Type "' . $type) . '" must not have an element type hint (') . $elementType) . ').', 1309255650);
55 }
56 return array(
57 'type' => $type,
58 'elementType' => $elementType
59 );
60 } else {
61 throw new \InvalidArgumentException('Invalid type encountered: ' . var_export($type, TRUE), 1309255651);
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 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 $type = ltrim($type, '\\');
87 return $type;
88 }
89
90 /**
91 * Returns TRUE if the $type is a literal.
92 *
93 * @param string $type
94 * @return boolean
95 */
96 public function isLiteral($type) {
97 return preg_match(self::LITERAL_TYPE_PATTERN, $type) === 1;
98 }
99
100 /**
101 * Returns TRUE if the $type is a simple type.
102 *
103 * @param string $type
104 * @return boolean
105 */
106 public function isSimpleType($type) {
107 return in_array(self::normalizeType($type), array('array', 'string', 'float', 'integer', 'boolean'), TRUE);
108 }
109
110 }
111
112
113 ?>