-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathCardinalitiesExtractor.php
136 lines (114 loc) · 4.06 KB
/
CardinalitiesExtractor.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Graph as RdfGraph;
use EasyRdf\Resource as RdfResource;
/**
* Extracts cardinalities from the OWL definition, from GoodRelations or from Schema.org's comments.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class CardinalitiesExtractor
{
public const CARDINALITY_0_1 = '(0..1)';
public const CARDINALITY_0_N = '(0..*)';
public const CARDINALITY_1_1 = '(1..1)';
public const CARDINALITY_1_N = '(1..*)';
public const CARDINALITY_N_0 = '(*..0)';
public const CARDINALITY_N_1 = '(*..1)';
public const CARDINALITY_N_N = '(*..*)';
public const CARDINALITY_UNKNOWN = 'unknown';
private GoodRelationsBridge $goodRelationsBridge;
public function __construct(GoodRelationsBridge $goodRelationsBridge)
{
$this->goodRelationsBridge = $goodRelationsBridge;
}
/**
* Extracts cardinality of properties.
*
* @param RdfGraph[] $graphs
*
* @return array<string, string>
*/
public function extract(array $graphs): array
{
$properties = [];
foreach ($graphs as $graph) {
foreach (TypesGenerator::$propertyTypes as $propertyType) {
/** @var RdfResource $property */
foreach ($graph->allOfType($propertyType) as $property) {
if ($property->isBNode()) {
continue;
}
$properties[$property->getUri()] = $this->extractForProperty($property);
}
}
}
return $properties;
}
/**
* Extracts the cardinality of a property.
*
* Based on [Geraint Luff work](https://github.com/geraintluff/schema-org-gen).
*
* @return string The cardinality
*/
private function extractForProperty(RdfResource $property): string
{
$minCardinality = $property->get('owl:minCardinality');
$maxCardinality = $property->get('owl:maxCardinality');
if (null !== $minCardinality && null !== $maxCardinality && $minCardinality >= 1 && $maxCardinality <= 1) {
return self::CARDINALITY_1_1;
}
if ((null !== $minCardinality) && $minCardinality >= 1) {
return self::CARDINALITY_1_N;
}
if ((null !== $maxCardinality) && $maxCardinality <= 1) {
return self::CARDINALITY_0_1;
}
if ($property->isA('owl:FunctionalProperty')) {
return self::CARDINALITY_0_1;
}
if ($property->isA('owl:InverseFunctionalProperty')) {
return self::CARDINALITY_0_N;
}
if (!str_starts_with($property->getUri(), 'https://schema.org')) {
return self::CARDINALITY_UNKNOWN;
}
if (!\is_string($localName = $property->localName())) {
return self::CARDINALITY_UNKNOWN;
}
$fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName);
if (false !== $fromGoodRelations) {
return $fromGoodRelations;
}
if (!$rdfsComment = $property->get('rdfs:comment')) {
return self::CARDINALITY_UNKNOWN;
}
$comment = $rdfsComment->getValue();
if (
// https://schema.org/acceptedOffer, https://schema.org/acceptedPaymentMethod, https://schema.org/exerciseType
preg_match('/\(s\)/', $comment)
// https://schema.org/follows
|| preg_match('/^The most generic uni-directional social relation./', $comment)
|| preg_match('/one or more/i', $comment)
) {
return self::CARDINALITY_0_N;
}
if (
preg_match('/^is/', $localName)
|| preg_match('/^The /', $comment)
) {
return self::CARDINALITY_0_1;
}
return self::CARDINALITY_UNKNOWN;
}
}