You are here

class MutableResourceTypeLinkProviderDeriver in JSON:API Hypermedia 8

Derives plugins that add HATEOAS controls for mutable resources.

Hierarchy

Expanded class hierarchy of MutableResourceTypeLinkProviderDeriver

File

examples/Plugin/Derivative/MutableResourceTypeLinkProviderDeriver.php, line 14

Namespace

Drupal\jsonapi_hypermedia\Plugin\Derivative
View source
class MutableResourceTypeLinkProviderDeriver extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The JSON:API resource type repository.
   *
   * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
   */
  protected $resourceTypeRepository;

  /**
   * MutableResourceTypeLinkProviderDeriver constructor.
   *
   * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
   *   The JSON:API resource type repository.
   */
  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository) {
    $this->resourceTypeRepository = $resource_type_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('jsonapi.resource_type.repository'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $resource_types = array_filter($this->resourceTypeRepository
      ->all(), function (ResourceType $resource_type) {
      return $resource_type
        ->isLocatable() && $resource_type
        ->isMutable();
    });
    $derivative_definitions = array_reduce($resource_types, function ($derivative_definitions, ResourceType $resource_type) use ($base_plugin_definition) {
      foreach ([
        'update',
        'remove',
      ] as $operation) {
        $derivative_id = "{$resource_type->getTypeName()}.{$operation}";
        $derivative_definitions[$derivative_id] = array_merge($base_plugin_definition, [
          'link_key' => 'self',
          'link_relation_type' => $operation,
          'link_context' => [
            'resource_object' => $resource_type
              ->getTypeName(),
          ],
          'default_configuration' => [
            'operation' => $operation,
          ],
        ]);
      }
      return $derivative_definitions;
    }, []);
    return $derivative_definitions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
MutableResourceTypeLinkProviderDeriver::$resourceTypeRepository protected property The JSON:API resource type repository.
MutableResourceTypeLinkProviderDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
MutableResourceTypeLinkProviderDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
MutableResourceTypeLinkProviderDeriver::__construct public function MutableResourceTypeLinkProviderDeriver constructor.