|
| 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