Skip to content

Commit faa4748

Browse files
committed
Add readOnly property modifier
1 parent b2d02b1 commit faa4748

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed

src/Model/Property/Property.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Property implements PropertyInterface
2525
protected $type = 'null';
2626
/** @var bool */
2727
protected $isPropertyRequired = true;
28+
/** @var bool */
29+
protected $isPropertyReadOnly = false;
2830
/** @var string */
2931
protected $description = '';
3032
/** @var mixed */
@@ -262,6 +264,17 @@ function ($element) {
262264
public function setRequired(bool $isPropertyRequired): PropertyInterface
263265
{
264266
$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+
265278
return $this;
266279
}
267280

@@ -291,6 +304,14 @@ public function isRequired(): bool
291304
return $this->isPropertyRequired;
292305
}
293306

307+
/**
308+
* @inheritdoc
309+
*/
310+
public function isReadOnly(): bool
311+
{
312+
return $this->isPropertyReadOnly;
313+
}
314+
294315
/**
295316
* @inheritdoc
296317
*/

src/Model/Property/PropertyInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ public function hasDecorators(): bool;
140140
*/
141141
public function setRequired(bool $isPropertyRequired): PropertyInterface;
142142

143+
/**
144+
* @param bool $isPropertyReadOnly
145+
*
146+
* @return PropertyInterface
147+
*/
148+
public function setReadOnly(bool $isPropertyReadOnly): PropertyInterface;
149+
143150
/**
144151
* @param mixed $defaultValue
145152
*
@@ -157,6 +164,11 @@ public function getDefaultValue();
157164
*/
158165
public function isRequired(): bool;
159166

167+
/**
168+
* @return bool
169+
*/
170+
public function isReadOnly(): bool;
171+
160172
/**
161173
* Set a nested schema
162174
*

src/Model/Property/PropertyProxy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ public function isRequired(): bool
196196
return $this->getProperty()->isRequired();
197197
}
198198

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+
199215
/**
200216
* @inheritdoc
201217
*/

src/PropertyProcessor/Property/AbstractValueProcessor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PHPModelGenerator\PropertyProcessor\Property;
66

7+
use PHPModelGenerator\Exception\SchemaException;
78
use PHPModelGenerator\Model\Property\Property;
89
use PHPModelGenerator\Model\Property\PropertyInterface;
910
use PHPModelGenerator\Model\Schema;
@@ -39,12 +40,18 @@ public function __construct(
3940

4041
/**
4142
* @inheritdoc
43+
*
44+
* @throws SchemaException
4245
*/
4346
public function process(string $propertyName, array $propertyData): PropertyInterface
4447
{
4548
$property = (new Property($propertyName, $this->type))
4649
->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+
);
4855

4956
$this->generateValidators($property, $propertyData);
5057

src/Templates/Model.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class {{ class }}
103103
return $this->{{ property.getAttribute() }};
104104
}
105105

106-
{% if not generatorConfiguration.isImmutable() %}
106+
{% if not property.isReadOnly() %}
107107
/**
108108
* Set the value of {{ property.getName() }}.
109109
*

tests/Basic/BasicSchemaGenerationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public function testImmutableGeneratorDoesntGenerateSetter(): void
3939
$this->assertNull($object->getProperty());
4040
}
4141

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+
4258
public function testSetterChangeTheInternalState(): void
4359
{
4460
$className = $this->generateClassFromFile('BasicSchema.json');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)