Skip to content

Commit 55d13b9

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

31 files changed

+1430
-42
lines changed

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

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
public function __construct($uri = null, $data = null, $format = null, $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+
public function __call($methodName, $arguments)
50+
{
51+
$arguments = RdfResource::fromRdftoEasyRdfResources($arguments);
52+
53+
return RdfResource::wrapEasyRdfResource(\call_user_func_array([$this->graph, $methodName], $arguments));
54+
}
55+
56+
/**
57+
* Gets all the resources for a property of a resource and ensures that each
58+
* EasyRdf\Resource matched is returned as wrapped in an RdfResource.
59+
*/
60+
public function allResources($resource, $property): array
61+
{
62+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
63+
64+
return RdfResource::wrapEasyRdfResources($this->graph->allResources($resource, $property));
65+
}
66+
67+
/**
68+
* Gets all values for a property path and ensures that each EasyRdf\Resource
69+
* matched is returned as wrapped in an RdfResource.
70+
*/
71+
public function all($resource, $propertyPath, $type = null, $lang = null): array
72+
{
73+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
74+
75+
return RdfResource::wrapEasyRdfResources($this->graph->all($resource, $propertyPath, $type, $lang));
76+
}
77+
78+
/**
79+
* Gets all the resources in the graph of a certain type and ensures that
80+
* each EasyRdf\Resource matched is returned as wrapped in an RdfResource.
81+
*/
82+
public function allOfType($type): array
83+
{
84+
return RdfResource::wrapEasyRdfResources($this->graph->allOfType($type));
85+
}
86+
87+
/**
88+
* Gets all values for a single property of a resource and ensures that each
89+
* EasyRdf\Resource matched is returned as wrapped in an RdfResource.
90+
*/
91+
public function allForSingleProperty($resource, $property, $type = null, $lang = null): array
92+
{
93+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
94+
95+
return RdfResource::wrapEasyRdfResources($this->graph->allForSingleProperty($resource, $property, $type, $lang));
96+
}
97+
98+
/**
99+
* Gets the resource types of the graph as list of RdfResource.
100+
*/
101+
public function typesAsResources($resource = null): array
102+
{
103+
$resource = RdfResource::fromRdftoEasyRdfResource($resource);
104+
105+
return RdfResource::wrapEasyRdfResources($this->graph->typesAsResources($resource));
106+
}
107+
108+
/**
109+
* Gets an associative array of all the resources stored in the graph as
110+
* RdfResources. The keys of the array is the URI of the related RdfResource.
111+
*/
112+
public function resources(): array
113+
{
114+
return RdfResource::wrapEasyRdfResources($this->graph->resources());
115+
}
116+
117+
/**
118+
* Get an array of RdfResources matching a certain property and optional value.
119+
*/
120+
public function resourcesMatching($property, $value = null): array
121+
{
122+
return RdfResource::wrapEasyRdfResources($this->graph->resourcesMatching($property, $value));
123+
}
124+
125+
/**
126+
* Turns any provided EasyRdf\Graph into an RdfGraph.
127+
*/
128+
public static function fromEasyRdf(EasyRdfGraph $graph): self
129+
{
130+
$rdfGraph = new static(null, null, null, $graph);
131+
132+
return $rdfGraph;
133+
}
134+
135+
/**
136+
* Ensures that any EasyRdf\Graph provided by reference will be wrapped in
137+
* an RdfGraph.
138+
*
139+
* @param EasyRdfGraph|RdfGraph &$graph
140+
*/
141+
public static function ensureGraphClass(&$graph): void
142+
{
143+
$graph = ($graph instanceof EasyRdfGraph) ? self::fromEasyRdf($graph) : $graph;
144+
}
145+
146+
/**
147+
* Ensures that each EasyRdf\Graph, in an array of Graphs passed by reference,
148+
* is wrapped in an RdfGraph.
149+
*/
150+
public static function ensureGraphsClass(array &$graphs): void
151+
{
152+
array_walk($graphs, self::class.'::ensureGraphClass');
153+
}
154+
155+
/**
156+
* Statically creates a new RdfGraph and loads RDF data from the provided URI.
157+
*/
158+
public static function newAndLoad($uri, $format = null): self
159+
{
160+
$graph = new self($uri);
161+
$graph->load($uri, $format);
162+
163+
return $graph;
164+
}
165+
166+
public function __toString()
167+
{
168+
return $this->graph->__toString();
169+
}
170+
171+
public function __isset($name)
172+
{
173+
return $this->graph->__isset($name);
174+
}
175+
176+
public function __set($name, $value)
177+
{
178+
return $this->graph->__set($name, $value);
179+
}
180+
181+
public function __get($name)
182+
{
183+
return $this->graph->__get($name);
184+
}
185+
186+
public function __unset($name)
187+
{
188+
return $this->graph->__unset($name);
189+
}
190+
}

0 commit comments

Comments
 (0)