Skip to content

Commit 7469bdb

Browse files
committed
Add a readable and a writable config option
1 parent 10e6f58 commit 7469bdb

File tree

8 files changed

+99
-33
lines changed

8 files changed

+99
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Generated classes include scalar typehints and return type
66
* Nullable typehints are generated instead of a default `null` value
7+
* Add new `readable` and `writable` options to fields to skip getter or mutator generation.
78
* A fluent interface isn't generated anymore by default unless you set the `fluentMutatorMethods` config flag to `true`
89
* Useless PHPDoc (`@param` and `@return` annotations when a typehint exist and mutator description) isn't generated anymore
910
* `DateTimeInterface` is used instead of `DateTime` when possible

src/TypesGenerator.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -295,24 +295,28 @@ public function generate(array $config): void
295295
// Generate ID
296296
if ($config['generateId']) {
297297
foreach ($classes as &$class) {
298-
if (!$class['hasChild'] && !$class['isEnum'] && !$class['embeddable']) {
299-
$class['fields'] = [
300-
'id' => [
301-
'name' => 'id',
302-
'resource' => null,
303-
'range' => 'Integer',
304-
'cardinality' => CardinalitiesExtractor::CARDINALITY_1_1,
305-
'ormColumn' => null,
306-
'isArray' => false,
307-
'isNullable' => false,
308-
'isUnique' => false,
309-
'isCustom' => true,
310-
'isEnum' => false,
311-
'isId' => true,
312-
'typeHint' => 'int',
313-
],
314-
] + $class['fields'];
298+
if ($class['hasChild'] || $class['isEnum'] || $class['embeddable']) {
299+
continue;
315300
}
301+
302+
$class['fields'] = [
303+
'id' => [
304+
'name' => 'id',
305+
'resource' => null,
306+
'range' => 'Integer',
307+
'cardinality' => CardinalitiesExtractor::CARDINALITY_1_1,
308+
'ormColumn' => null,
309+
'isArray' => false,
310+
'isReadable' => true,
311+
'isWritable' => false,
312+
'isNullable' => false,
313+
'isUnique' => false,
314+
'isCustom' => true,
315+
'isEnum' => false,
316+
'isId' => true,
317+
'typeHint' => 'int',
318+
],
319+
] + $class['fields'];
316320
}
317321
}
318322

@@ -593,8 +597,6 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t
593597
$cardinality = $property ? $this->cardinalities[$propertyName] : CardinalitiesExtractor::CARDINALITY_1_1;
594598
}
595599

596-
$ormColumn = $propertyConfig['ormColumn'] ?? null;
597-
598600
$isArray = in_array($cardinality, [
599601
CardinalitiesExtractor::CARDINALITY_0_N,
600602
CardinalitiesExtractor::CARDINALITY_1_N,
@@ -622,8 +624,10 @@ private function generateField(array $config, array $class, \EasyRdf_Resource $t
622624
'resource' => $property,
623625
'range' => $ranges[0],
624626
'cardinality' => $cardinality,
625-
'ormColumn' => $ormColumn,
627+
'ormColumn' => $propertyConfig['ormColumn'] ?? null,
626628
'isArray' => $isArray,
629+
'isReadable' => $propertyConfig['readable'] ?? true,
630+
'isWritable' => $propertyConfig['writable'] ?? true,
627631
'isNullable' => $isNullable,
628632
'isUnique' => isset($propertyConfig['unique']) && $propertyConfig['unique'],
629633
'isCustom' => empty($property),

src/TypesGeneratorConfiguration.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ function ($rdfa) {
162162
->info('Symfony Serialization Groups')
163163
->prototype('scalar')->end()
164164
->end()
165-
->scalarNode('nullable')->defaultTrue()->info('The property nullable')->end()
166-
->scalarNode('unique')->defaultFalse()->info('The property unique')->end()
165+
->booleanNode('readable')->defaultTrue()->info('Is the property readable?')->end()
166+
->booleanNode('writable')->defaultTrue()->info('Is the property writable?')->end()
167+
->booleanNode('nullable')->defaultTrue()->info('Is the property nullable?')->end()
168+
->booleanNode('unique')->defaultFalse()->info('The property unique')->end()
167169
->booleanNode('embedded')->defaultFalse()->info('Is the property embedded?')->end()
168170
->booleanNode('columnPrefix')->defaultFalse()->info('The property columnPrefix')->end()
169171
->end()

templates/class.php.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use {{ use }};
6161
6262
{% if config.accessorMethods %}
6363
{% for field in class.fields %}
64+
{% if field.isWritable %}
6465
{% if field.isArray %}
6566
/**
6667
{% for annotation in field.adderAnnotations %}
@@ -112,7 +113,9 @@ use {{ use }};
112113
{% endif %}
113114
}
114115
{% endif %}
116+
{% endif %}
115117
118+
{% if field.isReadable %}
116119
/**
117120
{% for annotation in field.getterAnnotations %}
118121
* {{ annotation }}
@@ -123,6 +126,7 @@ use {{ use }};
123126
return $this->{{ field.name }};
124127
}
125128
129+
{% endif %}
126130
{% endfor %}
127131
{% endif %}
128132
}

tests/Command/DumpConfigurationTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,13 @@ interface: null
157157
# Symfony Serialization Groups
158158
groups: []
159159
160-
# The property nullable
160+
# Is the property readable?
161+
readable: true
162+
163+
# Is the property writable?
164+
writable: true
165+
166+
# Is the property nullable?
161167
nullable: true
162168
163169
# The property unique

tests/Command/GenerateTypesCommandTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function testDoctrineCollection()
6666
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
6767

6868
$person = file_get_contents($outputDir.'/AddressBook/Entity/Person.php');
69-
7069
$this->assertContains('use Doctrine\Common\Collections\ArrayCollection;', $person);
7170
$this->assertContains('use Doctrine\Common\Collections\Collection;', $person);
7271

@@ -86,8 +85,8 @@ public function testFluentMutators()
8685
$this->fs->mkdir($outputDir);
8786
$commandTester = new CommandTester(new GenerateTypesCommand());
8887
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
89-
$person = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');
9088

89+
$person = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');
9190
$this->assertContains(<<<'PHP'
9291
public function setUrl(?string $url): self
9392
{
@@ -126,10 +125,30 @@ public function testDoNotGenerateAccessorMethods()
126125
$commandTester = new CommandTester(new GenerateTypesCommand());
127126
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
128127

129-
$organization = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');
130-
$this->assertNotContains('function get', $organization);
131-
$this->assertNotContains('function set', $organization);
132-
$this->assertNotContains('function add', $organization);
133-
$this->assertNotContains('function remove', $organization);
128+
$person = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');
129+
$this->assertNotContains('function get', $person);
130+
$this->assertNotContains('function set', $person);
131+
$this->assertNotContains('function add', $person);
132+
$this->assertNotContains('function remove', $person);
133+
}
134+
135+
public function testReadableWritable()
136+
{
137+
$outputDir = __DIR__.'/../../build/readable-writable';
138+
$config = __DIR__.'/../config/readable-writable.yaml';
139+
140+
$this->fs->mkdir($outputDir);
141+
142+
$commandTester = new CommandTester(new GenerateTypesCommand());
143+
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
144+
145+
$person = file_get_contents($outputDir.'/AppBundle/Entity/Person.php');
146+
$this->assertContains('function getId(', $person);
147+
$this->assertNotContains('function setId(', $person);
148+
$this->assertContains('function getName(', $person);
149+
$this->assertNotContains('function setName(', $person);
150+
$this->assertContains('function getFriends(', $person);
151+
$this->assertNotContains('function addFriends(', $person);
152+
$this->assertNotContains('function removeFriends(', $person);
134153
}
135154
}

tests/TypesGeneratorTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\SchemaGenerator\Tests;
1515

1616
use ApiPlatform\SchemaGenerator\CardinalitiesExtractor;
17+
use ApiPlatform\SchemaGenerator\GoodRelationsBridge;
1718
use ApiPlatform\SchemaGenerator\TypesGenerator;
1819
use ApiPlatform\SchemaGenerator\TypesGeneratorConfiguration;
1920
use PHPUnit\Framework\TestCase;
@@ -27,19 +28,19 @@ class TypesGeneratorTest extends TestCase
2728
{
2829
public function testGenerate(): void
2930
{
30-
$twigProphecy = $this->prophesize('Twig_Environment');
31+
$twigProphecy = $this->prophesize(\Twig_Environment::class);
3132
foreach ($this->getClasses() as $class) {
3233
$twigProphecy->render('class.php.twig', Argument::that($this->getContextMatcher($class)))->willReturn()->shouldBeCalled();
3334
}
3435
$twigProphecy->render('class.php.twig', Argument::type('array'))->willReturn();
3536
$twig = $twigProphecy->reveal();
3637

37-
$cardinalitiesExtractorProphecy = $this->prophesize('ApiPlatform\SchemaGenerator\CardinalitiesExtractor');
38+
$cardinalitiesExtractorProphecy = $this->prophesize(CardinalitiesExtractor::class);
3839
$cardinalities = $this->getCardinalities();
3940
$cardinalitiesExtractorProphecy->extract()->willReturn($cardinalities)->shouldBeCalled();
4041
$cardinalitiesExtractor = $cardinalitiesExtractorProphecy->reveal();
4142

42-
$goodRelationsBridgeProphecy = $this->prophesize('ApiPlatform\SchemaGenerator\GoodRelationsBridge');
43+
$goodRelationsBridgeProphecy = $this->prophesize(GoodRelationsBridge::class);
4344
$goodRelationsBridge = $goodRelationsBridgeProphecy->reveal();
4445

4546
$typesGenerator = new TypesGenerator($twig, new NullLogger(), $this->getGraphs(), $cardinalitiesExtractor, $goodRelationsBridge);
@@ -190,6 +191,8 @@ private function getClasses(): array
190191
'isCustom' => false,
191192
'isEnum' => false,
192193
'isId' => false,
194+
'isReadable' => true,
195+
'isWritable' => true,
193196
'isNullable' => true,
194197
'isUnique' => false,
195198
'name' => 'articleBody',
@@ -201,6 +204,8 @@ private function getClasses(): array
201204
'isCustom' => false,
202205
'isEnum' => false,
203206
'isId' => false,
207+
'isReadable' => true,
208+
'isWritable' => true,
204209
'isNullable' => true,
205210
'isUnique' => false,
206211
'name' => 'articleSection',
@@ -223,6 +228,8 @@ private function getClasses(): array
223228
'isCustom' => true,
224229
'isEnum' => false,
225230
'isId' => true,
231+
'isReadable' => true,
232+
'isWritable' => true,
226233
'isNullable' => false,
227234
'isUnique' => false,
228235
'name' => 'id',
@@ -245,6 +252,8 @@ private function getClasses(): array
245252
'isCustom' => false,
246253
'isEnum' => false,
247254
'isId' => false,
255+
'isReadable' => true,
256+
'isWritable' => true,
248257
'isNullable' => true,
249258
'isUnique' => false,
250259
'name' => 'author',
@@ -256,6 +265,8 @@ private function getClasses(): array
256265
'isCustom' => false,
257266
'isEnum' => false,
258267
'isId' => false,
268+
'isReadable' => true,
269+
'isWritable' => true,
259270
'isNullable' => true,
260271
'isUnique' => false,
261272
'name' => 'datePublished',
@@ -267,6 +278,8 @@ private function getClasses(): array
267278
'isCustom' => false,
268279
'isEnum' => false,
269280
'isId' => false,
281+
'isReadable' => true,
282+
'isWritable' => true,
270283
'isNullable' => true,
271284
'isUnique' => false,
272285
'name' => 'headline',
@@ -278,6 +291,8 @@ private function getClasses(): array
278291
'isCustom' => false,
279292
'isEnum' => false,
280293
'isId' => false,
294+
'isReadable' => true,
295+
'isWritable' => true,
281296
'isNullable' => true,
282297
'isUnique' => false,
283298
'name' => 'isFamilyFriendly',
@@ -300,6 +315,8 @@ private function getClasses(): array
300315
'isCustom' => true,
301316
'isEnum' => false,
302317
'isId' => true,
318+
'isReadable' => true,
319+
'isWritable' => true,
303320
'isNullable' => false,
304321
'isUnique' => false,
305322
'name' => 'id',
@@ -322,6 +339,8 @@ private function getClasses(): array
322339
'isCustom' => false,
323340
'isEnum' => false,
324341
'isId' => false,
342+
'isReadable' => true,
343+
'isWritable' => true,
325344
'isNullable' => true,
326345
'isUnique' => false,
327346
'name' => 'sharedContent',
@@ -344,6 +363,8 @@ private function getClasses(): array
344363
'isCustom' => false,
345364
'isEnum' => false,
346365
'isId' => false,
366+
'isReadable' => true,
367+
'isWritable' => true,
347368
'isNullable' => true,
348369
'isUnique' => false,
349370
'name' => 'name',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
rdfa: [{ uri: tests/data/schema.rdfa, format: rdfa }]
2+
relations: [tests/data/v1.owl]
3+
4+
types:
5+
Person:
6+
properties:
7+
name: { writable: false }
8+
familyName: { readable: false }
9+
friends: { range: "Person", cardinality: (0..*), writable: false }

0 commit comments

Comments
 (0)