diff --git a/src/Normalizer/ConfigEntityNormalizer.php b/src/Normalizer/ConfigEntityNormalizer.php index f231379..c6f5e88 100644 --- a/src/Normalizer/ConfigEntityNormalizer.php +++ b/src/Normalizer/ConfigEntityNormalizer.php @@ -3,8 +3,6 @@ namespace Drupal\jsonapi\Normalizer; use Drupal\Core\Config\Entity\ConfigEntityInterface; -use Drupal\jsonapi\EntityCollection; -use Drupal\jsonapi\Relationship; use Drupal\jsonapi\RelationshipInterface; use Symfony\Component\Serializer\Exception\UnexpectedValueException; @@ -15,7 +13,7 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException; * * @package Drupal\jsonapi\Normalizer */ -class ConfigEntityNormalizer extends ContentEntityNormalizer { +class ConfigEntityNormalizer extends EntityNormalizer { /** * The interface or class that this Normalizer supports. diff --git a/src/Normalizer/ContentEntityNormalizer.php b/src/Normalizer/ContentEntityNormalizer.php index 745cd2b..92b6c29 100644 --- a/src/Normalizer/ContentEntityNormalizer.php +++ b/src/Normalizer/ContentEntityNormalizer.php @@ -4,192 +4,17 @@ namespace Drupal\jsonapi\Normalizer; use Drupal\Core\Access\AccessibleInterface; use Drupal\Core\Entity\ContentEntityInterface; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; -use Drupal\Core\Field\FieldItemListInterface; use Drupal\jsonapi\Configuration\ResourceConfigInterface; use Drupal\jsonapi\Context\CurrentContextInterface; -use Drupal\jsonapi\EntityCollection; use Drupal\jsonapi\LinkManager\LinkManagerInterface; -use Drupal\jsonapi\Relationship; use Drupal\jsonapi\RelationshipInterface; use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; /** - * Converts the Drupal entity object structure to a HAL array structure. + * Converts the Drupal content entity object structure to a HAL array structure. */ -class ContentEntityNormalizer extends NormalizerBase implements DenormalizerInterface, ContentEntityNormalizerInterface { - - /** - * The interface or class that this Normalizer supports. - * - * @var string - */ - protected $supportedInterfaceOrClass = ContentEntityInterface::class; - - /** - * The formats that the Normalizer can handle. - * - * @var array - */ - protected $formats = array('api_json'); - - /** - * The link manager. - * - * @var \Drupal\jsonapi\LinkManager\LinkManagerInterface - */ - protected $linkManager; - - /** - * The resource manager. - * - * @var \Drupal\jsonapi\Configuration\ResourceManagerInterface - */ - protected $resourceManager; - - /** - * The current JSON API request context. - * - * @var \Drupal\jsonapi\Context\CurrentContextInterface - */ - protected $currentContext; - - /** - * Constructs an ContentEntityNormalizer object. - * - * @param \Drupal\jsonapi\LinkManager\LinkManagerInterface $link_manager - * The link manager. - * @param \Drupal\jsonapi\Context\CurrentContextInterface $current_context - * The current context. - */ - public function __construct(LinkManagerInterface $link_manager, CurrentContextInterface $current_context) { - $this->linkManager = $link_manager; - $this->currentContext = $current_context; - $this->resourceManager = $current_context->getResourceManager(); - } - - /** - * {@inheritdoc} - */ - public function normalize($entity, $format = NULL, array $context = array()) { - // If the fields to use were specified, only output those field values. - $resource_type = $context['resource_config']->getTypeName(); - // Get the bundle ID of the requested resource. This is used to determine if - // this is a bundle level resource or an entity level resource. - $bundle_id = $context['resource_config']->getBundleId(); - if (!empty($context['sparse_fieldset'][$resource_type])) { - $field_names = $context['sparse_fieldset'][$resource_type]; - } - else { - $field_names = $this->getFieldNames($entity, $bundle_id); - } - /* @var Value\FieldNormalizerValueInterface[] $normalizer_values */ - $normalizer_values = []; - foreach ($this->getFields($entity, $bundle_id) as $field_name => $field) { - // Relationships cannot be excluded by using sparse fieldsets. - $is_relationship = $this->isRelationship($field); - if (!$is_relationship && !in_array($field_name, $field_names)) { - continue; - } - $normalizer_values[$field_name] = $this->serializeField($field, $context, $format); - } - // Clean all the NULL values coming from denied access. - $normalizer_values = array_filter($normalizer_values); - - $link_context = ['link_manager' => $this->linkManager]; - return new Value\ContentEntityNormalizerValue($normalizer_values, $context, $entity, $link_context); - } - - /** - * Checks if the passed field is a relationship field. - * - * @param mixed $field - * The field. - * - * @return bool - * TRUE if it's a JSON API relationship. - */ - protected function isRelationship($field) { - return $field instanceof EntityReferenceFieldItemList || $field instanceof RelationshipInterface; - } - - /** - * {@inheritdoc} - */ - public function denormalize($data, $class, $format = NULL, array $context = array()) { - if (empty($context['resource_config']) || !$context['resource_config'] instanceof ResourceConfigInterface) { - throw new PreconditionFailedHttpException('Missing context during denormalization.'); - } - /* @var \Drupal\jsonapi\Configuration\ResourceConfigInterface $resource_config */ - $resource_config = $context['resource_config']; - $bundle_id = $resource_config->getBundleId(); - $bundle_key = $this->resourceManager - ->getEntityTypeManager() - ->getDefinition($resource_config->getEntityTypeId()) - ->getKey('bundle'); - if ($bundle_key && $bundle_id) { - $data[$bundle_key] = $bundle_id; - } - - return $resource_config->getStorage()->create($data); - } - - /** - * Gets the field names for the given entity. - * - * @param mixed $entity - * The entity. - * - * @return string[] - * The field names. - */ - protected function getFieldNames($entity, $bundle_id) { - /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - return array_keys($this->getFields($entity, $bundle_id)); - } - - /** - * Gets the field names for the given entity. - * - * @param mixed $entity - * The entity. - * @param string $bundle_id - * The bundle id. - * - * @return array - * The fields. - */ - protected function getFields($entity, $bundle_id) { - /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - return $entity->getFields(); - } - - /** - * Serializes a given field. - * - * @param mixed $field - * The field to serialize. - * @param array $context - * The normalization context. - * @param string $format - * The serialization format. - * - * @return Value\FieldNormalizerValueInterface - * The normalized value. - */ - protected function serializeField($field, $context, $format) { - /* @var \Drupal\Core\Field\FieldItemListInterface|\Drupal\jsonapi\RelationshipInterface $field */ - // Continue if the current user does not have access to view this field. - if ($field instanceof AccessibleInterface && !$field->access('view', $context['account'])) { - return NULL; - } - $output = $this->serializer->normalize($field, $format, $context); - $is_relationship = $this->isRelationship($field); - $property_type = $is_relationship ? 'relationships' : 'attributes'; - $output->setPropertyType($property_type); - return $output; - } +class ContentEntityNormalizer extends EntityNormalizer { } diff --git a/src/Normalizer/DocumentRootNormalizerInterface.php b/src/Normalizer/DocumentRootNormalizerInterface.php index 1efd612..40dba31 100644 --- a/src/Normalizer/DocumentRootNormalizerInterface.php +++ b/src/Normalizer/DocumentRootNormalizerInterface.php @@ -2,8 +2,6 @@ namespace Drupal\jsonapi\Normalizer; -use Drupal\Core\Field\EntityReferenceFieldItemListInterface; -use Drupal\jsonapi\EntityCollectionInterface; /** * Class DocumentRootNormalizerInterface. @@ -11,6 +9,7 @@ use Drupal\jsonapi\EntityCollectionInterface; * @package Drupal\jsonapi\Normalizer */ interface DocumentRootNormalizerInterface { + /** * Build the normalizer value. * @@ -18,4 +17,5 @@ interface DocumentRootNormalizerInterface { * The normalizer value. */ public function buildNormalizerValue($data, $format = NULL, array $context = array()); + } diff --git a/src/Normalizer/ContentEntityNormalizer.php b/src/Normalizer/EntityNormalizer.php similarity index 95% copy from src/Normalizer/ContentEntityNormalizer.php copy to src/Normalizer/EntityNormalizer.php index 745cd2b..7e91ad6 100644 --- a/src/Normalizer/ContentEntityNormalizer.php +++ b/src/Normalizer/EntityNormalizer.php @@ -4,14 +4,10 @@ namespace Drupal\jsonapi\Normalizer; use Drupal\Core\Access\AccessibleInterface; use Drupal\Core\Entity\ContentEntityInterface; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; -use Drupal\Core\Field\FieldItemListInterface; use Drupal\jsonapi\Configuration\ResourceConfigInterface; use Drupal\jsonapi\Context\CurrentContextInterface; -use Drupal\jsonapi\EntityCollection; use Drupal\jsonapi\LinkManager\LinkManagerInterface; -use Drupal\jsonapi\Relationship; use Drupal\jsonapi\RelationshipInterface; use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; @@ -19,7 +15,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; /** * Converts the Drupal entity object structure to a HAL array structure. */ -class ContentEntityNormalizer extends NormalizerBase implements DenormalizerInterface, ContentEntityNormalizerInterface { +class EntityNormalizer extends NormalizerBase implements DenormalizerInterface, ContentEntityNormalizerInterface { /** * The interface or class that this Normalizer supports. diff --git a/src/Normalizer/EntityReferenceFieldNormalizer.php b/src/Normalizer/EntityReferenceFieldNormalizer.php index 0300d8d..8604332 100644 --- a/src/Normalizer/EntityReferenceFieldNormalizer.php +++ b/src/Normalizer/EntityReferenceFieldNormalizer.php @@ -6,7 +6,6 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; -use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Field\TypedData\FieldItemDataDefinition; use Drupal\jsonapi\Configuration\ResourceManagerInterface; use Drupal\jsonapi\EntityCollection; diff --git a/src/Normalizer/FieldItemNormalizer.php b/src/Normalizer/FieldItemNormalizer.php index d4ac599..0a504a4 100644 --- a/src/Normalizer/FieldItemNormalizer.php +++ b/src/Normalizer/FieldItemNormalizer.php @@ -27,8 +27,8 @@ class FieldItemNormalizer extends NormalizerBase { /** * {@inheritdoc} */ - public function normalize($relationship_item, $format = NULL, array $context = array()) { - $values = $relationship_item->toArray(); + public function normalize($field_item, $format = NULL, array $context = array()) { + $values = $field_item->toArray(); if (isset($context['langcode'])) { $values['lang'] = $context['langcode']; } diff --git a/src/Normalizer/RelationshipItemNormalizer.php b/src/Normalizer/RelationshipItemNormalizer.php index 1127699..b5d06f2 100644 --- a/src/Normalizer/RelationshipItemNormalizer.php +++ b/src/Normalizer/RelationshipItemNormalizer.php @@ -5,7 +5,6 @@ namespace Drupal\jsonapi\Normalizer; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\jsonapi\Configuration\ResourceManagerInterface; -use Drupal\jsonapi\Relationship; use Drupal\jsonapi\RelationshipItemInterface; use Drupal\serialization\EntityResolver\UuidReferenceInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; diff --git a/src/Normalizer/RelationshipNormalizer.php b/src/Normalizer/RelationshipNormalizer.php index c09ae4f..ae62446 100644 --- a/src/Normalizer/RelationshipNormalizer.php +++ b/src/Normalizer/RelationshipNormalizer.php @@ -6,8 +6,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\jsonapi\Configuration\ResourceManagerInterface; use Drupal\jsonapi\LinkManager\LinkManagerInterface; use Drupal\jsonapi\Relationship; -use Drupal\jsonapi\RelationshipInterface; -use League\Container\ContainerAwareTrait; use Symfony\Component\Serializer\Exception\UnexpectedValueException; class RelationshipNormalizer extends NormalizerBase { diff --git a/src/RelationshipItem.php b/src/RelationshipItem.php index 0d431e4..bd97e93 100644 --- a/src/RelationshipItem.php +++ b/src/RelationshipItem.php @@ -75,9 +75,7 @@ class RelationshipItem implements RelationshipItemInterface { * {@inheritdoc} */ public function getValue() { - $method = $this->getTargetResourceConfig()->getIdKey() == 'uuid' ? - 'uuid' : - 'id'; + $method = $this->getTargetResourceConfig()->getIdKey() == 'uuid' ?: 'id'; return [$this->targetKey => $this->getTargetEntity()->{$method}()]; }