Skip to content

Commit 466523c

Browse files
committed
Map the result of gettype for const properties to internal types to generate valid code (fixes #29)
1 parent 72ace3b commit 466523c

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

src/Model/GeneratorConfiguration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,14 @@ public function getClassNameGenerator(): ClassNameGeneratorInterface
127127

128128
/**
129129
* @param ClassNameGeneratorInterface $classNameGenerator
130+
*
131+
* @return $this
130132
*/
131-
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): void
133+
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): self
132134
{
133135
$this->classNameGenerator = $classNameGenerator;
136+
137+
return $this;
134138
}
135139

136140
/**

src/PropertyProcessor/Property/ConstProcessor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@
1818
*/
1919
class ConstProcessor implements PropertyProcessorInterface
2020
{
21+
const TYPE_MAP = [
22+
'boolean' => 'bool',
23+
'integer' => 'int',
24+
'double' => 'float',
25+
];
26+
2127
/**
2228
* @inheritdoc
2329
*/
2430
public function process(string $propertyName, JsonSchema $propertySchema): PropertyInterface
2531
{
2632
$json = $propertySchema->getJson();
33+
$type = gettype($json['const']);
2734

2835
$property = new Property(
2936
$propertyName,
30-
gettype($json['const']),
37+
self::TYPE_MAP[gettype($json['const'])] ?? $type,
3138
$propertySchema,
3239
$json['description'] ?? ''
3340
);

tests/Objects/ConstPropertyTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public function testProvidedConstPropertyIsValid(): void
2525
{
2626
$className = $this->generateClassFromFile('ConstProperty.json');
2727

28-
$object = new $className(['property' => 'MyConstValue']);
28+
$object = new $className(['stringProperty' => 'MyConstValue', 'integerProperty' => 42]);
2929

30-
$this->assertSame('MyConstValue', $object->getProperty());
30+
$this->assertSame('MyConstValue', $object->getStringProperty());
31+
$this->assertSame(42, $object->getIntegerProperty());
3132
}
3233

3334
/**
@@ -38,7 +39,7 @@ public function testProvidedConstPropertyIsValid(): void
3839
public function testNotProvidedConstPropertyThrowsAnException(): void
3940
{
4041
$this->expectException(ValidationException::class);
41-
$this->expectExceptionMessage('Invalid value for property declined by const constraint');
42+
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');
4243

4344
$className = $this->generateClassFromFile('ConstProperty.json');
4445

@@ -57,11 +58,11 @@ public function testNotProvidedConstPropertyThrowsAnException(): void
5758
public function testNotMatchingProvidedDataThrowsAnException($propertyValue): void
5859
{
5960
$this->expectException(ValidationException::class);
60-
$this->expectExceptionMessage('Invalid value for property declined by const constraint');
61+
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');
6162

6263
$className = $this->generateClassFromFile('ConstProperty.json');
6364

64-
new $className(['property' => $propertyValue]);
65+
new $className(['stringProperty' => $propertyValue]);
6566
}
6667

6768
public function invalidPropertyDataProvider(): array
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"type": "object",
33
"properties": {
4-
"property": {
4+
"stringProperty": {
55
"const": "MyConstValue"
6+
},
7+
"integerProperty": {
8+
"const": 42
69
}
710
}
811
}

0 commit comments

Comments
 (0)