TYPO3 API  SVNRelease
TypeHandling.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Christian Müller <christian@kitsunet.de>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * PHP type handling functions
00027  *
00028  * @package Extbase
00029  * @subpackage Utility
00030  * @version $ID:$
00031  * @api
00032  */
00033 class Tx_Extbase_Utility_TypeHandling {
00034 
00035     /**
00036      * A property type parse pattern.
00037      */
00038     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_]+)>)?/';
00039 
00040     /**
00041      * Adds (defines) a specific property and its type.
00042      *
00043      * @param string $type Type of the property (see PARSE_TYPE_PATTERN)
00044      * @return array An array with information about the type
00045      */
00046     static public function parseType($type) {
00047         $matches = array();
00048         if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
00049             $type = self::normalizeType($matches['type']);
00050             $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
00051 
00052             if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'SplObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
00053                 throw new InvalidArgumentException('Type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1264093642);
00054             }
00055 
00056             return array(
00057                 'type' => $type,
00058                 'elementType' => $elementType
00059             );
00060         } else {
00061             throw new InvalidArgumentException('Invalid type encountered: ' . var_export($type, TRUE), 1264093630);
00062         }
00063     }
00064 
00065     /**
00066      * Normalize data types so they match the PHP type names:
00067      *  int -> integer
00068      *  float -> double
00069      *  bool -> boolean
00070      *
00071      * @param string $type Data type to unify
00072      * @return string unified data type
00073      */
00074     static public function normalizeType($type) {
00075         switch ($type) {
00076             case 'int':
00077                 $type = 'integer';
00078                 break;
00079             case 'bool':
00080                 $type = 'boolean';
00081                 break;
00082             case 'double':
00083                 $type = 'float';
00084                 break;
00085         }
00086         return $type;
00087     }
00088 
00089 }
00090 ?>