Skip to content

Commit

Permalink
Merge f67439a into 997f6f8
Browse files Browse the repository at this point in the history
  • Loading branch information
Angatar committed Dec 12, 2023
2 parents 997f6f8 + f67439a commit f778616
Show file tree
Hide file tree
Showing 32 changed files with 1,595 additions and 43 deletions.
4 changes: 3 additions & 1 deletion src/AttributeGenerator/ConfigurationAttributeGenerator.php
Expand Up @@ -25,8 +25,10 @@ public function generateClassAttributes(Class_ $class): array
{
$typeAttributes = $this->config['types'][$class->name()]['attributes'] ?? [[]];
$vocabAttributes = [[]];

if ($class instanceof SchemaClass) {
$vocabAttributes = $this->config['vocabularies'][$class->resource()->getGraph()->getUri()]['attributes'] ?? [[]];
$graphUri = (null !== $class->resource()->getGraph()) ? $class->resource()->getGraph()->getUri() : null;
$vocabAttributes = $this->config['vocabularies'][$graphUri]['attributes'] ?? [[]];
}

$getAttributesNames = static fn (array $config) => $config === [[]]
Expand Down
6 changes: 3 additions & 3 deletions src/CardinalitiesExtractor.php
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());
if (false !== $fromGoodRelations) {
return $fromGoodRelations;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ClassMutator/AnnotationsAppender.php
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
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
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
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
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
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
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
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
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
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
251 changes: 251 additions & 0 deletions src/Schema/Rdf/RdfGraph.php
@@ -0,0 +1,251 @@
<?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.
*/
final public function __construct(string $uri = null, string $data = null, string $format = null, EasyRdfGraph $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.
*
* @param array<mixed> $arguments
*
* @return mixed depending on the method called
*/
#[\ReturnTypeWillChange]
public function __call(string $methodName, array $arguments)
{
$arguments = RdfResource::fromRdftoEasyRdfResources($arguments);
$callback = [$this->graph, $methodName];
if (\is_callable($callback)) {
return RdfResource::wrapEasyRdfResource(\call_user_func_array($callback, $arguments));
}
throw new \Exception('Method not found');
}

/**
* Gets all the resources for a property of a resource and ensures that each
* EasyRdf\Resource matched is returned as wrapped in an RdfResource.
*
* @return array<string>
*/
public function allResources(string $resource, string $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.
*
* @return array<string>
*/
public function all(string $resource, string $propertyPath, string $type = null, string $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.
*
* @return array<mixed>
*/
public function allOfType(string $type): array
{
return RdfResource::wrapEasyRdfResources($this->graph->allOfType($type));
}

/**
* Gets the resource types of the graph as list of RdfResource.
*
* @param string|null $resource
*
* @return 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.
*
* @return RdfResource[]
*/
public function resources(): array
{
return RdfResource::wrapEasyRdfResources($this->graph->resources());
}

/**
* Get an array of RdfResources matching a certain property and optional value.
*
* @param string $property the property to check
* @param mixed $value optional, the value of the propery to check for
*
* @return RdfResource[]
*/
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.
*
* @param array<EasyRdfGraph|RdfGraph> &$graphs
*/
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(string $uri, string $format = null): self
{
$graph = new self($uri);
$graph->load($uri, $format);

return $graph;
}

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

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

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

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

public function __unset(string $name): void
{
$this->graph->__unset($name);
}

// The following Methods are not required but avoid PHPStan special cases of
// class Reflection without implementing extensions; see __call()

/**
* Get the URI of the EasyRdf\Graph.
*/
public function getUri(): ?string
{
return $this->graph->getUri();
}

/** Get or create a resource stored in a graph.
*
* If the resource did not previously exist, then a new resource will
* be created.
*
* If URI is null, then the URI of the graph is used.
*
* @param string $uri The URI of the resource
* @param mixed $types RDF type of a new resource (e.g. foaf:Person)
*/
public function resource($uri = null, $types = []): RdfResource
{
return RdfResource::wrapEasyRdfResource($this->graph->resource($uri, $types));
}

public function load(string $uri = null, string $format = null): int
{
return $this->graph->load($uri, $format);
}

/**
* Parse a file containing RDF data into the graph object.
*
* @param string $filename The path of the file to load
* @param string $format Optional format of the file
* @param string $uri The URI of the file to load
*
* @return int The number of triples added to the graph
*/
public function parseFile($filename, $format = null, $uri = null): int
{
return $this->graph->parseFile($filename, $format, $uri);
}
}

0 comments on commit f778616

Please sign in to comment.