Skip to content

Commit a578225

Browse files
committed
Allow to generate all properties again
1 parent 67bd04e commit a578225

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/TypesGenerator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ public function __construct(\Twig_Environment $twig, LoggerInterface $logger, ar
113113

114114
/**
115115
* Generates files.
116+
*
117+
* @param array $config
116118
*/
117-
public function generate($config)
119+
public function generate(array $config)
118120
{
119121
$baseClass = [
120122
'constants' => [],
@@ -226,11 +228,9 @@ public function generate($config)
226228
// Fields
227229
foreach ($propertiesMap[$type->getUri()] as $property) {
228230
// Ignore properties not set if using a config file
229-
if (isset($typeConfig['properties']) && is_array($typeConfig['properties']) && !array_key_exists($property->localName(), $typeConfig['properties'])) {
230-
continue;
231+
if ($typeConfig['allProperties'] || (is_array($typeConfig['properties']) && array_key_exists($property->localName(), $typeConfig['properties']))) {
232+
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
231233
}
232-
233-
$class = $this->generateField($config, $class, $type, $typeName, $property->localName(), $property);
234234
}
235235

236236
// Add custom fields (non schema.org)
@@ -266,8 +266,7 @@ public function generate($config)
266266
}
267267

268268
// When including all properties, ignore properties already set on parent
269-
$typeConfig = isset($config['types'][$class['name']]) ? $config['types'][$class['name']] : null;
270-
if ((!isset($typeConfig['properties']) || !is_array($typeConfig['properties'])) && $class['parent']) {
269+
if (isset($config['types'][$class['name']]['allProperties']) && $config['types'][$class['name']]['allProperties'] && $class['parent']) {
271270
$type = $class['resource'];
272271

273272
foreach ($propertiesMap[$type->getUri()] as $property) {

src/TypesGeneratorConfiguration.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ function ($rdfa) {
5858
->end()
5959
->arrayNode('relations')
6060
->info('OWL relation files to use')
61-
->defaultValue([
62-
self::GOOD_RELATIONS_OWL_URL,
63-
])
61+
->defaultValue([self::GOOD_RELATIONS_OWL_URL])
6462
->prototype('scalar')->end()
6563
->end()
6664
->booleanNode('debug')->defaultFalse()->info('Debug mode')->end()
@@ -88,6 +86,18 @@ function ($rdfa) {
8886
->scalarNode('author')->defaultFalse()->info('The value of the phpDoc\'s @author annotation')->example('Kévin Dunglas <[email protected]>')->end()
8987
->enumNode('fieldVisibility')->values(['private', 'protected', 'public'])->defaultValue('private')->cannotBeEmpty()->info('Visibility of entities fields')->end()
9088
->arrayNode('types')
89+
->beforeNormalization()
90+
->always()
91+
->then(function ($v) {
92+
foreach ($v as $key => $type) {
93+
if (!isset($type['properties'])) {
94+
$v[$key]['allProperties'] = true;
95+
}
96+
}
97+
98+
return $v;
99+
})
100+
->end()
91101
->info('Schema.org\'s types to use')
92102
->useAttributeAsKey('id')
93103
->prototype('array')
@@ -111,6 +121,7 @@ function ($rdfa) {
111121
->end()
112122
->scalarNode('parent')->defaultNull()->info('The parent class, set to false for a top level class')->end()
113123
->scalarNode('guessFrom')->defaultValue('Thing')->info('If declaring a custom class, this will be the class from which properties type will be guessed')->end()
124+
->booleanNode('allProperties')->defaultFalse()->info('Import all existing properties')->end()
114125
->arrayNode('properties')
115126
->info('Properties of this type to use')
116127
->useAttributeAsKey('id')

tests/TypesGeneratorTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ public function testGenerate()
3131
$twigProphecy->render('class.php.twig', Argument::type('array'))->willReturn();
3232
$twig = $twigProphecy->reveal();
3333

34-
$logger = new NullLogger();
35-
36-
$graphs = $this->getGraphs();
37-
3834
$cardinalitiesExtractorProphecy = $this->prophesize('ApiPlatform\SchemaGenerator\CardinalitiesExtractor');
3935
$cardinalities = $this->getCardinalities();
4036
$cardinalitiesExtractorProphecy->extract()->willReturn($cardinalities)->shouldBeCalled();
@@ -43,7 +39,7 @@ public function testGenerate()
4339
$goodRelationsBridgeProphecy = $this->prophesize('ApiPlatform\SchemaGenerator\GoodRelationsBridge');
4440
$goodRelationsBridge = $goodRelationsBridgeProphecy->reveal();
4541

46-
$typesGenerator = new TypesGenerator($twig, $logger, $graphs, $cardinalitiesExtractor, $goodRelationsBridge);
42+
$typesGenerator = new TypesGenerator($twig, new NullLogger(), $this->getGraphs(), $cardinalitiesExtractor, $goodRelationsBridge);
4743

4844
$typesGenerator->generate($this->getConfig());
4945
}
@@ -132,13 +128,15 @@ private function getConfig()
132128
'output' => 'build/type-generator-test',
133129
'types' => [
134130
'Article' => [
131+
'allProperties' => false,
135132
'properties' => [
136133
'articleBody' => null,
137134
'articleSection' => null,
138135
],
139136
'vocabularyNamespace' => TypesGenerator::SCHEMA_ORG_NAMESPACE,
140137
],
141138
'CreativeWork' => [
139+
'allProperties' => false,
142140
'properties' => [
143141
'author' => [
144142
'cardinality' => CardinalitiesExtractor::CARDINALITY_N_0,
@@ -151,22 +149,21 @@ private function getConfig()
151149
'vocabularyNamespace' => TypesGenerator::SCHEMA_ORG_NAMESPACE,
152150
],
153151
'BlogPosting' => [
152+
'allProperties' => true,
154153
'properties' => null,
155154
'vocabularyNamespace' => TypesGenerator::SCHEMA_ORG_NAMESPACE,
156155
],
157156
'Person' => [
158-
'properties' => [
159-
],
157+
'allProperties' => false,
158+
'properties' => [],
160159
'vocabularyNamespace' => TypesGenerator::SCHEMA_ORG_NAMESPACE,
161160
],
162161
'SocialMediaPosting' => [
163-
'properties' => null,
162+
'allProperties' => true,
164163
'vocabularyNamespace' => TypesGenerator::SCHEMA_ORG_NAMESPACE,
165164
],
166165
'Thing' => [
167-
'properties' => [
168-
'name' => null,
169-
],
166+
'allProperties' => true,
170167
'vocabularyNamespace' => TypesGenerator::SCHEMA_ORG_NAMESPACE,
171168
],
172169
],

0 commit comments

Comments
 (0)