|
| 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 | + final public function __construct(string $uri = null, string $data = null, string $format = null, EasyRdfGraph $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 | + * @param array<mixed> $arguments |
| 50 | + * |
| 51 | + * @return mixed depending on the method called |
| 52 | + */ |
| 53 | + #[\ReturnTypeWillChange] |
| 54 | + public function __call(string $methodName, array $arguments) |
| 55 | + { |
| 56 | + $arguments = RdfResource::fromRdftoEasyRdfResources($arguments); |
| 57 | + $callback = [$this->graph, $methodName]; |
| 58 | + if (\is_callable($callback)) { |
| 59 | + return RdfResource::wrapEasyRdfResource(\call_user_func_array($callback, $arguments)); |
| 60 | + } |
| 61 | + throw new \Exception('Method not found'); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets all the resources for a property of a resource and ensures that each |
| 66 | + * EasyRdf\Resource matched is returned as wrapped in an RdfResource. |
| 67 | + * |
| 68 | + * @return array<string> |
| 69 | + */ |
| 70 | + public function allResources(string $resource, string $property): array |
| 71 | + { |
| 72 | + $resource = RdfResource::fromRdftoEasyRdfResource($resource); |
| 73 | + |
| 74 | + return RdfResource::wrapEasyRdfResources($this->graph->allResources($resource, $property)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets all values for a property path and ensures that each EasyRdf\Resource |
| 79 | + * matched is returned as wrapped in an RdfResource. |
| 80 | + * |
| 81 | + * @return array<string> |
| 82 | + */ |
| 83 | + public function all(string $resource, string $propertyPath, string $type = null, string $lang = null): array |
| 84 | + { |
| 85 | + $resource = RdfResource::fromRdftoEasyRdfResource($resource); |
| 86 | + |
| 87 | + return RdfResource::wrapEasyRdfResources($this->graph->all($resource, $propertyPath, $type, $lang)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets all the resources in the graph of a certain type and ensures that |
| 92 | + * each EasyRdf\Resource matched is returned as wrapped in an RdfResource. |
| 93 | + * |
| 94 | + * @return array<mixed> |
| 95 | + */ |
| 96 | + public function allOfType(string $type): array |
| 97 | + { |
| 98 | + return RdfResource::wrapEasyRdfResources($this->graph->allOfType($type)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the resource types of the graph as list of RdfResource. |
| 103 | + * |
| 104 | + * @param string|null $resource |
| 105 | + * |
| 106 | + * @return RdfResource[] |
| 107 | + */ |
| 108 | + public function typesAsResources($resource = null): array |
| 109 | + { |
| 110 | + $resource = RdfResource::fromRdftoEasyRdfResource($resource); |
| 111 | + |
| 112 | + return RdfResource::wrapEasyRdfResources($this->graph->typesAsResources($resource)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets an associative array of all the resources stored in the graph as |
| 117 | + * RdfResources. The keys of the array is the URI of the related RdfResource. |
| 118 | + * |
| 119 | + * @return RdfResource[] |
| 120 | + */ |
| 121 | + public function resources(): array |
| 122 | + { |
| 123 | + return RdfResource::wrapEasyRdfResources($this->graph->resources()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get an array of RdfResources matching a certain property and optional value. |
| 128 | + * |
| 129 | + * @param string $property the property to check |
| 130 | + * @param mixed $value optional, the value of the propery to check for |
| 131 | + * |
| 132 | + * @return RdfResource[] |
| 133 | + */ |
| 134 | + public function resourcesMatching($property, $value = null): array |
| 135 | + { |
| 136 | + return RdfResource::wrapEasyRdfResources($this->graph->resourcesMatching($property, $value)); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Turns any provided EasyRdf\Graph into an RdfGraph. |
| 141 | + */ |
| 142 | + public static function fromEasyRdf(EasyRdfGraph $graph): self |
| 143 | + { |
| 144 | + $rdfGraph = new static(null, null, null, $graph); |
| 145 | + |
| 146 | + return $rdfGraph; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Ensures that any EasyRdf\Graph provided by reference will be wrapped in |
| 151 | + * an RdfGraph. |
| 152 | + * |
| 153 | + * @param EasyRdfGraph|RdfGraph &$graph |
| 154 | + */ |
| 155 | + public static function ensureGraphClass(&$graph): void |
| 156 | + { |
| 157 | + $graph = ($graph instanceof EasyRdfGraph) ? self::fromEasyRdf($graph) : $graph; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Ensures that each EasyRdf\Graph, in an array of Graphs passed by reference, |
| 162 | + * is wrapped in an RdfGraph. |
| 163 | + * |
| 164 | + * @param array<EasyRdfGraph|RdfGraph> &$graphs |
| 165 | + */ |
| 166 | + public static function ensureGraphsClass(array &$graphs): void |
| 167 | + { |
| 168 | + array_walk($graphs, self::class.'::ensureGraphClass'); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Statically creates a new RdfGraph and loads RDF data from the provided URI. |
| 173 | + */ |
| 174 | + public static function newAndLoad(string $uri, string $format = null): self |
| 175 | + { |
| 176 | + $graph = new self($uri); |
| 177 | + $graph->load($uri, $format); |
| 178 | + |
| 179 | + return $graph; |
| 180 | + } |
| 181 | + |
| 182 | + public function __toString(): string |
| 183 | + { |
| 184 | + return $this->graph->__toString(); |
| 185 | + } |
| 186 | + |
| 187 | + public function __isset(string $name): bool |
| 188 | + { |
| 189 | + return $this->graph->__isset($name); |
| 190 | + } |
| 191 | + |
| 192 | + public function __set(string $name, string $value): void |
| 193 | + { |
| 194 | + $this->graph->__set($name, $value); |
| 195 | + } |
| 196 | + |
| 197 | + public function __get(string $name): ?string |
| 198 | + { |
| 199 | + return $this->graph->__get($name); |
| 200 | + } |
| 201 | + |
| 202 | + public function __unset(string $name): void |
| 203 | + { |
| 204 | + $this->graph->__unset($name); |
| 205 | + } |
| 206 | + |
| 207 | + // The following Methods are not required but avoid PHPStan special cases of |
| 208 | + // class Reflection without implementing extensions; see __call() |
| 209 | + |
| 210 | + /** |
| 211 | + * Get the URI of the EasyRdf\Graph. |
| 212 | + */ |
| 213 | + public function getUri(): ?string |
| 214 | + { |
| 215 | + return $this->graph->getUri(); |
| 216 | + } |
| 217 | + |
| 218 | + /** Get or create a resource stored in a graph. |
| 219 | + * |
| 220 | + * If the resource did not previously exist, then a new resource will |
| 221 | + * be created. |
| 222 | + * |
| 223 | + * If URI is null, then the URI of the graph is used. |
| 224 | + * |
| 225 | + * @param string $uri The URI of the resource |
| 226 | + * @param mixed $types RDF type of a new resource (e.g. foaf:Person) |
| 227 | + */ |
| 228 | + public function resource($uri = null, $types = []): RdfResource |
| 229 | + { |
| 230 | + return RdfResource::wrapEasyRdfResource($this->graph->resource($uri, $types)); |
| 231 | + } |
| 232 | + |
| 233 | + public function load(string $uri = null, string $format = null): int |
| 234 | + { |
| 235 | + return $this->graph->load($uri, $format); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Parse a file containing RDF data into the graph object. |
| 240 | + * |
| 241 | + * @param string $filename The path of the file to load |
| 242 | + * @param string $format Optional format of the file |
| 243 | + * @param string $uri The URI of the file to load |
| 244 | + * |
| 245 | + * @return int The number of triples added to the graph |
| 246 | + */ |
| 247 | + public function parseFile($filename, $format = null, $uri = null): int |
| 248 | + { |
| 249 | + return $this->graph->parseFile($filename, $format, $uri); |
| 250 | + } |
| 251 | +} |
0 commit comments