[TASK] Namespace classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / ClassSchema.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Reflection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * A class schema
31 *
32 * @version $Id$
33 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
34 */
35 class ClassSchema {
36
37 /**
38 * Available model types
39 */
40 const MODELTYPE_ENTITY = 1;
41 const MODELTYPE_VALUEOBJECT = 2;
42 /**
43 * Name of the class this schema is referring to
44 *
45 * @var string
46 */
47 protected $className;
48
49 /**
50 * Model type of the class this schema is referring to
51 *
52 * @var integer
53 */
54 protected $modelType = self::MODELTYPE_ENTITY;
55
56 /**
57 * Whether a repository exists for the class this schema is referring to
58 *
59 * @var boolean
60 */
61 protected $aggregateRoot = FALSE;
62
63 /**
64 * The name of the property holding the uuid of an entity, if any.
65 *
66 * @var string
67 */
68 protected $uuidPropertyName;
69
70 /**
71 * Properties of the class which need to be persisted
72 *
73 * @var array
74 */
75 protected $properties = array();
76
77 /**
78 * The properties forming the identity of an object
79 *
80 * @var array
81 */
82 protected $identityProperties = array();
83
84 /**
85 * @var \TYPO3\CMS\Extbase\Service\TypeHandlingService
86 */
87 protected $typeHandlingService;
88
89 /**
90 * Constructs this class schema
91 *
92 * @param string $className Name of the class this schema is referring to
93 * @author Robert Lemke <robert@typo3.org>
94 */
95 public function __construct($className) {
96 $this->className = $className;
97 }
98
99 /**
100 * @param \TYPO3\CMS\Extbase\Service\TypeHandlingService $typeHandlingService
101 * @return void
102 */
103 public function injectTypeHandlingService(\TYPO3\CMS\Extbase\Service\TypeHandlingService $typeHandlingService) {
104 $this->typeHandlingService = $typeHandlingService;
105 }
106
107 /**
108 * Returns the class name this schema is referring to
109 *
110 * @return string The class name
111 * @author Robert Lemke <robert@typo3.org>
112 */
113 public function getClassName() {
114 return $this->className;
115 }
116
117 /**
118 * Adds (defines) a specific property and its type.
119 *
120 * @param string $name Name of the property
121 * @param string $type Type of the property
122 * @param boolean $lazy Whether the property should be lazy-loaded when reconstituting
123 * @param string $cascade Strategy to cascade the object graph.
124 * @return void
125 */
126 public function addProperty($name, $type, $lazy = FALSE, $cascade = '') {
127 $type = $this->typeHandlingService->parseType($type);
128 $this->properties[$name] = array(
129 'type' => $type['type'],
130 'elementType' => $type['elementType'],
131 'lazy' => $lazy,
132 'cascade' => $cascade
133 );
134 }
135
136 /**
137 * Returns the given property defined in this schema. Check with
138 * hasProperty($propertyName) before!
139 *
140 * @param string $propertyName
141 * @return array
142 * @author Karsten Dambekalns <karsten@typo3.org>
143 */
144 public function getProperty($propertyName) {
145 return is_array($this->properties[$propertyName]) ? $this->properties[$propertyName] : array();
146 }
147
148 /**
149 * Returns all properties defined in this schema
150 *
151 * @return array
152 * @author Robert Lemke <robert@typo3.org>
153 */
154 public function getProperties() {
155 return $this->properties;
156 }
157
158 /**
159 * Sets the model type of the class this schema is referring to.
160 *
161 * @param integer The model type, one of the MODELTYPE_* constants.
162 * @return void
163 * @author Robert Lemke <robert@typo3.org>
164 */
165 public function setModelType($modelType) {
166 if ($modelType < self::MODELTYPE_ENTITY || $modelType > self::MODELTYPE_VALUEOBJECT) {
167 throw new \InvalidArgumentException(('"' . $modelType) . '" is an invalid model type.', 1212519195);
168 }
169 $this->modelType = $modelType;
170 }
171
172 /**
173 * Returns the model type of the class this schema is referring to.
174 *
175 * @return integer The model type, one of the MODELTYPE_* constants.
176 * @author Robert Lemke <robert@typo3.org>
177 */
178 public function getModelType() {
179 return $this->modelType;
180 }
181
182 /**
183 * Marks the class if it is root of an aggregate and therefore accessible
184 * through a repository - or not.
185 *
186 * @param boolean $isRoot TRUE if it is the root of an aggregate
187 * @return void
188 * @author Karsten Dambekalns <karsten@typo3.org>
189 */
190 public function setAggregateRoot($isRoot) {
191 $this->aggregateRoot = $isRoot;
192 }
193
194 /**
195 * Whether the class is an aggregate root and therefore accessible through
196 * a repository.
197 *
198 * @return boolean TRUE if it is managed
199 * @author Karsten Dambekalns <karsten@typo3.org>
200 */
201 public function isAggregateRoot() {
202 return $this->aggregateRoot;
203 }
204
205 /**
206 * If the class schema has a certain property.
207 *
208 * @param string $propertyName Name of the property
209 * @return boolean
210 * @author Robert Lemke <robert@typo3.org>
211 */
212 public function hasProperty($propertyName) {
213 return array_key_exists($propertyName, $this->properties);
214 }
215
216 /**
217 * Sets the property marked as uuid of an object with @uuid
218 *
219 * @param string $propertyName
220 * @return void
221 * @author Karsten Dambekalns <karsten@typo3.org>
222 */
223 public function setUuidPropertyName($propertyName) {
224 if (!array_key_exists($propertyName, $this->properties)) {
225 throw new \InvalidArgumentException(('Property "' . $propertyName) . '" must be added to the class schema before it can be marked as UUID property.', 1233863842);
226 }
227 $this->uuidPropertyName = $propertyName;
228 }
229
230 /**
231 * Gets the name of the property marked as uuid of an object
232 *
233 * @return string
234 * @author Karsten Dambekalns <karsten@typo3.org>
235 */
236 public function getUuidPropertyName() {
237 return $this->uuidPropertyName;
238 }
239
240 /**
241 * Marks the given property as one of properties forming the identity
242 * of an object. The property must already be registered in the class
243 * schema.
244 *
245 * @param string $propertyName
246 * @return void
247 * @author Karsten Dambekalns <karsten@typo3.org>
248 */
249 public function markAsIdentityProperty($propertyName) {
250 if (!array_key_exists($propertyName, $this->properties)) {
251 throw new \InvalidArgumentException(('Property "' . $propertyName) . '" must be added to the class schema before it can be marked as identity property.', 1233775407);
252 }
253 if ($this->properties[$propertyName]['lazy'] === TRUE) {
254 throw new \InvalidArgumentException(('Property "' . $propertyName) . '" must not be makred for lazy loading to be marked as identity property.', 1239896904);
255 }
256 $this->identityProperties[$propertyName] = $this->properties[$propertyName]['type'];
257 }
258
259 /**
260 * Gets the properties (names and types) forming the identity of an object.
261 *
262 * @return array
263 * @author Karsten Dambekalns <karsten@typo3.org>
264 * @see markAsIdentityProperty()
265 */
266 public function getIdentityProperties() {
267 return $this->identityProperties;
268 }
269
270 }
271
272
273 ?>