Completed
Push — compatibility-v9 ( 6c12d0...62deca )
by Romain
01:58
created

ClassSchema::addProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 4
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
     * Returns the given property defined in this schema. Check with
115
     * hasProperty($propertyName) before!
116
     *
117
     * @param string $propertyName
118
     * @return array
119
     */
120
    public function getProperty($propertyName)
121
    {
122
        return is_array($this->properties[$propertyName]) ? $this->properties[$propertyName] : [];
123
    }
124
125
    /**
126
     * Returns all properties defined in this schema
127
     *
128
     * @return array
129
     */
130
    public function getProperties()
131
    {
132
        return $this->properties;
133
    }
134
135
    /**
136
     * Sets the model type of the class this schema is referring to.
137
     *
138
     * @param int $modelType The model type, one of the MODELTYPE_* constants.
139
     * @throws \InvalidArgumentException
140
     */
141
    public function setModelType($modelType)
142
    {
143
        if ($modelType < self::MODELTYPE_ENTITY || $modelType > self::MODELTYPE_VALUEOBJECT) {
144
            throw new \InvalidArgumentException('"' . $modelType . '" is an invalid model type.', 1212519195);
145
        }
146
        $this->modelType = $modelType;
147
    }
148
149
    /**
150
     * Returns the model type of the class this schema is referring to.
151
     *
152
     * @return int The model type, one of the MODELTYPE_* constants.
153
     */
154
    public function getModelType()
155
    {
156
        return $this->modelType;
157
    }
158
159
    /**
160
     * Marks the class if it is root of an aggregate and therefore accessible
161
     * through a repository - or not.
162
     *
163
     * @param bool $isRoot TRUE if it is the root of an aggregate
164
     */
165
    public function setAggregateRoot($isRoot)
166
    {
167
        $this->aggregateRoot = $isRoot;
168
    }
169
170
    /**
171
     * Whether the class is an aggregate root and therefore accessible through
172
     * a repository.
173
     *
174
     * @return bool TRUE if it is managed
175
     */
176
    public function isAggregateRoot()
177
    {
178
        return $this->aggregateRoot;
179
    }
180
181
    /**
182
     * If the class schema has a certain property.
183
     *
184
     * @param string $propertyName Name of the property
185
     * @return bool
186
     */
187
    public function hasProperty($propertyName)
188
    {
189
        return array_key_exists($propertyName, $this->properties);
190
    }
191
192
    /**
193
     * Sets the property marked as uuid of an object with @uuid
194
     *
195
     * @param string $propertyName
196
     * @throws \InvalidArgumentException
197
     */
198
    public function setUuidPropertyName($propertyName)
199
    {
200
        if (!array_key_exists($propertyName, $this->properties)) {
201
            throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as UUID property.', 1233863842);
202
        }
203
        $this->uuidPropertyName = $propertyName;
204
    }
205
206
    /**
207
     * Gets the name of the property marked as uuid of an object
208
     *
209
     * @return string
210
     */
211
    public function getUuidPropertyName()
212
    {
213
        return $this->uuidPropertyName;
214
    }
215
216
    /**
217
     * Marks the given property as one of properties forming the identity
218
     * of an object. The property must already be registered in the class
219
     * schema.
220
     *
221
     * @param string $propertyName
222
     * @throws \InvalidArgumentException
223
     */
224
    public function markAsIdentityProperty($propertyName)
225
    {
226
        if (!array_key_exists($propertyName, $this->properties)) {
227
            throw new \InvalidArgumentException('Property "' . $propertyName . '" must be added to the class schema before it can be marked as identity property.', 1233775407);
228
        }
229
        if ($this->properties[$propertyName]['lazy'] === true) {
230
            throw new \InvalidArgumentException('Property "' . $propertyName . '" must not be makred for lazy loading to be marked as identity property.', 1239896904);
231
        }
232
        $this->identityProperties[$propertyName] = $this->properties[$propertyName]['type'];
233
    }
234
235
    /**
236
     * Gets the properties (names and types) forming the identity of an object.
237
     *
238
     * @return array
239
     * @see markAsIdentityProperty()
240
     */
241
    public function getIdentityProperties()
242
    {
243
        return $this->identityProperties;
244
    }
245
}
246