1
|
|
|
<?php |
2
|
|
|
namespace Romm\ConfigurationObject\Legacy\Reflection; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A class schema |
21
|
|
|
* |
22
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later |
23
|
|
|
*/ |
24
|
|
|
class ClassSchema |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Available model types |
28
|
|
|
*/ |
29
|
|
|
const MODELTYPE_ENTITY = 1; |
30
|
|
|
const MODELTYPE_VALUEOBJECT = 2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Name of the class this schema is referring to |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $className; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Model type of the class this schema is referring to |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $modelType = self::MODELTYPE_ENTITY; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Whether a repository exists for the class this schema is referring to |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $aggregateRoot = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The name of the property holding the uuid of an entity, if any. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $uuidPropertyName; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Properties of the class which need to be persisted |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $properties = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The properties forming the identity of an object |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $identityProperties = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructs this class schema |
76
|
|
|
* |
77
|
|
|
* @param string $className Name of the class this schema is referring to |
78
|
|
|
*/ |
79
|
|
|
public function __construct($className) |
80
|
|
|
{ |
81
|
|
|
$this->className = $className; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the class name this schema is referring to |
86
|
|
|
* |
87
|
|
|
* @return string The class name |
88
|
|
|
*/ |
89
|
|
|
public function getClassName() |
90
|
|
|
{ |
91
|
|
|
return $this->className; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Adds (defines) a specific property and its type. |
96
|
|
|
* |
97
|
|
|
* @param string $name Name of the property |
98
|
|
|
* @param string $type Type of the property |
99
|
|
|
* @param bool $lazy Whether the property should be lazy-loaded when reconstituting |
100
|
|
|
* @param string $cascade Strategy to cascade the object graph. |
101
|
|
|
*/ |
102
|
|
|
public function addProperty($name, $type, $lazy = false, $cascade = '') |
103
|
|
|
{ |
104
|
|
|
$type = TypeHandlingUtility::parseType($type); |
105
|
|
|
$this->properties[$name] = [ |
106
|
|
|
'type' => $type['type'], |
107
|
|
|
'elementType' => $type['elementType'], |
108
|
|
|
'lazy' => $lazy, |
109
|
|
|
'cascade' => $cascade |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function hasConstructor(): bool |
117
|
|
|
{ |
118
|
|
|
return isset($this->methods['__construct']); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the given property defined in this schema. Check with |
123
|
|
|
* hasProperty($propertyName) before! |
124
|
|
|
* |
125
|
|
|
* @param string $propertyName |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getProperty($propertyName) |
129
|
|
|
{ |
130
|
|
|
return is_array($this->properties[$propertyName]) ? $this->properties[$propertyName] : []; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns all properties defined in this schema |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getProperties() |
139
|
|
|
{ |
140
|
|
|
return $this->properties; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sets the model type of the class this schema is referring to. |
145
|
|
|
* |
146
|
|
|
* @param int $modelType The model type, one of the MODELTYPE_* constants. |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
*/ |
149
|
|
|
public function setModelType($modelType) |
150
|
|
|
{ |
151
|
|
|
if ($modelType < self::MODELTYPE_ENTITY || $modelType > self::MODELTYPE_VALUEOBJECT) { |
152
|
|
|
throw new \InvalidArgumentException('"' . $modelType . '" is an invalid model type.', 1212519195); |
153
|
|
|
} |
154
|
|
|
$this->modelType = $modelType; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the model type of the class this schema is referring to. |
159
|
|
|
* |
160
|
|
|
* @return int The model type, one of the MODELTYPE_* constants. |
161
|
|
|
*/ |
162
|
|
|
public function getModelType() |
163
|
|
|
{ |
164
|
|
|
return $this->modelType; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Marks the class if it is root of an aggregate and therefore accessible |
169
|
|
|
* through a repository - or not. |
170
|
|
|
* |
171
|
|
|
* @param bool $isRoot TRUE if it is the root of an aggregate |
172
|
|
|
*/ |
173
|
|
|
public function setAggregateRoot($isRoot) |
174
|
|
|
{ |
175
|
|
|
$this->aggregateRoot = $isRoot; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Whether the class is an aggregate root and therefore accessible through |
180
|
|
|
* a repository. |
181
|
|
|
* |
182
|
|
|
* @return bool TRUE if it is managed |
183
|
|
|
*/ |
184
|
|
|
public function isAggregateRoot() |
185
|
|
|
{ |
186
|
|
|
return $this->aggregateRoot; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* If the class schema has a certain property. |
191
|
|
|
* |
192
|
|
|
* @param string $propertyName Name of the property |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function hasProperty($propertyName) |
196
|
|
|
{ |
197
|
|
|
return array_key_exists($propertyName, $this->properties); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets the property marked as uuid of an object with @uuid |
202
|
|
|
* |
203
|
|
|
* @param string $propertyName |
204
|
|
|
* @throws \InvalidArgumentException |
205
|
|
|
*/ |
206
|
|
|
public function setUuidPropertyName($propertyName) |
207
|
|
|
{ |
208
|
|
|
if (!array_key_exists($propertyName, $this->properties)) { |
209
|
|
|
throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as UUID property.', 1233863842); |
210
|
|
|
} |
211
|
|
|
$this->uuidPropertyName = $propertyName; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Gets the name of the property marked as uuid of an object |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getUuidPropertyName() |
220
|
|
|
{ |
221
|
|
|
return $this->uuidPropertyName; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Marks the given property as one of properties forming the identity |
226
|
|
|
* of an object. The property must already be registered in the class |
227
|
|
|
* schema. |
228
|
|
|
* |
229
|
|
|
* @param string $propertyName |
230
|
|
|
* @throws \InvalidArgumentException |
231
|
|
|
*/ |
232
|
|
|
public function markAsIdentityProperty($propertyName) |
233
|
|
|
{ |
234
|
|
|
if (!array_key_exists($propertyName, $this->properties)) { |
235
|
|
|
throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as identity property.', 1233775407); |
236
|
|
|
} |
237
|
|
|
if ($this->properties[$propertyName]['lazy'] === true) { |
238
|
|
|
throw new \InvalidArgumentException('Property "' . $propertyName . '" must not be makred for lazy loading to be marked as identity property.', 1239896904); |
239
|
|
|
} |
240
|
|
|
$this->identityProperties[$propertyName] = $this->properties[$propertyName]['type']; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Gets the properties (names and types) forming the identity of an object. |
245
|
|
|
* |
246
|
|
|
* @return array |
247
|
|
|
* @see markAsIdentityProperty() |
248
|
|
|
*/ |
249
|
|
|
public function getIdentityProperties() |
250
|
|
|
{ |
251
|
|
|
return $this->identityProperties; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: