Skip to content

Commit 9dcb97e

Browse files
feat: default value (#389)
1 parent ed297c7 commit 9dcb97e

File tree

11 files changed

+379
-394
lines changed

11 files changed

+379
-394
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"symfony/string": "^5.2 || ^6.0"
5252
},
5353
"require-dev": {
54-
"api-platform/core": "^v2.7.0-beta.1",
54+
"api-platform/core": "^v2.7.0-rc.1",
5555
"doctrine/orm": "^2.7",
5656
"myclabs/php-enum": "^1.7",
5757
"symfony/doctrine-bridge": "^5.2 || ^6.0",

composer.lock

Lines changed: 338 additions & 389 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ parameters:
1414
readable: boolean,
1515
writable: boolean,
1616
nullable: boolean,
17+
defaultValue: mixed,
1718
required: ?boolean,
1819
unique: boolean,
1920
embedded: boolean,

src/Model/Property.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class Property
2727
public string $cardinality;
2828
/** @var ?string the data type (array and object are not one) */
2929
public ?string $type = null;
30+
/** @var mixed */
31+
public $defaultValue = null;
3032
/** @var ?string the array data type (object is not one) */
3133
public ?string $arrayType = null;
3234
/** @var bool can be true and array type false if the property is an array of references */
@@ -148,7 +150,9 @@ public function toNetteProperty(PhpNamespace $namespace, string $visibility = nu
148150
$property->setNullable($this->isNullable);
149151
}
150152

151-
if (($default = $this->guessDefaultGeneratedValue($useDoctrineCollections)) !== -1) {
153+
if (null !== $default = $this->defaultValue) {
154+
$property->setValue($default);
155+
} elseif (-1 !== $default = $this->guessDefaultGeneratedValue($useDoctrineCollections)) {
152156
$property->setValue($default);
153157
}
154158

src/OpenApi/ClassGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,14 @@ public function generate(OpenApi $openApi, array $config): array
137137
}
138138

139139
// Second pass
140+
$useInterface = $config['useInterface'];
141+
$generateId = $config['id']['generate'];
140142
foreach ($classes as $class) {
141-
if ($config['useInterface']) {
143+
if ($useInterface) {
142144
(new ClassInterfaceMutator($config['namespaces']['interface']))($class, []);
143145
}
144146

145-
if ($config['id']['generate']) {
147+
if ($generateId) {
146148
(new ClassIdAppender(new IdPropertyGenerator(), $config))($class, []);
147149
}
148150

src/Schema/Generator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function generate(array $configuration, OutputInterface $output, SymfonyS
4545
$graphs[] = $graph;
4646
}
4747

48+
$relationsUris = $configuration['relations']['uris'];
4849
$relations = [];
49-
foreach ($configuration['relations']['uris'] as $relation) {
50+
foreach ($relationsUris as $relation) {
5051
$relations[] = new \SimpleXMLElement($relation, 0, true);
5152
}
5253

src/Schema/PropertyGenerator/PropertyGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function __invoke(string $name, array $config, Class_ $class, array $cont
149149
$schemaProperty->range = $range;
150150
$schemaProperty->rangeName = $rangeName;
151151
$schemaProperty->type = $type;
152+
$schemaProperty->defaultValue = $propertyConfig['defaultValue'] ?? null;
152153
$schemaProperty->cardinality = $cardinality;
153154
$schemaProperty->isReadable = $propertyConfig['readable'] ?? true;
154155
$schemaProperty->isWritable = $propertyConfig['writable'] ?? true;

src/SchemaGeneratorConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public function getConfigTreeBuilder(): TreeBuilder
241241
->booleanNode('readable')->defaultTrue()->info('Is the property readable?')->end()
242242
->booleanNode('writable')->defaultTrue()->info('Is the property writable?')->end()
243243
->booleanNode('nullable')->defaultNull()->info('Is the property nullable? (if null, cardinality will be used: will be true if no cardinality found)')->end()
244+
->variableNode('defaultValue')->defaultNull()->info('The property default value')->end()
244245
->booleanNode('required')->defaultTrue()->info('Is the property required?')->end()
245246
->booleanNode('unique')->defaultFalse()->info('The property unique')->end()
246247
->booleanNode('embedded')->defaultFalse()->info('Is the property embedded?')->end()

tests/Command/DumpConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ interface: null
240240
# Is the property nullable? (if null, cardinality will be used: will be true if no cardinality found)
241241
nullable: null
242242
243+
# The property default value
244+
defaultValue: null
245+
243246
# Is the property required?
244247
required: true
245248

tests/Command/GenerateCommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ public function testImplicitAndExplicitPropertyInheritance(): void
206206
$this->assertStringNotContainsString('setName(', $webPage);
207207
}
208208

209+
public function testPropertyDefault(): void
210+
{
211+
$outputDir = __DIR__.'/../../build/property-default';
212+
$config = __DIR__.'/../config/property-default.yaml';
213+
$this->fs->mkdir($outputDir);
214+
$commandTester = new CommandTester(new GenerateCommand());
215+
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
216+
217+
$book = file_get_contents("$outputDir/App/Entity/Book.php");
218+
219+
$this->assertStringContainsString(<<<'PHP'
220+
private string $availability = 'https://schema.org/InStock';
221+
PHP
222+
, $book);
223+
}
224+
209225
public function testReadableWritable(): void
210226
{
211227
$outputDir = __DIR__.'/../../build/readable-writable';

0 commit comments

Comments
 (0)