Skip to content

Commit 9ecfdc8

Browse files
authored
refactor: better handling of data types (#377)
1 parent e634e2f commit 9ecfdc8

File tree

6 files changed

+16
-50
lines changed

6 files changed

+16
-50
lines changed

src/OpenApi/PhpTypeConverter.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515

1616
use ApiPlatform\SchemaGenerator\Model\Property;
1717
use ApiPlatform\SchemaGenerator\PhpTypeConverterInterface;
18-
use EasyRdf\Resource as RdfResource;
1918

2019
final class PhpTypeConverter implements PhpTypeConverterInterface
2120
{
22-
public function isDatatype(RdfResource $range): bool
23-
{
24-
return true;
25-
}
26-
2721
public function getPhpType(Property $property, array $config = [], array $classes = []): ?string
2822
{
2923
if ($property->reference && $property->isArray) {

src/PhpTypeConverter.php

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717
use ApiPlatform\SchemaGenerator\Model\Property;
1818
use ApiPlatform\SchemaGenerator\Schema\Model\Property as SchemaProperty;
1919
use ApiPlatform\SchemaGenerator\Schema\TypeConverter;
20-
use EasyRdf\Resource as RdfResource;
2120

2221
final class PhpTypeConverter implements PhpTypeConverterInterface
2322
{
24-
private const RDF_LANG_STRING = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
23+
private TypeConverter $typeConverter;
2524

26-
/**
27-
* Is this type a datatype?
28-
*/
29-
public function isDatatype(RdfResource $range): bool
25+
public function __construct()
3026
{
31-
return isset(TypeConverter::RANGE_MAPPING[$this->getUri($range)]) || $this->isLangString($range);
27+
$this->typeConverter = new TypeConverter();
3228
}
3329

3430
public function getPhpType(Property $property, array $config = [], array $classes = []): ?string
@@ -38,7 +34,7 @@ public function getPhpType(Property $property, array $config = [], array $classe
3834
}
3935

4036
if ($property->isArray && $property->range) {
41-
return ($config['doctrine']['useCollection'] ?? false) && !$this->isDatatype($property->range) ? 'Collection' : 'array';
37+
return ($config['doctrine']['useCollection'] ?? false) && !$this->typeConverter->getType($property->range) ? 'Collection' : 'array';
4238
}
4339

4440
return $this->getNonArrayType($property, $classes);
@@ -101,33 +97,6 @@ private function getNonArrayType(SchemaProperty $property, array $classes): ?str
10197
return $type;
10298
}
10399

104-
if ($this->isLangString($property->range)) {
105-
return 'string';
106-
}
107-
108100
return null;
109101
}
110-
111-
/**
112-
* This is a hack to detect internationalized strings.
113-
*
114-
* @todo find something smarter to detect this kind of strings
115-
*/
116-
private function isLangString(RdfResource $range): bool
117-
{
118-
return self::RDF_LANG_STRING === $this->getUri($range)
119-
|| ($range->isBNode() &&
120-
null !== ($unionOf = $range->get('owl:unionOf')) &&
121-
null !== ($rdfFirst = $unionOf->get('rdf:first')) &&
122-
self::RDF_LANG_STRING === $rdfFirst->getUri());
123-
}
124-
125-
private function getUri(RdfResource $range): string
126-
{
127-
if ($range->isBNode() && $onDatatype = $range->get('owl:onDatatype')) {
128-
return $onDatatype->getUri();
129-
}
130-
131-
return $range->getUri();
132-
}
133102
}

src/PhpTypeConverterInterface.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use ApiPlatform\SchemaGenerator\Model\Class_;
1717
use ApiPlatform\SchemaGenerator\Model\Property;
18-
use EasyRdf\Resource as RdfResource;
1918

2019
interface PhpTypeConverterInterface
2120
{
@@ -27,11 +26,6 @@ interface PhpTypeConverterInterface
2726
'collection', // Doctrine
2827
];
2928

30-
/**
31-
* Is this type a datatype?
32-
*/
33-
public function isDatatype(RdfResource $range): bool;
34-
3529
/**
3630
* Gets the PHP type of this field.
3731
*

src/Schema/PropertyGenerator/PropertyGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function __invoke(string $name, array $config, Class_ $class, array $cont
177177
private function getRanges(RdfResource $range, ?array $propertyConfig, array $config): array
178178
{
179179
$localName = $range->localName();
180-
$dataType = $this->phpTypeConverter->isDatatype($range);
180+
$dataType = (bool) $this->typeConverter->getType($range);
181181
$ranges = [];
182182
if (!$dataType && $range->isBNode()) {
183183
if (null !== ($unionOf = $range->get('owl:unionOf'))) {

src/Schema/TypeConverter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
final class TypeConverter
1919
{
20-
public const RANGE_MAPPING = [
20+
public const RANGE_DATA_TYPE_MAPPING = [
2121
'https://schema.org/URL' => 'url',
2222

2323
'https://schema.org/Boolean' => 'boolean',
@@ -89,6 +89,8 @@ final class TypeConverter
8989
'http://www.w3.org/2001/XMLSchema#ENTITY' => 'string',
9090
'http://www.w3.org/2001/XMLSchema#ENTITIES' => 'string',
9191

92+
'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString' => 'string',
93+
9294
'https://schema.org/DataType' => 'mixed',
9395
];
9496

@@ -98,7 +100,7 @@ public function getType(?RdfResource $range): ?string
98100
return null;
99101
}
100102

101-
return self::RANGE_MAPPING[$this->getUri($range)] ?? null;
103+
return self::RANGE_DATA_TYPE_MAPPING[$this->getUri($range)] ?? null;
102104
}
103105

104106
private function getUri(RdfResource $range): string
@@ -107,6 +109,12 @@ private function getUri(RdfResource $range): string
107109
return $onDatatype->getUri();
108110
}
109111

112+
if ($range->isBNode() &&
113+
null !== ($unionOf = $range->get('owl:unionOf')) &&
114+
null !== ($rdfFirst = $unionOf->get('rdf:first'))) {
115+
return $rdfFirst->getUri();
116+
}
117+
110118
return $range->getUri();
111119
}
112120
}

tests/Command/GenerateCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public function testActivityStreams(): void
442442
*
443443
* @see http://www.w3.org/ns/activitystreams#content
444444
*/
445+
#[ORM\Column(type: 'text', nullable: true)]
445446
#[ApiProperty(iri: 'http://www.w3.org/ns/activitystreams#content')]
446447
private ?string $content = null;
447448
PHP

0 commit comments

Comments
 (0)