Skip to content

Commit 26e1f3b

Browse files
committed
Implement default values
1 parent 89f9bf1 commit 26e1f3b

File tree

11 files changed

+349
-11
lines changed

11 files changed

+349
-11
lines changed

src/Model/Property/Property.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Property implements PropertyInterface
2626
protected $isPropertyRequired = true;
2727
/** @var string */
2828
protected $description = '';
29+
/** @var mixed */
30+
protected $defaultValue;
2931

3032
/** @var Validator[] */
3133
protected $validator = [];
@@ -258,6 +260,24 @@ public function setRequired(bool $isPropertyRequired): PropertyInterface
258260
return $this;
259261
}
260262

263+
/**
264+
* @inheritdoc
265+
*/
266+
public function setDefaultValue($defaultValue): PropertyInterface
267+
{
268+
$this->defaultValue = $defaultValue;
269+
270+
return $this;
271+
}
272+
273+
/**
274+
* @inheritdoc
275+
*/
276+
public function getDefaultValue()
277+
{
278+
return var_export($this->defaultValue, true);
279+
}
280+
261281
/**
262282
* @inheritdoc
263283
*/

src/Model/Property/PropertyInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ public function getExceptionClasses(): array;
134134
*/
135135
public function setRequired(bool $isPropertyRequired): PropertyInterface;
136136

137+
/**
138+
* @param mixed $defaultValue
139+
*
140+
* @return PropertyInterface
141+
*/
142+
public function setDefaultValue($defaultValue): PropertyInterface;
143+
144+
/**
145+
* @return mixed
146+
*/
147+
public function getDefaultValue();
148+
137149
/**
138150
* @return bool
139151
*/

src/Model/Property/PropertyProxy.php

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

190+
/**
191+
* @inheritdoc
192+
*/
193+
public function setDefaultValue($defaultValue): PropertyInterface
194+
{
195+
return $this->getProperty()->setDefaultValue($defaultValue);
196+
}
197+
198+
/**
199+
* @inheritdoc
200+
*/
201+
public function getDefaultValue()
202+
{
203+
return $this->getProperty()->getDefaultValue();
204+
}
205+
190206
/**
191207
* @inheritdoc
192208
*/

src/PropertyProcessor/Property/AbstractTypedValueProcessor.php

Lines changed: 46 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\PropertyInterface;
89
use PHPModelGenerator\Model\Schema;
910
use PHPModelGenerator\Model\Validator\TypeCheckValidator;
@@ -34,6 +35,45 @@ public function __construct(
3435
parent::__construct($propertyCollectionProcessor, $schemaProcessor, $schema, static::TYPE);
3536
}
3637

38+
/**
39+
* @param string $propertyName
40+
* @param array $propertyData
41+
*
42+
* @return PropertyInterface
43+
*
44+
* @throws SchemaException
45+
*/
46+
public function process(string $propertyName, array $propertyData): PropertyInterface
47+
{
48+
$property = parent::process($propertyName, $propertyData);
49+
50+
if (isset($propertyData['default'])) {
51+
$this->setDefaultValue($property, $propertyData['default']);
52+
}
53+
54+
return $property;
55+
}
56+
57+
/**
58+
* @param PropertyInterface $property
59+
* @param $default
60+
*
61+
* @throws SchemaException
62+
*/
63+
public function setDefaultValue(PropertyInterface $property, $default): void
64+
{
65+
// allow integer default values for Number properties
66+
if ($this instanceof NumberProcessor && is_int($default)) {
67+
$default = (float) $default;
68+
}
69+
70+
if (!$this->getTypeCheckFunction()($default)) {
71+
throw new SchemaException("Invalid type for default value of property {$property->getName()}");
72+
}
73+
74+
$property->setDefaultValue($default);
75+
}
76+
3777
/**
3878
* @inheritdoc
3979
*/
@@ -46,6 +86,11 @@ protected function generateValidators(PropertyInterface $property, array $proper
4686

4787
protected function getTypeCheck(): string
4888
{
49-
return 'is_' . strtolower(static::TYPE) . '($value) && ';
89+
return $this->getTypeCheckFunction() . '($value) && ';
90+
}
91+
92+
private function getTypeCheckFunction(): string
93+
{
94+
return 'is_' . strtolower(static::TYPE);
5095
}
5196
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
<?php
22

3+
declare(strict_types = 1);
4+
35
namespace PHPModelGenerator\PropertyProcessor\Property;
46

7+
use PHPModelGenerator\Model\Property\PropertyInterface;
8+
9+
/**
10+
* Class AnyProcessor
11+
*
12+
* @package PHPModelGenerator\PropertyProcessor\Property
13+
*/
514
class AnyProcessor extends AbstractValueProcessor
615
{
16+
/**
17+
* @param string $propertyName
18+
* @param array $propertyData
19+
*
20+
* @return PropertyInterface
21+
*/
22+
public function process(string $propertyName, array $propertyData): PropertyInterface
23+
{
24+
$property = parent::process($propertyName, $propertyData);
25+
26+
if (isset($propertyData['default'])) {
27+
$property->setDefaultValue($propertyData['default']);
28+
}
29+
30+
return $property;
31+
}
732
}

src/PropertyProcessor/Property/MultiTypeProcessor.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,14 @@ public function __construct(
6767
* @param array $propertyData An array containing the data of the property
6868
*
6969
* @return PropertyInterface
70+
*
71+
* @throws SchemaException
7072
*/
7173
public function process(string $propertyName, array $propertyData): PropertyInterface
7274
{
7375
$property = parent::process($propertyName, $propertyData);
7476

75-
foreach ($this->propertyProcessors as $propertyProcessor) {
76-
$subProperty = $propertyProcessor->process($propertyName, $propertyData);
77-
$this->transferValidators($subProperty, $property);
78-
79-
if ($subProperty->hasDecorators()) {
80-
$property->addDecorator(new PropertyTransferDecorator($subProperty));
81-
}
82-
}
77+
$this->processSubProperties($propertyName, $propertyData, $property);
8378

8479
if (empty($this->allowedPropertyTypeChecks)) {
8580
return $property;
@@ -122,4 +117,48 @@ protected function transferValidators(PropertyInterface $source, PropertyInterfa
122117
$this->checks[] = $validator->getValidator()->getCheck();
123118
}
124119
}
120+
121+
/**
122+
* @param string $propertyName
123+
* @param array $propertyData
124+
* @param PropertyInterface $property
125+
*
126+
* @throws SchemaException
127+
*/
128+
protected function processSubProperties(
129+
string $propertyName,
130+
array $propertyData,
131+
PropertyInterface $property
132+
): void {
133+
$defaultValue = null;
134+
$invalidDefaultValueException = null;
135+
$invalidDefaultValues = 0;
136+
137+
if ($propertyData['default']) {
138+
$defaultValue = $propertyData['default'];
139+
unset($propertyData['default']);
140+
}
141+
142+
foreach ($this->propertyProcessors as $propertyProcessor) {
143+
$subProperty = $propertyProcessor->process($propertyName, $propertyData);
144+
$this->transferValidators($subProperty, $property);
145+
146+
if ($subProperty->hasDecorators()) {
147+
$property->addDecorator(new PropertyTransferDecorator($subProperty));
148+
}
149+
150+
if ($defaultValue !== null && $propertyProcessor instanceof AbstractTypedValueProcessor) {
151+
try {
152+
$propertyProcessor->setDefaultValue($property, $defaultValue);
153+
} catch (SchemaException $e) {
154+
$invalidDefaultValues++;
155+
$invalidDefaultValueException = $e;
156+
}
157+
}
158+
}
159+
160+
if ($invalidDefaultValues === count($this->propertyProcessors)) {
161+
throw $invalidDefaultValueException;
162+
}
163+
}
125164
}

src/Templates/Model.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class {{ class }}
1818
{
1919
{% foreach properties as property %}
2020
/** @var {% if property.getType() %}{{ property.getType() }}{% else %}mixed{% endif %}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %} */
21-
protected ${{ property.getAttribute() }};
21+
protected ${{ property.getAttribute() }}{% if not viewHelper.isNull(property.getDefaultValue()) %} = {{ property.getDefaultValue() }}{% endif %};
2222
{% endforeach %}
2323
/** @var array */
2424
protected $rawModelDataInput;
@@ -92,7 +92,7 @@ class {{ class }}
9292
*/
9393
protected function process{{ viewHelper.ucfirst(property.getAttribute()) }}(array $modelData): void
9494
{
95-
$value = $rawValue = $modelData['{{ property.getName() }}'] ?? null;
95+
$value = $rawValue = $modelData['{{ property.getName() }}'] ?? $this->{{ property.getAttribute() }};
9696

9797
{{ viewHelper.resolvePropertyDecorator(property) }}
9898

src/Utils/RenderHelper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public function ucfirst(string $value): string
2121
return ucfirst($value);
2222
}
2323

24+
/**
25+
* @param $value
26+
*
27+
* @return bool
28+
*/
29+
public function isNull($value): bool
30+
{
31+
return $value === null;
32+
}
33+
2434
/**
2535
* @param string $fqcn
2636
*

0 commit comments

Comments
 (0)