-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow schema generator to use labels for naming API resources a…
…nd PHP classes
- Loading branch information
Showing
31 changed files
with
1,430 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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); | ||
} | ||
} |
Oops, something went wrong.