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