Skip to content

Commit 1063eea

Browse files
authored
Merge pull request #198 from dunglas/fix/schema-org
fix: support the latest version of Schema.org
2 parents 97289ed + 72d13c9 commit 1063eea

19 files changed

+192
-183
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/vendor/
44
/*.phar
55
/.phpunit.result.cache
6+
/tmp

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": ">=7.4",
2222
"ext-json": "*",
2323
"doctrine/inflector": "^1.4.3 || ^2.0",
24-
"easyrdf/easyrdf": "^0.9",
24+
"easyrdf/easyrdf": "dev-master",
2525
"friendsofphp/php-cs-fixer": "^2.15",
2626
"league/html-to-markdown": "^4.9",
2727
"psr/log": "^1.0",
@@ -32,7 +32,7 @@
3232
},
3333
"require-dev": {
3434
"api-platform/core": "^2.6",
35-
"doctrine/orm": "^2.8",
35+
"doctrine/orm": "^2.7",
3636
"myclabs/php-enum": "^1.7",
3737
"symfony/doctrine-bridge": "^5.1",
3838
"symfony/filesystem": "^5.1",

composer.lock

+29-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AnnotationGenerator/AbstractAnnotationGenerator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\SchemaGenerator\AnnotationGenerator;
1515

1616
use Doctrine\Inflector\Inflector;
17+
use EasyRdf\Graph;
1718
use Psr\Log\LoggerInterface;
1819

1920
/**
@@ -34,7 +35,7 @@ abstract class AbstractAnnotationGenerator implements AnnotationGeneratorInterfa
3435
protected $logger;
3536

3637
/**
37-
* @var \EasyRdf_Graph[]
38+
* @var Graph[]
3839
*/
3940
protected $graphs;
4041

src/AnnotationGenerator/AnnotationGeneratorInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\SchemaGenerator\AnnotationGenerator;
1515

1616
use Doctrine\Inflector\Inflector;
17+
use EasyRdf\Graph;
1718
use Psr\Log\LoggerInterface;
1819

1920
/**
@@ -24,7 +25,7 @@
2425
interface AnnotationGeneratorInterface
2526
{
2627
/**
27-
* @param \EasyRdf_Graph[] $graphs
28+
* @param Graph[] $graphs
2829
*/
2930
public function __construct(Inflector $inflector, LoggerInterface $logger, array $graphs, array $cardinalities, array $config, array $classes);
3031

src/AnnotationGenerator/DoctrineOrmAnnotationGenerator.php

+70-52
Original file line numberDiff line numberDiff line change
@@ -140,64 +140,78 @@ public function generateFieldAnnotations(string $className, string $fieldName):
140140
}
141141

142142
$annotations[] = $annotation;
143-
} elseif ($field['isEmbedded']) {
143+
144+
return $annotations;
145+
}
146+
147+
$relationName = $field['range'] ? $this->getRelationName($field['range']) : null;
148+
if ($field['isEmbedded']) {
144149
$columnPrefix = ', columnPrefix=';
145150
if (\is_bool($field['columnPrefix'])) {
146151
$columnPrefix .= $field['columnPrefix'] ? 'true' : 'false';
147152
} else {
148153
$columnPrefix .= sprintf('"%s"', $field['columnPrefix']);
149154
}
150-
$annotations[] = sprintf('@ORM\Embedded(class="%s"%s)', $this->getRelationName($field['range']), $columnPrefix);
151-
} else {
152-
switch ($field['cardinality']) {
153-
case CardinalitiesExtractor::CARDINALITY_0_1:
154-
$annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $this->getRelationName($field['range']));
155-
break;
156-
case CardinalitiesExtractor::CARDINALITY_1_1:
157-
$annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $this->getRelationName($field['range']));
158-
$annotations[] = '@ORM\JoinColumn(nullable=false)';
159-
break;
160-
case CardinalitiesExtractor::CARDINALITY_UNKNOWN:
161-
case CardinalitiesExtractor::CARDINALITY_N_0:
162-
if ($field['inversedBy'] ?? false) {
163-
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $this->getRelationName($field['range']), $field['inversedBy']);
164-
} else {
165-
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $this->getRelationName($field['range']));
166-
}
167-
break;
168-
case CardinalitiesExtractor::CARDINALITY_N_1:
169-
if ($field['inversedBy'] ?? false) {
170-
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $this->getRelationName($field['range']), $field['inversedBy']);
171-
} else {
172-
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $this->getRelationName($field['range']));
173-
}
174-
$annotations[] = '@ORM\JoinColumn(nullable=false)';
175-
break;
176-
case CardinalitiesExtractor::CARDINALITY_0_N:
177-
if ($field['mappedBy'] ?? false) {
178-
$annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $this->getRelationName($field['range']), $field['mappedBy']);
179-
} else {
180-
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range']));
181-
}
182-
$name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : '';
183-
$annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(unique=true)})';
184-
break;
185-
case CardinalitiesExtractor::CARDINALITY_1_N:
186-
if ($field['mappedBy'] ?? false) {
187-
$annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $this->getRelationName($field['range']), $field['mappedBy']);
188-
} else {
189-
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range']));
190-
}
191-
$name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : '';
192-
$annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(nullable=false, unique=true)})';
193-
break;
194-
case CardinalitiesExtractor::CARDINALITY_N_N:
195-
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $this->getRelationName($field['range']));
196-
if ($field['relationTableName']) {
197-
$annotations[] = sprintf('@ORM\JoinTable(name="%s")', $field['relationTableName']);
198-
}
199-
break;
155+
156+
if ($relationName) {
157+
$annotations[] = sprintf('@ORM\Embedded(class="%s"%s)', $relationName, $columnPrefix);
200158
}
159+
160+
return $annotations;
161+
}
162+
163+
if (!$relationName) {
164+
return $annotations;
165+
}
166+
167+
switch ($field['cardinality']) {
168+
case CardinalitiesExtractor::CARDINALITY_0_1:
169+
$annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $relationName);
170+
break;
171+
case CardinalitiesExtractor::CARDINALITY_1_1:
172+
$annotations[] = sprintf('@ORM\OneToOne(targetEntity="%s")', $relationName);
173+
$annotations[] = '@ORM\JoinColumn(nullable=false)';
174+
break;
175+
case CardinalitiesExtractor::CARDINALITY_UNKNOWN:
176+
case CardinalitiesExtractor::CARDINALITY_N_0:
177+
if ($field['inversedBy'] ?? false) {
178+
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $relationName, $field['inversedBy']);
179+
} else {
180+
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $relationName);
181+
}
182+
break;
183+
case CardinalitiesExtractor::CARDINALITY_N_1:
184+
if ($field['inversedBy'] ?? false) {
185+
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s", inversedBy="%s")', $relationName, $field['inversedBy']);
186+
} else {
187+
$annotations[] = sprintf('@ORM\ManyToOne(targetEntity="%s")', $relationName);
188+
}
189+
$annotations[] = '@ORM\JoinColumn(nullable=false)';
190+
break;
191+
case CardinalitiesExtractor::CARDINALITY_0_N:
192+
if ($field['mappedBy'] ?? false) {
193+
$annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $relationName, $field['mappedBy']);
194+
} else {
195+
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $relationName);
196+
}
197+
$name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : '';
198+
$annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(unique=true)})';
199+
break;
200+
case CardinalitiesExtractor::CARDINALITY_1_N:
201+
if ($field['mappedBy'] ?? false) {
202+
$annotations[] = sprintf('@ORM\OneToMany(targetEntity="%s", mappedBy="%s")', $relationName, $field['mappedBy']);
203+
} else {
204+
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $relationName);
205+
}
206+
$name = $field['relationTableName'] ? sprintf('name="%s", ', $field['relationTableName']) : '';
207+
$annotations[] = '@ORM\JoinTable('.$name.'inverseJoinColumns={@ORM\JoinColumn(nullable=false, unique=true)})';
208+
break;
209+
case CardinalitiesExtractor::CARDINALITY_N_N:
210+
$annotations[] = sprintf('@ORM\ManyToMany(targetEntity="%s")', $relationName);
211+
if ($field['relationTableName']) {
212+
$annotations[] = sprintf('@ORM\JoinTable(name="%s")', $field['relationTableName']);
213+
}
214+
break;
201215
}
202216

203217
return $annotations;
@@ -243,8 +257,12 @@ private function generateIdAnnotations(): array
243257
/**
244258
* Gets class or interface name to use in relations.
245259
*/
246-
private function getRelationName(string $range): string
260+
private function getRelationName(string $range): ?string
247261
{
262+
if (!isset($this->classes[$range])) {
263+
return null;
264+
}
265+
248266
$class = $this->classes[$range];
249267

250268
if (isset($class['interfaceName'])) {

src/CardinalitiesExtractor.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace ApiPlatform\SchemaGenerator;
1515

16+
use EasyRdf\Graph;
17+
use EasyRdf\Resource;
18+
1619
/**
1720
* Cardinality extractor.
1821
*
@@ -30,14 +33,14 @@ class CardinalitiesExtractor
3033
public const CARDINALITY_UNKNOWN = 'unknown';
3134

3235
/**
33-
* @var \EasyRdf_Graph[]
36+
* @var Graph[]
3437
*/
3538
private $graphs;
3639

3740
private $goodRelationsBridge;
3841

3942
/**
40-
* @param \EasyRdf_Graph[] $graphs
43+
* @param Graph[] $graphs
4144
*/
4245
public function __construct(array $graphs, GoodRelationsBridge $goodRelationsBridge)
4346
{
@@ -68,7 +71,7 @@ public function extract(): array
6871
*
6972
* @return string The cardinality
7073
*/
71-
private function extractForProperty(\EasyRdf_Resource $property): string
74+
private function extractForProperty(Resource $property): string
7275
{
7376
$localName = $property->localName();
7477
$fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName);

0 commit comments

Comments
 (0)