TYPO3 API  SVNRelease
ClassSchema.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * A class schema
00030  *
00031  * @version $Id: ClassSchema.php 2180 2010-04-08 09:23:09Z jocrau $
00032  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00033  */
00034 class Tx_Extbase_Reflection_ClassSchema {
00035 
00036     /**
00037      * Available model types
00038      */
00039     const MODELTYPE_ENTITY = 1;
00040     const MODELTYPE_VALUEOBJECT = 2;
00041 
00042     /**
00043      * Name of the class this schema is referring to
00044      *
00045      * @var string
00046      */
00047     protected $className;
00048 
00049     /**
00050      * Model type of the class this schema is referring to
00051      *
00052      * @var integer
00053      */
00054     protected $modelType = self::MODELTYPE_ENTITY;
00055 
00056     /**
00057      * Whether a repository exists for the class this schema is referring to
00058      * @var boolean
00059      */
00060     protected $aggregateRoot = FALSE;
00061 
00062     /**
00063      * The name of the property holding the uuid of an entity, if any.
00064      *
00065      * @var string
00066      */
00067     protected $uuidPropertyName;
00068 
00069     /**
00070      * Properties of the class which need to be persisted
00071      *
00072      * @var array
00073      */
00074     protected $properties = array();
00075 
00076     /**
00077      * The properties forming the identity of an object
00078      *
00079      * @var array
00080      */
00081     protected $identityProperties = array();
00082 
00083     /**
00084      * Constructs this class schema
00085      *
00086      * @param string $className Name of the class this schema is referring to
00087      * @author Robert Lemke <robert@typo3.org>
00088      */
00089     public function __construct($className) {
00090         $this->className = $className;
00091     }
00092 
00093     /**
00094      * Returns the class name this schema is referring to
00095      *
00096      * @return string The class name
00097      * @author Robert Lemke <robert@typo3.org>
00098      */
00099     public function getClassName() {
00100         return $this->className;
00101     }
00102 
00103     /**
00104      * Adds (defines) a specific property and its type.
00105      *
00106      * @param string $name Name of the property
00107      * @param string $type Type of the property
00108      * @param boolean $lazy Whether the property should be lazy-loaded when reconstituting
00109      * @param string $cascade Strategy to cascade the object graph.
00110      * @return void
00111      */
00112     public function addProperty($name, $type, $lazy = FALSE, $cascade = '') {
00113         $type = Tx_Extbase_Utility_TypeHandling::parseType($type);
00114         $this->properties[$name] = array(
00115             'type' => $type['type'],
00116             'elementType' => $type['elementType'],
00117             'lazy' => $lazy,
00118             'cascade' => $cascade
00119         );
00120     }
00121 
00122     /**
00123      * Returns the given property defined in this schema. Check with
00124      * hasProperty($propertyName) before!
00125      *
00126      * @return array
00127      * @author Karsten Dambekalns <karsten@typo3.org>
00128      */
00129     public function getProperty($propertyName) {
00130         return is_array($this->properties[$propertyName]) ? $this->properties[$propertyName] : array();
00131     }
00132 
00133     /**
00134      * Returns all properties defined in this schema
00135      *
00136      * @return array
00137      * @author Robert Lemke <robert@typo3.org>
00138      */
00139     public function getProperties() {
00140         return $this->properties;
00141     }
00142 
00143     /**
00144      * Sets the model type of the class this schema is referring to.
00145      *
00146      * @param integer The model type, one of the MODELTYPE_* constants.
00147      * @return void
00148      * @author Robert Lemke <robert@typo3.org>
00149      */
00150     public function setModelType($modelType) {
00151         if ($modelType < self::MODELTYPE_ENTITY || $modelType > self::MODELTYPE_VALUEOBJECT) throw new InvalidArgumentException('"' . $modelType . '" is an invalid model type.', 1212519195);
00152         $this->modelType = $modelType;
00153     }
00154 
00155     /**
00156      * Returns the model type of the class this schema is referring to.
00157      *
00158      * @return integer The model type, one of the MODELTYPE_* constants.
00159      * @author Robert Lemke <robert@typo3.org>
00160      */
00161     public function getModelType() {
00162         return $this->modelType;
00163     }
00164 
00165     /**
00166      * Marks the class if it is root of an aggregate and therefore accessible
00167      * through a repository - or not.
00168      *
00169      * @param boolean $isRoot TRUE if it is the root of an aggregate
00170      * @return void
00171      * @author Karsten Dambekalns <karsten@typo3.org>
00172      */
00173     public function setAggregateRoot($isRoot) {
00174         $this->aggregateRoot = $isRoot;
00175     }
00176 
00177     /**
00178      * Whether the class is an aggregate root and therefore accessible through
00179      * a repository.
00180      *
00181      * @return boolean TRUE if it is managed
00182      * @author Karsten Dambekalns <karsten@typo3.org>
00183      */
00184     public function isAggregateRoot() {
00185         return $this->aggregateRoot;
00186     }
00187 
00188     /**
00189      * If the class schema has a certain property.
00190      *
00191      * @param string $propertyName Name of the property
00192      * @return boolean
00193      * @author Robert Lemke <robert@typo3.org>
00194      */
00195     public function hasProperty($propertyName) {
00196         return array_key_exists($propertyName, $this->properties);
00197     }
00198 
00199     /**
00200      * Sets the property marked as uuid of an object with @uuid
00201      *
00202      * @param string $propertyName
00203      * @return void
00204      * @author Karsten Dambekalns <karsten@typo3.org>
00205      */
00206     public function setUUIDPropertyName($propertyName) {
00207         if (!array_key_exists($propertyName, $this->properties)) {
00208             throw new InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as UUID property.', 1233863842);
00209         }
00210 
00211         $this->uuidPropertyName = $propertyName;
00212     }
00213 
00214     /**
00215      * Gets the name of the property marked as uuid of an object
00216      *
00217      * @return string
00218      * @author Karsten Dambekalns <karsten@typo3.org>
00219      */
00220     public function getUUIDPropertyName() {
00221         return $this->uuidPropertyName;
00222     }
00223 
00224     /**
00225      * Marks the given property as one of properties forming the identity
00226      * of an object. The property must already be registered in the class
00227      * schema.
00228      *
00229      * @param string $propertyName
00230      * @return void
00231      * @author Karsten Dambekalns <karsten@typo3.org>
00232      */
00233     public function markAsIdentityProperty($propertyName) {
00234         if (!array_key_exists($propertyName, $this->properties)) {
00235             throw new InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as identity property.', 1233775407);
00236         }
00237         if ($this->properties[$propertyName]['lazy'] === TRUE) {
00238             throw new InvalidArgumentException('Property "' . $propertyName . '" must not be makred for lazy loading to be marked as identity property.', 1239896904);
00239         }
00240 
00241         $this->identityProperties[$propertyName] = $this->properties[$propertyName]['type'];
00242     }
00243 
00244     /**
00245      * Gets the properties (names and types) forming the identity of an object.
00246      *
00247      * @return array
00248      * @author Karsten Dambekalns <karsten@typo3.org>
00249      * @see markAsIdentityProperty()
00250      */
00251     public function getIdentityProperties() {
00252         return $this->identityProperties;
00253     }
00254 
00255 }
00256 ?>