[+TASK] Extbase (Persistence): Improved Type Handling of properties. Related to ...
authorJochen Rau <j.rau@web.de>
Thu, 8 Apr 2010 09:23:09 +0000 (09:23 +0000)
committerJochen Rau <j.rau@web.de>
Thu, 8 Apr 2010 09:23:09 +0000 (09:23 +0000)
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapFactory.php
typo3/sysext/extbase/Classes/Persistence/Mapper/DataMapper.php
typo3/sysext/extbase/Classes/Reflection/ClassSchema.php
typo3/sysext/extbase/Classes/Utility/TypeHandling.php [new file with mode: 0644]

index c627483..f4a34e3 100644 (file)
@@ -216,7 +216,6 @@ class Tx_Extbase_Persistence_Mapper_DataMapFactory {
                } else {
                        throw new Tx_Extbase_Persistence_Exception_UnsupportedRelation('The given information to build a many-to-many-relation was not sufficient. Check your TCA definitions. mm-relations with IRRE must have at least a defined "MM" or "foreign_selector".', 1268817963);
                }
-               // debug($columnMap);
                return $columnMap;
        }
                
index f08f87a..e37b802 100644 (file)
@@ -369,7 +369,7 @@ class Tx_Extbase_Persistence_Mapper_DataMapper implements t3lib_Singleton {
                        $query->matching($conditions);
                        
                } else {
-                       throw new Tx_Extbase_Persistence_Exception('Could not determine type of relation.', 1252502725);
+                       throw new Tx_Extbase_Persistence_Exception('Could not determine type of relation. This is mainly caused by a missing type declaration above the property definition.', 1252502725);
                }
                return $query;
        }
index e57eeac..ee810df 100644 (file)
@@ -38,12 +38,7 @@ class Tx_Extbase_Reflection_ClassSchema {
         */
        const MODELTYPE_ENTITY = 1;
        const MODELTYPE_VALUEOBJECT = 2;
-
-       /**
-        * Specifies the allowed property types.
-        */
-       const ALLOWED_TYPES_PATTERN = '/^(?P<type>integer|int|float|boolean|string|DateTime|Tx_[a-zA-Z0-9_]+|array|ArrayObject)(?:<(?P<elementType>[a-zA-Z0-9_]+)>)?/';
-
+       
        /**
         * Name of the class this schema is referring to
         *
@@ -109,32 +104,19 @@ class Tx_Extbase_Reflection_ClassSchema {
         * Adds (defines) a specific property and its type.
         *
         * @param string $name Name of the property
-        * @param string $type Type of the property (see ALLOWED_TYPES_PATTERN)
+        * @param string $type Type of the property
         * @param boolean $lazy Whether the property should be lazy-loaded when reconstituting
         * @param string $cascade Strategy to cascade the object graph.
         * @return void
-        * @author Robert Lemke <robert@typo3.org>
-        * @author Karsten Dambekalns <karsten@typo3.org>
         */
        public function addProperty($name, $type, $lazy = FALSE, $cascade = '') {
-               $matches = array();
-               if (preg_match(self::ALLOWED_TYPES_PATTERN, $type, $matches)) {
-                       $type = ($matches['type'] === 'int') ? 'integer' : $matches['type'];
-                       $elementType = isset($matches['elementType']) ? $matches['elementType'] : NULL;
-
-                       if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'Tx_Extbase_Persistence_ObjectStorage', 'Tx_Extbase_Persistence_LazyObjectStorage'))) {
-                               throw new Tx_Extbase_Reflection_Exception_InvalidPropertyType('Property  of type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1248103053);
-                       }
-
-                       $this->properties[$name] = array(
-                               'type' => $type,
-                               'elementType' => $elementType,
-                               'lazy' => $lazy,
-                               'cascade' => $cascade
-                       );
-               } else {
-                       throw new Tx_Extbase_Reflection_Exception_InvalidPropertyType('Invalid property type encountered: ' . $type, 1220387528);
-               }
+               $type = Tx_Extbase_Utility_TypeHandling::parseType($type);
+               $this->properties[$name] = array(
+                       'type' => $type['type'],
+                       'elementType' => $type['elementType'],
+                       'lazy' => $lazy,
+                       'cascade' => $cascade
+               );
        }
 
        /**
diff --git a/typo3/sysext/extbase/Classes/Utility/TypeHandling.php b/typo3/sysext/extbase/Classes/Utility/TypeHandling.php
new file mode 100644 (file)
index 0000000..a25624e
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Christian Müller <christian@kitsunet.de>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+/**
+ * PHP type handling functions
+ *
+ * @package Extbase
+ * @subpackage Utility
+ * @version $ID:$
+ * @api
+ */
+class Tx_Extbase_Utility_TypeHandling {
+
+       /**
+        * A property type parse pattern.
+        */
+       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_]+)>)?/';
+
+       /**
+        * Adds (defines) a specific property and its type.
+        *
+        * @param string $type Type of the property (see PARSE_TYPE_PATTERN)
+        * @return array An array with information about the type
+        */
+       static public function parseType($type) {
+               $matches = array();
+               if (preg_match(self::PARSE_TYPE_PATTERN, $type, $matches)) {
+                       $type = self::normalizeType($matches['type']);
+                       $elementType = isset($matches['elementType']) ? self::normalizeType($matches['elementType']) : NULL;
+
+                       if ($elementType !== NULL && !in_array($type, array('array', 'ArrayObject', 'SplObjectStorage', 'Tx_Extbase_Persistence_ObjectStorage'))) {
+                               throw new InvalidArgumentException('Type "' . $type . '" must not have an element type hint (' . $elementType . ').', 1264093642);
+                       }
+
+                       return array(
+                               'type' => $type,
+                               'elementType' => $elementType
+                       );
+               } else {
+                       throw new InvalidArgumentException('Invalid type encountered: ' . var_export($type, TRUE), 1264093630);
+               }
+       }
+
+       /**
+        * Normalize data types so they match the PHP type names:
+        *  int -> integer
+        *  float -> double
+        *  bool -> boolean
+        *
+        * @param string $type Data type to unify
+        * @return string unified data type
+        */
+       static public function normalizeType($type) {
+               switch ($type) {
+                       case 'int':
+                               $type = 'integer';
+                               break;
+                       case 'bool':
+                               $type = 'boolean';
+                               break;
+                       case 'double':
+                               $type = 'float';
+                               break;
+               }
+               return $type;
+       }
+       
+}
+?>
\ No newline at end of file