Skip to content

Commit

Permalink
feat: Allow schema generator to use labels for naming API resources a…
Browse files Browse the repository at this point in the history
…nd PHP classes
  • Loading branch information
Angatar committed Dec 11, 2023
1 parent 997f6f8 commit 55d13b9
Show file tree
Hide file tree
Showing 31 changed files with 1,430 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/CardinalitiesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\SchemaGenerator;

use EasyRdf\Graph as RdfGraph;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;

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

$fromGoodRelations = $this->goodRelationsBridge->extractCardinality($localName);
$fromGoodRelations = $this->goodRelationsBridge->extractCardinality($property->localId());

Check failure on line 107 in src/CardinalitiesExtractor.php

View workflow job for this annotation

GitHub Actions / Build and Test (8.3)

Parameter #1 $id of method ApiPlatform\SchemaGenerator\GoodRelationsBridge::extractCardinality() expects string, string|null given.
if (false !== $fromGoodRelations) {
return $fromGoodRelations;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ClassMutator/AnnotationsAppender.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use ApiPlatform\SchemaGenerator\AnnotationGenerator\AnnotationGeneratorInterface;
use ApiPlatform\SchemaGenerator\Model\Class_;
use ApiPlatform\SchemaGenerator\Model\Use_;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;

final class AnnotationsAppender implements ClassMutatorInterface
{
Expand Down
4 changes: 2 additions & 2 deletions src/ClassMutator/ClassPropertiesAppender.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use ApiPlatform\SchemaGenerator\Model\Class_;
use ApiPlatform\SchemaGenerator\PropertyGenerator\PropertyGeneratorInterface;
use ApiPlatform\SchemaGenerator\Schema\Model\Class_ as SchemaClass;
use EasyRdf\Graph as RdfGraph;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
use Psr\Log\LoggerAwareTrait;

final class ClassPropertiesAppender implements ClassMutatorInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ExtractCardinalitiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use ApiPlatform\SchemaGenerator\CardinalitiesExtractor;
use ApiPlatform\SchemaGenerator\GoodRelationsBridge;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
use ApiPlatform\SchemaGenerator\SchemaGeneratorConfiguration;
use EasyRdf\Graph as RdfGraph;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/ClassMutator/EnumClassMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use ApiPlatform\SchemaGenerator\Model\Class_;
use ApiPlatform\SchemaGenerator\PhpTypeConverterInterface;
use ApiPlatform\SchemaGenerator\Schema\Model\Constant as SchemaConstant;
use EasyRdf\Graph as RdfGraph;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;

final class EnumClassMutator extends BaseEnumClassMutator
{
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
use ApiPlatform\SchemaGenerator\GoodRelationsBridge;
use ApiPlatform\SchemaGenerator\PhpTypeConverter;
use ApiPlatform\SchemaGenerator\Printer;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
use ApiPlatform\SchemaGenerator\TwigBuilder;
use ApiPlatform\SchemaGenerator\TypesGenerator;
use EasyRdf\Graph as RdfGraph;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Model/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ApiPlatform\SchemaGenerator\Schema\Model;

use ApiPlatform\SchemaGenerator\Model\Class_ as BaseClass_;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;

final class Class_ extends BaseClass_
{
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Model/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ApiPlatform\SchemaGenerator\Schema\Model;

use ApiPlatform\SchemaGenerator\Model\Constant as BaseConstant;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;

final class Constant extends BaseConstant
{
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Model/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ApiPlatform\SchemaGenerator\Schema\Model;

use ApiPlatform\SchemaGenerator\Model\Property as BaseProperty;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;

final class Property extends BaseProperty
{
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/PropertyGenerator/IdPropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use ApiPlatform\SchemaGenerator\PropertyGenerator\IdPropertyGeneratorInterface;
use ApiPlatform\SchemaGenerator\Schema\Model\Property as SchemaProperty;
use ApiPlatform\SchemaGenerator\Schema\Model\Type\PrimitiveType as SchemaPrimitiveType;
use EasyRdf\Resource as RdfResource;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;

final class IdPropertyGenerator implements IdPropertyGeneratorInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/PropertyGenerator/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
use ApiPlatform\SchemaGenerator\PropertyGenerator\PropertyGeneratorInterface;
use ApiPlatform\SchemaGenerator\Schema\Model\Property as SchemaProperty;
use ApiPlatform\SchemaGenerator\Schema\Model\Type\PrimitiveType;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
use ApiPlatform\SchemaGenerator\Schema\TypeConverter;
use EasyRdf\Resource as RdfResource;
use Psr\Log\LoggerAwareTrait;

final class PropertyGenerator implements PropertyGeneratorInterface
Expand Down
190 changes: 190 additions & 0 deletions src/Schema/Rdf/RdfGraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?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\Schema\Rdf;

use EasyRdf\Graph as EasyRdfGraph;

/**
* This class is a wrapper around the EasyRdf\Graph class. It allows the Schema
* Generator to get RdfResource objects instead of EasyRdf\Resource ones when required.
*
* @author d3fk::Angatar
*/
class RdfGraph
{
private EasyRdfGraph $graph;

/**
* Constructor, creates the RdfGraph decorating a freshly created or given
* EasyRdf\Graph with the capability to return/use RdfResource instead of EasyRdf\Resource.
*/
public function __construct($uri = null, $data = null, $format = null, $graph = null)
{
$this->graph = $graph ?? new EasyRdfGraph($uri, $data, $format);
}

/**
* Returns the corresponding EasyRdf\Graph.
*/
public function getEasyGraph(): EasyRdfGraph
{
return $this->graph;
}

/**
* Passes any call for an absent method to the contained EasyRdf\Graph, ensuring
* that it returns a Schema Generator's RdfResource in place of any EasyRdf\Resource.
*/
public function __call($methodName, $arguments)
{
$arguments = RdfResource::fromRdftoEasyRdfResources($arguments);

return RdfResource::wrapEasyRdfResource(\call_user_func_array([$this->graph, $methodName], $arguments));
}

/**
* Gets all the resources for a property of a resource and ensures that each
* EasyRdf\Resource matched is returned as wrapped in an RdfResource.
*/
public function allResources($resource, $property): array
{
$resource = RdfResource::fromRdftoEasyRdfResource($resource);

return RdfResource::wrapEasyRdfResources($this->graph->allResources($resource, $property));
}

/**
* Gets all values for a property path and ensures that each EasyRdf\Resource
* matched is returned as wrapped in an RdfResource.
*/
public function all($resource, $propertyPath, $type = null, $lang = null): array
{
$resource = RdfResource::fromRdftoEasyRdfResource($resource);

return RdfResource::wrapEasyRdfResources($this->graph->all($resource, $propertyPath, $type, $lang));
}

/**
* Gets all the resources in the graph of a certain type and ensures that
* each EasyRdf\Resource matched is returned as wrapped in an RdfResource.
*/
public function allOfType($type): array
{
return RdfResource::wrapEasyRdfResources($this->graph->allOfType($type));
}

/**
* Gets all values for a single property of a resource and ensures that each
* EasyRdf\Resource matched is returned as wrapped in an RdfResource.
*/
public function allForSingleProperty($resource, $property, $type = null, $lang = null): array
{
$resource = RdfResource::fromRdftoEasyRdfResource($resource);

return RdfResource::wrapEasyRdfResources($this->graph->allForSingleProperty($resource, $property, $type, $lang));
}

/**
* Gets the resource types of the graph as list of RdfResource.
*/
public function typesAsResources($resource = null): array
{
$resource = RdfResource::fromRdftoEasyRdfResource($resource);

return RdfResource::wrapEasyRdfResources($this->graph->typesAsResources($resource));
}

/**
* Gets an associative array of all the resources stored in the graph as
* RdfResources. The keys of the array is the URI of the related RdfResource.
*/
public function resources(): array
{
return RdfResource::wrapEasyRdfResources($this->graph->resources());
}

/**
* Get an array of RdfResources matching a certain property and optional value.
*/
public function resourcesMatching($property, $value = null): array
{
return RdfResource::wrapEasyRdfResources($this->graph->resourcesMatching($property, $value));
}

/**
* Turns any provided EasyRdf\Graph into an RdfGraph.
*/
public static function fromEasyRdf(EasyRdfGraph $graph): self
{
$rdfGraph = new static(null, null, null, $graph);

return $rdfGraph;
}

/**
* Ensures that any EasyRdf\Graph provided by reference will be wrapped in
* an RdfGraph.
*
* @param EasyRdfGraph|RdfGraph &$graph
*/
public static function ensureGraphClass(&$graph): void
{
$graph = ($graph instanceof EasyRdfGraph) ? self::fromEasyRdf($graph) : $graph;
}

/**
* Ensures that each EasyRdf\Graph, in an array of Graphs passed by reference,
* is wrapped in an RdfGraph.
*/
public static function ensureGraphsClass(array &$graphs): void
{
array_walk($graphs, self::class.'::ensureGraphClass');
}

/**
* Statically creates a new RdfGraph and loads RDF data from the provided URI.
*/
public static function newAndLoad($uri, $format = null): self
{
$graph = new self($uri);
$graph->load($uri, $format);

return $graph;
}

public function __toString()
{
return $this->graph->__toString();
}

public function __isset($name)
{
return $this->graph->__isset($name);
}

public function __set($name, $value)
{
return $this->graph->__set($name, $value);
}

public function __get($name)
{
return $this->graph->__get($name);
}

public function __unset($name)
{
return $this->graph->__unset($name);
}
}

0 comments on commit 55d13b9

Please sign in to comment.