ResourceBase.php in JSON:API Resources 8
File
src/Resource/ResourceBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\jsonapi_resources\Resource;
use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel;
use Drupal\jsonapi\JsonApiResource\LinkCollection;
use Drupal\jsonapi\JsonApiResource\ResourceObjectData;
use Drupal\jsonapi\ResourceResponse;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Drupal\jsonapi_resources\Exception\RouteDefinitionException;
use Drupal\jsonapi_resources\Unstable\DocumentExtractor;
use Drupal\jsonapi_resources\Unstable\ResourceResponseFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
abstract class ResourceBase {
protected $resourceTypeRepository;
private $resourceResponseFactory;
private $documentExtractor;
public function setResourceTypeRepository(ResourceTypeRepositoryInterface $resource_type_repository) : void {
$this->resourceTypeRepository = $resource_type_repository;
}
public function setResourceResponseFactory(ResourceResponseFactory $resource_response_factory) : void {
$this->resourceResponseFactory = $resource_response_factory;
}
public function setDocumentExtractor(DocumentExtractor $document_extractor) : void {
$this->documentExtractor = $document_extractor;
}
protected function getDocumentFromRequest(Request $request) : JsonApiDocumentTopLevel {
return $this->documentExtractor
->getDocument($request);
}
protected function createJsonapiResponse(ResourceObjectData $data, Request $request, $response_code = 200, array $headers = [], LinkCollection $links = NULL, array $meta = []) : ResourceResponse {
return $this->resourceResponseFactory
->create($data, $request, $response_code, $headers, $links, $meta);
}
public function getRouteResourceTypes(Route $route, string $route_name) : array {
return array_map(function ($resource_type_name) use ($route_name) {
$resource_type = $this->resourceTypeRepository
->getByTypeName($resource_type_name);
if (is_null($resource_type)) {
throw new RouteDefinitionException("The {$route_name} route definition's _jsonapi_resource_types route default declares the resource type {$resource_type_name} but a resource type by that name does not exist.");
}
return $resource_type;
}, $route
->getDefault('_jsonapi_resource_types') ?: []);
}
}
Classes
Name |
Description |
ResourceBase |
Defines basic functionality for a JSON:API Resource. |