You are here

final class DocumentResolver in JSON:API Resources 8

Deserializes POST, PATCH and DELETE request documents.

@internal

Hierarchy

  • class \Drupal\jsonapi_resources\Unstable\Controller\ArgumentResolver\DocumentResolver implements \Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface

Expanded class hierarchy of DocumentResolver

1 string reference to 'DocumentResolver'
jsonapi_resources.services.yml in ./jsonapi_resources.services.yml
jsonapi_resources.services.yml
1 service uses DocumentResolver
jsonapi_resources.argument_resolver.document in ./jsonapi_resources.services.yml
Drupal\jsonapi_resources\Unstable\Controller\ArgumentResolver\DocumentResolver

File

src/Unstable/Controller/ArgumentResolver/DocumentResolver.php, line 16

Namespace

Drupal\jsonapi_resources\Unstable\Controller\ArgumentResolver
View source
final class DocumentResolver implements ArgumentValueResolverInterface {

  /**
   * The document extractor.
   *
   * @var \Drupal\jsonapi_resources\Unstable\DocumentExtractor
   */
  protected $documentExtractor;

  /**
   * Constructs a JSON:API document argument resolver.
   *
   * @param \Drupal\jsonapi_resources\Unstable\DocumentExtractor $document_extractor
   *   The document extractor.
   */
  public function __construct(DocumentExtractor $document_extractor) {
    $this->documentExtractor = $document_extractor;
  }

  /**
   * {@inheritdoc}
   */
  public function supports(Request $request, ArgumentMetadata $argument) {
    $supported_method = in_array($request
      ->getMethod(), [
      'POST',
      'PATCH',
    ]);
    $is_delete = $request
      ->isMethod('DELETE');
    $is_relationship = $request->attributes
      ->has('_jsonapi_relationship_field_name');
    $supported_method = $supported_method || $is_delete && $is_relationship;
    $supported_format = $request
      ->getRequestFormat() === 'api_json';
    $correct_type = $argument
      ->getType() === JsonApiDocumentTopLevel::class;
    return $supported_method && $supported_format && $correct_type;
  }

  /**
   * {@inheritdoc}
   */
  public function resolve(Request $request, ArgumentMetadata $argument) {
    (yield $this->documentExtractor
      ->getDocument($request));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DocumentResolver::$documentExtractor protected property The document extractor.
DocumentResolver::resolve public function
DocumentResolver::supports public function
DocumentResolver::__construct public function Constructs a JSON:API document argument resolver.