Skip to content

Commit f67439a

Browse files
committed
feat: Allow schema generator to use labels for naming API resources and PHP classes
1 parent 997f6f8 commit f67439a

32 files changed

+1595
-43
lines changed

src/AttributeGenerator/ConfigurationAttributeGenerator.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public function generateClassAttributes(Class_ $class): array
2525
{
2626
$typeAttributes = $this->config['types'][$class->name()]['attributes'] ?? [[]];
2727
$vocabAttributes = [[]];
28+
2829
if ($class instanceof SchemaClass) {
29-
$vocabAttributes = $this->config['vocabularies'][$class->resource()->getGraph()->getUri()]['attributes'] ?? [[]];
30+
$graphUri = (null !== $class->resource()->getGraph()) ? $class->resource()->getGraph()->getUri() : null;
31+
$vocabAttributes = $this->config['vocabularies'][$graphUri]['attributes'] ?? [[]];
3032
}
3133

3234
$getAttributesNames = static fn (array $config) => $config === [[]]

src/CardinalitiesExtractor.php

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

1414
namespace ApiPlatform\SchemaGenerator;
1515

16-
use EasyRdf\Graph as RdfGraph;
17-
use EasyRdf\Resource as RdfResource;
16+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
17+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
1818

1919
/**
2020
* Extracts cardinalities from the OWL definition, from GoodRelations or from Schema.org's comments.
@@ -104,7 +104,7 @@ private function extractForProperty(RdfResource $property): string
104104
return self::CARDINALITY_UNKNOWN;
105105
}
106106

107-
$fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName);
107+
$fromGoodRelations = $this->goodRelationsBridge->extractCardinality($property->localId());
108108
if (false !== $fromGoodRelations) {
109109
return $fromGoodRelations;
110110
}

src/ClassMutator/AnnotationsAppender.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use ApiPlatform\SchemaGenerator\AnnotationGenerator\AnnotationGeneratorInterface;
1717
use ApiPlatform\SchemaGenerator\Model\Class_;
1818
use ApiPlatform\SchemaGenerator\Model\Use_;
19-
use EasyRdf\Resource as RdfResource;
19+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
2020

2121
final class AnnotationsAppender implements ClassMutatorInterface
2222
{

src/ClassMutator/ClassPropertiesAppender.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use ApiPlatform\SchemaGenerator\Model\Class_;
1717
use ApiPlatform\SchemaGenerator\PropertyGenerator\PropertyGeneratorInterface;
1818
use ApiPlatform\SchemaGenerator\Schema\Model\Class_ as SchemaClass;
19-
use EasyRdf\Graph as RdfGraph;
20-
use EasyRdf\Resource as RdfResource;
19+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
20+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
2121
use Psr\Log\LoggerAwareTrait;
2222

2323
final class ClassPropertiesAppender implements ClassMutatorInterface

src/Command/ExtractCardinalitiesCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use ApiPlatform\SchemaGenerator\CardinalitiesExtractor;
1717
use ApiPlatform\SchemaGenerator\GoodRelationsBridge;
18+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
1819
use ApiPlatform\SchemaGenerator\SchemaGeneratorConfiguration;
19-
use EasyRdf\Graph as RdfGraph;
2020
use Symfony\Component\Console\Command\Command;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Symfony\Component\Console\Input\InputOption;

src/Schema/ClassMutator/EnumClassMutator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use ApiPlatform\SchemaGenerator\Model\Class_;
1818
use ApiPlatform\SchemaGenerator\PhpTypeConverterInterface;
1919
use ApiPlatform\SchemaGenerator\Schema\Model\Constant as SchemaConstant;
20-
use EasyRdf\Graph as RdfGraph;
20+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
2121

2222
final class EnumClassMutator extends BaseEnumClassMutator
2323
{

src/Schema/Generator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
use ApiPlatform\SchemaGenerator\GoodRelationsBridge;
1919
use ApiPlatform\SchemaGenerator\PhpTypeConverter;
2020
use ApiPlatform\SchemaGenerator\Printer;
21+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
2122
use ApiPlatform\SchemaGenerator\TwigBuilder;
2223
use ApiPlatform\SchemaGenerator\TypesGenerator;
23-
use EasyRdf\Graph as RdfGraph;
2424
use Symfony\Component\Console\Logger\ConsoleLogger;
2525
use Symfony\Component\Console\Output\OutputInterface;
2626
use Symfony\Component\Console\Style\SymfonyStyle;

src/Schema/Model/Class_.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace ApiPlatform\SchemaGenerator\Schema\Model;
1515

1616
use ApiPlatform\SchemaGenerator\Model\Class_ as BaseClass_;
17-
use EasyRdf\Resource as RdfResource;
17+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
1818

1919
final class Class_ extends BaseClass_
2020
{

src/Schema/Model/Constant.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace ApiPlatform\SchemaGenerator\Schema\Model;
1515

1616
use ApiPlatform\SchemaGenerator\Model\Constant as BaseConstant;
17-
use EasyRdf\Resource as RdfResource;
17+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
1818

1919
final class Constant extends BaseConstant
2020
{

src/Schema/Model/Property.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace ApiPlatform\SchemaGenerator\Schema\Model;
1515

1616
use ApiPlatform\SchemaGenerator\Model\Property as BaseProperty;
17-
use EasyRdf\Resource as RdfResource;
17+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
1818

1919
final class Property extends BaseProperty
2020
{

src/Schema/PropertyGenerator/IdPropertyGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use ApiPlatform\SchemaGenerator\PropertyGenerator\IdPropertyGeneratorInterface;
2020
use ApiPlatform\SchemaGenerator\Schema\Model\Property as SchemaProperty;
2121
use ApiPlatform\SchemaGenerator\Schema\Model\Type\PrimitiveType as SchemaPrimitiveType;
22-
use EasyRdf\Resource as RdfResource;
22+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
2323

2424
final class IdPropertyGenerator implements IdPropertyGeneratorInterface
2525
{

src/Schema/PropertyGenerator/PropertyGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
use ApiPlatform\SchemaGenerator\PropertyGenerator\PropertyGeneratorInterface;
2424
use ApiPlatform\SchemaGenerator\Schema\Model\Property as SchemaProperty;
2525
use ApiPlatform\SchemaGenerator\Schema\Model\Type\PrimitiveType;
26+
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
2627
use ApiPlatform\SchemaGenerator\Schema\TypeConverter;
27-
use EasyRdf\Resource as RdfResource;
2828
use Psr\Log\LoggerAwareTrait;
2929

3030
final class PropertyGenerator implements PropertyGeneratorInterface

src/Schema/Rdf/RdfGraph.php

+251
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\SchemaGenerator\Schema\Rdf;
15+
16+
use EasyRdf\Graph as EasyRdfGraph;
17+
18+
/**
19+
* This class is a wrapper around the EasyRdf\Graph class. It allows the Schema
20+
* Generator to get RdfResource objects instead of EasyRdf\Resource ones when required.
21+
*
22+
* @author d3fk::Angatar
23+
*/
24+
class RdfGraph
25+
{
26+
private EasyRdfGraph $graph;
27+
28+
/**
29+
* Constructor, creates the RdfGraph decorating a freshly created or given
30+
* EasyRdf\Graph with the capability to return/use RdfResource instead of EasyRdf\Resource.
31+
*/
32+
final public function __construct(string $uri = null, string $data = null, string $format = null, EasyRdfGraph $graph = null)
33+
{
34+
$this->graph = $graph ?? new EasyRdfGraph($uri, $data, $format);
35+
}
36+
37+
/**
38+
* Returns the corresponding EasyRdf\Graph.
39+
*/
40+
public function getEasyGraph(): EasyRdfGraph
41+
{
42+
return $this->graph;
43+
}
44+
45+
/**
46+
* Passes any call for an absent method to the contained EasyRdf\Graph, ensuring
47+
* that it returns a Schema Generator's RdfResource in place of any EasyRdf\Resource.
48+
*
49+
* @param array<mixed> $arguments
50+
*
51+
* @return mixed depending on the method called
52+
*/
53+
#[\ReturnTypeWillChange]
54+
public function __call(string $methodName, array $arguments)
55+
{
56+
$arguments = RdfResource::fromRdftoEasyRdfResources($arguments);
57+
$callback = [$this->graph, $methodName];
58+
if (\is_callable($callback)) {
59+
return RdfResource::wrapEasyRdfResource(\call_user_func_array($callback, $arguments));
60+
}
61+
throw new \Exception('Method not found');
62+
}
63+
64+
/**
65+
* Gets all the resources for a property of a resource and ensures that each
66+
* EasyRdf\Resource matched is returned as wrapped in an RdfResource.
67+
*
68+
* @return array<string>
69+
*/
70+
public function allResources(string $resource, string $property): array
71+
{
72+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
73+
74+
return RdfResource::wrapEasyRdfResources($this->graph->allResources($resource, $property));
75+
}
76+
77+
/**
78+
* Gets all values for a property path and ensures that each EasyRdf\Resource
79+
* matched is returned as wrapped in an RdfResource.
80+
*
81+
* @return array<string>
82+
*/
83+
public function all(string $resource, string $propertyPath, string $type = null, string $lang = null): array
84+
{
85+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
86+
87+
return RdfResource::wrapEasyRdfResources($this->graph->all($resource, $propertyPath, $type, $lang));
88+
}
89+
90+
/**
91+
* Gets all the resources in the graph of a certain type and ensures that
92+
* each EasyRdf\Resource matched is returned as wrapped in an RdfResource.
93+
*
94+
* @return array<mixed>
95+
*/
96+
public function allOfType(string $type): array
97+
{
98+
return RdfResource::wrapEasyRdfResources($this->graph->allOfType($type));
99+
}
100+
101+
/**
102+
* Gets the resource types of the graph as list of RdfResource.
103+
*
104+
* @param string|null $resource
105+
*
106+
* @return RdfResource[]
107+
*/
108+
public function typesAsResources($resource = null): array
109+
{
110+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
111+
112+
return RdfResource::wrapEasyRdfResources($this->graph->typesAsResources($resource));
113+
}
114+
115+
/**
116+
* Gets an associative array of all the resources stored in the graph as
117+
* RdfResources. The keys of the array is the URI of the related RdfResource.
118+
*
119+
* @return RdfResource[]
120+
*/
121+
public function resources(): array
122+
{
123+
return RdfResource::wrapEasyRdfResources($this->graph->resources());
124+
}
125+
126+
/**
127+
* Get an array of RdfResources matching a certain property and optional value.
128+
*
129+
* @param string $property the property to check
130+
* @param mixed $value optional, the value of the propery to check for
131+
*
132+
* @return RdfResource[]
133+
*/
134+
public function resourcesMatching($property, $value = null): array
135+
{
136+
return RdfResource::wrapEasyRdfResources($this->graph->resourcesMatching($property, $value));
137+
}
138+
139+
/**
140+
* Turns any provided EasyRdf\Graph into an RdfGraph.
141+
*/
142+
public static function fromEasyRdf(EasyRdfGraph $graph): self
143+
{
144+
$rdfGraph = new static(null, null, null, $graph);
145+
146+
return $rdfGraph;
147+
}
148+
149+
/**
150+
* Ensures that any EasyRdf\Graph provided by reference will be wrapped in
151+
* an RdfGraph.
152+
*
153+
* @param EasyRdfGraph|RdfGraph &$graph
154+
*/
155+
public static function ensureGraphClass(&$graph): void
156+
{
157+
$graph = ($graph instanceof EasyRdfGraph) ? self::fromEasyRdf($graph) : $graph;
158+
}
159+
160+
/**
161+
* Ensures that each EasyRdf\Graph, in an array of Graphs passed by reference,
162+
* is wrapped in an RdfGraph.
163+
*
164+
* @param array<EasyRdfGraph|RdfGraph> &$graphs
165+
*/
166+
public static function ensureGraphsClass(array &$graphs): void
167+
{
168+
array_walk($graphs, self::class.'::ensureGraphClass');
169+
}
170+
171+
/**
172+
* Statically creates a new RdfGraph and loads RDF data from the provided URI.
173+
*/
174+
public static function newAndLoad(string $uri, string $format = null): self
175+
{
176+
$graph = new self($uri);
177+
$graph->load($uri, $format);
178+
179+
return $graph;
180+
}
181+
182+
public function __toString(): string
183+
{
184+
return $this->graph->__toString();
185+
}
186+
187+
public function __isset(string $name): bool
188+
{
189+
return $this->graph->__isset($name);
190+
}
191+
192+
public function __set(string $name, string $value): void
193+
{
194+
$this->graph->__set($name, $value);
195+
}
196+
197+
public function __get(string $name): ?string
198+
{
199+
return $this->graph->__get($name);
200+
}
201+
202+
public function __unset(string $name): void
203+
{
204+
$this->graph->__unset($name);
205+
}
206+
207+
// The following Methods are not required but avoid PHPStan special cases of
208+
// class Reflection without implementing extensions; see __call()
209+
210+
/**
211+
* Get the URI of the EasyRdf\Graph.
212+
*/
213+
public function getUri(): ?string
214+
{
215+
return $this->graph->getUri();
216+
}
217+
218+
/** Get or create a resource stored in a graph.
219+
*
220+
* If the resource did not previously exist, then a new resource will
221+
* be created.
222+
*
223+
* If URI is null, then the URI of the graph is used.
224+
*
225+
* @param string $uri The URI of the resource
226+
* @param mixed $types RDF type of a new resource (e.g. foaf:Person)
227+
*/
228+
public function resource($uri = null, $types = []): RdfResource
229+
{
230+
return RdfResource::wrapEasyRdfResource($this->graph->resource($uri, $types));
231+
}
232+
233+
public function load(string $uri = null, string $format = null): int
234+
{
235+
return $this->graph->load($uri, $format);
236+
}
237+
238+
/**
239+
* Parse a file containing RDF data into the graph object.
240+
*
241+
* @param string $filename The path of the file to load
242+
* @param string $format Optional format of the file
243+
* @param string $uri The URI of the file to load
244+
*
245+
* @return int The number of triples added to the graph
246+
*/
247+
public function parseFile($filename, $format = null, $uri = null): int
248+
{
249+
return $this->graph->parseFile($filename, $format, $uri);
250+
}
251+
}

0 commit comments

Comments
 (0)