File tree Expand file tree Collapse file tree 7 files changed +90
-2
lines changed
PropertyProcessor/Property
Schema/BasicSchemaGenerationTest Expand file tree Collapse file tree 7 files changed +90
-2
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ class Property implements PropertyInterface
25
25
protected $ type = 'null ' ;
26
26
/** @var bool */
27
27
protected $ isPropertyRequired = true ;
28
+ /** @var bool */
29
+ protected $ isPropertyReadOnly = false ;
28
30
/** @var string */
29
31
protected $ description = '' ;
30
32
/** @var mixed */
@@ -262,6 +264,17 @@ function ($element) {
262
264
public function setRequired (bool $ isPropertyRequired ): PropertyInterface
263
265
{
264
266
$ this ->isPropertyRequired = $ isPropertyRequired ;
267
+
268
+ return $ this ;
269
+ }
270
+
271
+ /**
272
+ * @inheritdoc
273
+ */
274
+ public function setReadOnly (bool $ isPropertyReadOnly ): PropertyInterface
275
+ {
276
+ $ this ->isPropertyReadOnly = $ isPropertyReadOnly ;
277
+
265
278
return $ this ;
266
279
}
267
280
@@ -291,6 +304,14 @@ public function isRequired(): bool
291
304
return $ this ->isPropertyRequired ;
292
305
}
293
306
307
+ /**
308
+ * @inheritdoc
309
+ */
310
+ public function isReadOnly (): bool
311
+ {
312
+ return $ this ->isPropertyReadOnly ;
313
+ }
314
+
294
315
/**
295
316
* @inheritdoc
296
317
*/
Original file line number Diff line number Diff line change @@ -140,6 +140,13 @@ public function hasDecorators(): bool;
140
140
*/
141
141
public function setRequired (bool $ isPropertyRequired ): PropertyInterface ;
142
142
143
+ /**
144
+ * @param bool $isPropertyReadOnly
145
+ *
146
+ * @return PropertyInterface
147
+ */
148
+ public function setReadOnly (bool $ isPropertyReadOnly ): PropertyInterface ;
149
+
143
150
/**
144
151
* @param mixed $defaultValue
145
152
*
@@ -157,6 +164,11 @@ public function getDefaultValue();
157
164
*/
158
165
public function isRequired (): bool ;
159
166
167
+ /**
168
+ * @return bool
169
+ */
170
+ public function isReadOnly (): bool ;
171
+
160
172
/**
161
173
* Set a nested schema
162
174
*
Original file line number Diff line number Diff line change @@ -196,6 +196,22 @@ public function isRequired(): bool
196
196
return $ this ->getProperty ()->isRequired ();
197
197
}
198
198
199
+ /**
200
+ * @inheritdoc
201
+ */
202
+ public function setReadOnly (bool $ isPropertyReadOnly ): PropertyInterface
203
+ {
204
+ return $ this ->getProperty ()->setReadOnly ($ isPropertyReadOnly );
205
+ }
206
+
207
+ /**
208
+ * @inheritdoc
209
+ */
210
+ public function isReadOnly (): bool
211
+ {
212
+ return $ this ->getProperty ()->isReadOnly ();
213
+ }
214
+
199
215
/**
200
216
* @inheritdoc
201
217
*/
Original file line number Diff line number Diff line change 4
4
5
5
namespace PHPModelGenerator \PropertyProcessor \Property ;
6
6
7
+ use PHPModelGenerator \Exception \SchemaException ;
7
8
use PHPModelGenerator \Model \Property \Property ;
8
9
use PHPModelGenerator \Model \Property \PropertyInterface ;
9
10
use PHPModelGenerator \Model \Schema ;
@@ -39,12 +40,18 @@ public function __construct(
39
40
40
41
/**
41
42
* @inheritdoc
43
+ *
44
+ * @throws SchemaException
42
45
*/
43
46
public function process (string $ propertyName , array $ propertyData ): PropertyInterface
44
47
{
45
48
$ property = (new Property ($ propertyName , $ this ->type ))
46
49
->setDescription ($ propertyData ['description ' ] ?? '' )
47
- ->setRequired ($ this ->propertyCollectionProcessor ->isAttributeRequired ($ propertyName ));
50
+ ->setRequired ($ this ->propertyCollectionProcessor ->isAttributeRequired ($ propertyName ))
51
+ ->setReadOnly (
52
+ (isset ($ propertyData ['readOnly ' ]) && $ propertyData ['readOnly ' ] === true ) ||
53
+ $ this ->schemaProcessor ->getGeneratorConfiguration ()->isImmutable ()
54
+ );
48
55
49
56
$ this ->generateValidators ($ property , $ propertyData );
50
57
Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ class {{ class }}
103
103
return $this->{{ property.getAttribute() }};
104
104
}
105
105
106
- {% if not generatorConfiguration.isImmutable () %}
106
+ {% if not property.isReadOnly () %}
107
107
/**
108
108
* Set the value of {{ property.getName() }}.
109
109
*
Original file line number Diff line number Diff line change @@ -39,6 +39,22 @@ public function testImmutableGeneratorDoesntGenerateSetter(): void
39
39
$ this ->assertNull ($ object ->getProperty ());
40
40
}
41
41
42
+ public function testReadOnlyPropertyDoesntGenerateSetter (): void
43
+ {
44
+ $ className = $ this ->generateClassFromFile ('ReadOnly.json ' );
45
+
46
+ $ object = new $ className ([]);
47
+
48
+ $ this ->assertTrue (is_callable ([$ object , 'getReadOnlyTrue ' ]));
49
+ $ this ->assertFalse (is_callable ([$ object , 'setReadOnlyTrue ' ]));
50
+
51
+ $ this ->assertTrue (is_callable ([$ object , 'getReadOnlyFalse ' ]));
52
+ $ this ->assertTrue (is_callable ([$ object , 'setReadOnlyFalse ' ]));
53
+
54
+ $ this ->assertTrue (is_callable ([$ object , 'getNoReadOnly ' ]));
55
+ $ this ->assertTrue (is_callable ([$ object , 'setNoReadOnly ' ]));
56
+ }
57
+
42
58
public function testSetterChangeTheInternalState (): void
43
59
{
44
60
$ className = $ this ->generateClassFromFile ('BasicSchema.json ' );
Original file line number Diff line number Diff line change
1
+ {
2
+ "type" : " object" ,
3
+ "properties" : {
4
+ "readOnlyTrue" : {
5
+ "type" : " string" ,
6
+ "readOnly" : true
7
+ },
8
+ "readOnlyFalse" : {
9
+ "type" : " string" ,
10
+ "readOnly" : false
11
+ },
12
+ "noReadOnly" : {
13
+ "type" : " string"
14
+ }
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments