You are here

class JsonapiHelper in Entity Share 8.3

Same name and namespace in other branches
  1. 8 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper
  2. 8.2 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper

Provides methods not present in the JSON:API module.

@package Drupal\entity_share_client\Service

Hierarchy

Expanded class hierarchy of JsonapiHelper

1 string reference to 'JsonapiHelper'
entity_share_client.services.yml in modules/entity_share_client/entity_share_client.services.yml
modules/entity_share_client/entity_share_client.services.yml
1 service uses JsonapiHelper
entity_share_client.jsonapi_helper in modules/entity_share_client/entity_share_client.services.yml
Drupal\entity_share_client\Service\JsonapiHelper

File

modules/entity_share_client/src/Service/JsonapiHelper.php, line 17

Namespace

Drupal\entity_share_client\Service
View source
class JsonapiHelper implements JsonapiHelperInterface {
  use StringTranslationTrait;

  /**
   * The JsonApiDocumentTopLevelNormalizer normalizer.
   *
   * @var \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer
   */
  protected $jsonapiDocumentTopLevelNormalizer;

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

  /**
   * JsonapiHelper constructor.
   *
   * @param \Symfony\Component\Serializer\SerializerInterface $serializer
   *   A serializer.
   * @param \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer $jsonapi_document_top_level_normalizer
   *   The JsonApiDocumentTopLevelNormalizer normalizer.
   * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
   *   The resource type repository.
   */
  public function __construct(SerializerInterface $serializer, JsonApiDocumentTopLevelNormalizer $jsonapi_document_top_level_normalizer, ResourceTypeRepositoryInterface $resource_type_repository) {
    $this->jsonapiDocumentTopLevelNormalizer = $jsonapi_document_top_level_normalizer;
    $this->jsonapiDocumentTopLevelNormalizer
      ->setSerializer($serializer);
    $this->resourceTypeRepository = $resource_type_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function extractEntity(array $data) {

    // Format JSON as in
    // JsonApiDocumentTopLevelNormalizerTest::testDenormalize().
    $prepared_json = [
      'data' => [
        'type' => $data['type'],
        'attributes' => $data['attributes'],
      ],
    ];
    return $this->jsonapiDocumentTopLevelNormalizer
      ->denormalize($prepared_json, NULL, 'api_json', [
      'resource_type' => $this->resourceTypeRepository
        ->getByTypeName($data['type']),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonapiHelper::$jsonapiDocumentTopLevelNormalizer protected property The JsonApiDocumentTopLevelNormalizer normalizer.
JsonapiHelper::$resourceTypeRepository protected property The resource type repository.
JsonapiHelper::extractEntity public function Helper function to unserialize an entity from the JSON:API response. Overrides JsonapiHelperInterface::extractEntity
JsonapiHelper::__construct public function JsonapiHelper constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.