[TASK] Replace inject methods with @inject
[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 * @inject
88 */
89 protected $typeHandlingService;
90
91 /**
92 * Constructs this class schema
93 *
94 * @param string $className Name of the class this schema is referring to
95 */
96 public function __construct($className) {
97 $this->className = $className;
98 }
99
100 /**
101 * Returns the class name this schema is referring to
102 *
103 * @return string The class name
104 */
105 public function getClassName() {
106 return $this->className;
107 }
108
109 /**
110 * Adds (defines) a specific property and its type.
111 *
112 * @param string $name Name of the property
113 * @param string $type Type of the property
114 * @param boolean $lazy Whether the property should be lazy-loaded when reconstituting
115 * @param string $cascade Strategy to cascade the object graph.
116 * @return void
117 */
118 public function addProperty($name, $type, $lazy = FALSE, $cascade = '') {
119 $type = $this->typeHandlingService->parseType($type);
120 $this->properties[$name] = array(
121 'type' => $type['type'],
122 'elementType' => $type['elementType'],
123 'lazy' => $lazy,
124 'cascade' => $cascade
125 );
126 }
127
128 /**
129 * Returns the given property defined in this schema. Check with
130 * hasProperty($propertyName) before!
131 *
132 * @param string $propertyName
133 * @return array
134 */
135 public function getProperty($propertyName) {
136 return is_array($this->properties[$propertyName]) ? $this->properties[$propertyName] : array();
137 }
138
139 /**
140 * Returns all properties defined in this schema
141 *
142 * @return array
143 */
144 public function getProperties() {
145 return $this->properties;
146 }
147
148 /**
149 * Sets the model type of the class this schema is referring to.
150 *
151 * @param integer $modelType The model type, one of the MODELTYPE_* constants.
152 * @throws \InvalidArgumentException
153 * @return void
154 */
155 public function setModelType($modelType) {
156 if ($modelType < self::MODELTYPE_ENTITY || $modelType > self::MODELTYPE_VALUEOBJECT) {
157 throw new \InvalidArgumentException('"' . $modelType . '" is an invalid model type.', 1212519195);
158 }
159 $this->modelType = $modelType;
160 }
161
162 /**
163 * Returns the model type of the class this schema is referring to.
164 *
165 * @return integer The model type, one of the MODELTYPE_* constants.
166 */
167 public function getModelType() {
168 return $this->modelType;
169 }
170
171 /**
172 * Marks the class if it is root of an aggregate and therefore accessible
173 * through a repository - or not.
174 *
175 * @param boolean $isRoot TRUE if it is the root of an aggregate
176 * @return void
177 */
178 public function setAggregateRoot($isRoot) {
179 $this->aggregateRoot = $isRoot;
180 }
181
182 /**
183 * Whether the class is an aggregate root and therefore accessible through
184 * a repository.
185 *
186 * @return boolean TRUE if it is managed
187 */
188 public function isAggregateRoot() {
189 return $this->aggregateRoot;
190 }
191
192 /**
193 * If the class schema has a certain property.
194 *
195 * @param string $propertyName Name of the property
196 * @return boolean
197 */
198 public function hasProperty($propertyName) {
199 return array_key_exists($propertyName, $this->properties);
200 }
201
202 /**
203 * Sets the property marked as uuid of an object with @uuid
204 *
205 * @param string $propertyName
206 * @throws \InvalidArgumentException
207 * @return void
208 */
209 public function setUuidPropertyName($propertyName) {
210 if (!array_key_exists($propertyName, $this->properties)) {
211 throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as UUID property.', 1233863842);
212 }
213 $this->uuidPropertyName = $propertyName;
214 }
215
216 /**
217 * Gets the name of the property marked as uuid of an object
218 *
219 * @return string
220 */
221 public function getUuidPropertyName() {
222 return $this->uuidPropertyName;
223 }
224
225 /**
226 * Marks the given property as one of properties forming the identity
227 * of an object. The property must already be registered in the class
228 * schema.
229 *
230 * @param string $propertyName
231 * @throws \InvalidArgumentException
232 * @return void
233 */
234 public function markAsIdentityProperty($propertyName) {
235 if (!array_key_exists($propertyName, $this->properties)) {
236 throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as identity property.', 1233775407);
237 }
238 if ($this->properties[$propertyName]['lazy'] === TRUE) {
239 throw new \InvalidArgumentException('Property "' . $propertyName . '" must not be makred for lazy loading to be marked as identity property.', 1239896904);
240 }
241 $this->identityProperties[$propertyName] = $this->properties[$propertyName]['type'];
242 }
243
244 /**
245 * Gets the properties (names and types) forming the identity of an object.
246 *
247 * @return array
248 * @see markAsIdentityProperty()
249 */
250 public function getIdentityProperties() {
251 return $this->identityProperties;
252 }
253 }
254
255 ?>