forked from api-platform/schema-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpTypeConverter.php
96 lines (78 loc) · 2.66 KB
/
PhpTypeConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\SchemaGenerator;
use EasyRdf\Resource;
final class PhpTypeConverter implements PhpTypeConverterInterface
{
/**
* Is this type a datatype?
*/
public function isDatatype(Resource $range): bool
{
return isset(PhpTypeConverterInterface::BASE_MAPPING[$this->getUri($range)]) || $this->isLangString($range);
}
public function getPhpType(array $field, array $config = [], array $classes = []): ?string
{
if ($field['isArray'] ?? false) {
return ($config['doctrine']['useCollection'] ?? false) && !$this->isDatatype($field['range']) ? 'Collection' : 'array';
}
return $this->getNonArrayType($field, $classes);
}
public function escapeIdentifier(string $identifier): string
{
foreach (self::RESERVED_KEYWORDS as $keyword) {
if (0 === strcasecmp($keyword, $identifier)) {
return $identifier.'_';
}
}
return $identifier;
}
private function getNonArrayType(array $field, array $classes): ?string
{
if ($field['isEnum']) {
return 'string';
}
if (null === $field['range']) {
return null;
}
$rangeUri = $this->getUri($field['range']);
if (isset(PhpTypeConverterInterface::BASE_MAPPING[$rangeUri])) {
return PhpTypeConverterInterface::BASE_MAPPING[$rangeUri];
}
$typeName = $field['rangeName'];
if ($type = $classes[$typeName]['interfaceName'] ?? $classes[$typeName]['name'] ?? null) {
return $type;
}
if ($this->isLangString($field['range'])) {
return 'string';
}
return null;
}
/**
* This is a hack to detect internationalized strings in ActivityStreams.
*
* @todo find something smarter to detect this kind of strings
*/
private function isLangString(Resource $range): bool
{
return $range->isBNode() &&
null !== ($unionOf = $range->get('owl:unionOf')) &&
null !== ($rdfFirst = $unionOf->get('rdf:first')) &&
'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString' === $rdfFirst->getUri();
}
private function getUri(Resource $range): string
{
if ($range->isBNode() && $onDatatype = $range->get('owl:onDatatype')) {
return $onDatatype->getUri();
}
return $range->getUri();
}
}