Skip to content

Commit 5ec7390

Browse files
authored
Add support for numeric properties (#59)
1 parent f0e65a8 commit 5ec7390

File tree

10 files changed

+40
-23
lines changed

10 files changed

+40
-23
lines changed

src/Model/Property/AbstractProperty.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ public function getName(): string
4949
/**
5050
* @inheritdoc
5151
*/
52-
public function getAttribute(): string
52+
public function getAttribute(bool $variableName = false): string
5353
{
54-
return ($this->isInternal() ? '_' : '') . $this->attribute;
54+
$attribute = !$this->isInternal() && $variableName && preg_match('/^\d/', $this->attribute) === 1
55+
? 'numeric_property_' . $this->attribute
56+
: $this->attribute;
57+
58+
return ($this->isInternal() ? '_' : '') . $attribute;
5559
}
5660

5761
/**

src/Model/Property/PropertyInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ interface PropertyInterface
2424
public function getName(): string;
2525

2626
/**
27+
* @param bool $variableName If set to true the name for the variable is returned. Otherwise, the name for functions
28+
* will be returned
29+
*
2730
* @return string
2831
*/
29-
public function getAttribute(): string;
32+
public function getAttribute(bool $variableName = false): string;
3033

3134
/**
3235
* @param bool $outputType If set to true the output type will be returned (may differ from the base type)

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ protected function addPropertiesToSchema(JsonSchema $propertySchema): void
275275
$propertyMetaDataCollection,
276276
$this->schemaProcessor,
277277
$this->schema,
278-
$propertyName,
278+
(string) $propertyName,
279279
$propertySchema->withJson($propertyStructure)
280280
)
281281
);

src/SchemaProcessor/PostProcessor/Internal/PatternPropertiesPostProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function addPatternPropertiesMapProperty(Schema $schema): void {
107107

108108
foreach ($schema->getProperties() as $property) {
109109
if (!$property->isInternal()) {
110-
$properties[$property->getName()] = $property->getAttribute();
110+
$properties[$property->getName()] = $property->getAttribute(true);
111111
}
112112
}
113113

@@ -157,7 +157,7 @@ public function getCode(): string
157157
'$this->_patternProperties["%s"]["%s"] = &$this->%s;' . PHP_EOL,
158158
$hash,
159159
$matchingProperty->getName(),
160-
$matchingProperty->getAttribute()
160+
$matchingProperty->getAttribute(true)
161161
);
162162
}
163163
}

src/SchemaProcessor/PostProcessor/Internal/SerializationPostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function addSerializeFunctionsForTransformingFilters(
7979
['Serialization', 'TransformingFilterSerializer.phptpl']
8080
),
8181
[
82-
'property' => $property->getAttribute(),
82+
'property' => $property,
8383
'serializerClass' => $serializerClass,
8484
'serializerMethod' => $serializerMethod,
8585
'serializerOptions' => var_export($validator->getFilterOptions(), true),

src/SchemaProcessor/PostProcessor/Templates/Populate.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function populate(array $modelData): self
3131
{% foreach schema.getProperties() as property %}
3232
{% if not property.isInternal() %}
3333
if (array_key_exists('{{ property.getName() }}', $modelData) &&
34-
$modelData['{{ property.getName() }}'] !== $this->{{ property.getAttribute() }}
34+
$modelData['{{ property.getName() }}'] !== $this->{{ property.getAttribute(true) }}
3535
) {
3636
{{ schemaHookResolver.resolveSetterBeforeValidationHook(property, true) }}
3737

38-
$rollbackValues['{{ property.getAttribute() }}'] = $this->{{ property.getAttribute() }};
38+
$rollbackValues['{{ property.getAttribute(true) }}'] = $this->{{ property.getAttribute(true) }};
3939
$this->process{{ viewHelper.ucfirst(property.getAttribute()) }}($modelData);
4040
}
4141
{% endif %}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* serialize the property {{ property }}
2+
* serialize the property {{ property.getAttribute() }}
33
*/
4-
protected function serialize{{ viewHelper.ucfirst(property) }}()
4+
protected function serialize{{ viewHelper.ucfirst(property.getAttribute()) }}()
55
{
6-
return \{{ serializerClass }}::{{ serializerMethod }}($this->{{ property }}, {{ serializerOptions }});
6+
return \{{ serializerClass }}::{{ serializerMethod }}($this->{{ property.getAttribute(true) }}, {{ serializerOptions }});
77
}

src/Templates/Model.phptpl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
2929

3030
{% foreach schema.getProperties() as property %}
3131
/**{% if viewHelper.getTypeHintAnnotation(property, true) %} @var {{ viewHelper.getTypeHintAnnotation(property, true) }}{% endif %}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %} */
32-
{% if property.isInternal() %}private{% else %}protected{% endif %} ${{ property.getAttribute() }}{% if not viewHelper.isNull(property.getDefaultValue()) %} = {{ property.getDefaultValue() }}{% endif %};
32+
{% if property.isInternal() %}private{% else %}protected{% endif %} ${{ property.getAttribute(true) }}{% if not viewHelper.isNull(property.getDefaultValue()) %} = {{ property.getDefaultValue() }}{% endif %};
3333
{% endforeach %}
3434
/** @var array */
3535
protected $_rawModelDataInput = [];
@@ -117,27 +117,27 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
117117
{
118118
{{ schemaHookResolver.resolveGetterHook(property) }}
119119

120-
return $this->{{ property.getAttribute() }};
120+
return $this->{{ property.getAttribute(true) }};
121121
}
122122

123123
{% if not property.isReadOnly() %}
124124
/**
125125
* Set the value of {{ property.getName() }}.
126126
*
127-
* @param {{ viewHelper.getTypeHintAnnotation(property) }} ${{ property.getAttribute() }}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %}
127+
* @param {{ viewHelper.getTypeHintAnnotation(property) }} ${{ property.getAttribute(true) }}{% if property.getDescription() %} {{ property.getDescription() }}{% endif %}
128128
*
129129
* {% if property.getValidators() %}@throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}{% endif %}
130130
*
131131
* @return self
132132
*/
133133
public function set{{ viewHelper.ucfirst(property.getAttribute()) }}(
134-
{{ viewHelper.getType(property) }} ${{ property.getAttribute() }}
134+
{{ viewHelper.getType(property) }} ${{ property.getAttribute(true) }}
135135
): self {
136-
if ($this->{{ property.getAttribute() }} === ${{ property.getAttribute() }}) {
136+
if ($this->{{ property.getAttribute(true) }} === ${{ property.getAttribute(true) }}) {
137137
return $this;
138138
}
139139

140-
$value = $modelData['{{ property.getName() }}'] = ${{ property.getAttribute() }};
140+
$value = $modelData['{{ property.getName() }}'] = ${{ property.getAttribute(true) }};
141141

142142
{% if generatorConfiguration.collectErrors() %}
143143
$this->_errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
@@ -153,8 +153,8 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
153153
}
154154
{% endif %}
155155

156-
$this->{{ property.getAttribute() }} = $value;
157-
$this->_rawModelDataInput['{{ property.getName() }}'] = ${{ property.getAttribute() }};
156+
$this->{{ property.getAttribute(true) }} = $value;
157+
$this->_rawModelDataInput['{{ property.getName() }}'] = ${{ property.getAttribute(true) }};
158158

159159
{{ schemaHookResolver.resolveSetterAfterValidationHook(property) }}
160160

@@ -173,17 +173,17 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
173173
{
174174
{% if not generatorConfiguration.isImplicitNullAllowed() %}
175175
{% if not property.isRequired() %}
176-
if (!array_key_exists('{{ property.getName() }}', $modelData) && $this->{{ property.getAttribute() }} === null) {
176+
if (!array_key_exists('{{ property.getName() }}', $modelData) && $this->{{ property.getAttribute(true) }} === null) {
177177
return;
178178
}
179179
{% endif %}
180180
{% endif %}
181181

182-
$value = array_key_exists('{{ property.getName() }}', $modelData) ? $modelData['{{ property.getName() }}'] : $this->{{ property.getAttribute() }};
182+
$value = array_key_exists('{{ property.getName() }}', $modelData) ? $modelData['{{ property.getName() }}'] : $this->{{ property.getAttribute(true) }};
183183

184184
{{ viewHelper.resolvePropertyDecorator(property, true) }}
185185

186-
$this->{{ property.getAttribute() }} = $this->validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, $modelData);
186+
$this->{{ property.getAttribute(true) }} = $this->validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, $modelData);
187187
}
188188

189189
/**

tests/Basic/BasicSchemaGenerationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,16 @@ public function testPropertyNamesAreNormalized(): void
272272
'minus-property' => '---',
273273
'space property' => ' ',
274274
'numeric42' => 13,
275+
'1000' => 1000,
276+
'1000string' => '1000'
275277
]);
276278

277279
$this->assertSame('___', $object->getUnderscoreProperty());
278280
$this->assertSame('---', $object->getMinusProperty());
279281
$this->assertSame(' ', $object->getSpaceProperty());
280282
$this->assertSame(13, $object->getNumeric42());
283+
$this->assertSame(1000, $object->get1000());
284+
$this->assertSame('1000', $object->get1000string());
281285
}
282286

283287
public function testEmptyNormalizedPropertyNameThrowsAnException(): void

tests/Schema/BasicSchemaGenerationTest/NameNormalization.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
},
1616
"numeric42": {
1717
"type": "integer"
18+
},
19+
"1000": {
20+
"type": "integer"
21+
},
22+
"1000string": {
23+
"type": "string"
1824
}
1925
}
2026
}

0 commit comments

Comments
 (0)