You are here

class UnprocessableHttpEntityExceptionNormalizer in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/UnprocessableHttpEntityExceptionNormalizer.php \Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer

Normalizes and UnprocessableHttpEntityException.

Normalizes an UnprocessableHttpEntityException in compliance with the JSON API specification. A source pointer is added to help client applications report validation errors, for example on an Entity edit form.

@internal

Hierarchy

Expanded class hierarchy of UnprocessableHttpEntityExceptionNormalizer

See also

http://jsonapi.org/format/#error-objects

1 string reference to 'UnprocessableHttpEntityExceptionNormalizer'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses UnprocessableHttpEntityExceptionNormalizer
serializer.normalizer.unprocessable_entity_exception.jsonapi in ./jsonapi.services.yml
Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer

File

src/Normalizer/UnprocessableHttpEntityExceptionNormalizer.php, line 20

Namespace

Drupal\jsonapi\Normalizer
View source
class UnprocessableHttpEntityExceptionNormalizer extends HttpExceptionNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = UnprocessableHttpEntityException::class;

  /**
   * {@inheritdoc}
   */
  protected function buildErrorObjects(HttpException $exception) {

    /* @var $exception \Drupal\jsonapi\Exception\UnprocessableHttpEntityException */
    $errors = parent::buildErrorObjects($exception);
    $error = $errors[0];
    unset($error['links']);
    $errors = [];
    $violations = $exception
      ->getViolations();
    $entity_violations = $violations
      ->getEntityViolations();
    foreach ($entity_violations as $violation) {

      /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
      $error['detail'] = 'Entity is not valid: ' . $violation
        ->getMessage();
      $error['source']['pointer'] = '/data';
      $errors[] = $error;
    }
    $entity = $violations
      ->getEntity();
    foreach ($violations
      ->getFieldNames() as $field_name) {
      $field_violations = $violations
        ->getByField($field_name);
      $cardinality = $entity
        ->get($field_name)
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getCardinality();
      foreach ($field_violations as $violation) {

        /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
        $error['detail'] = $violation
          ->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation
          ->getMessage());
        $pointer = '/data/attributes/' . str_replace('.', '/', $violation
          ->getPropertyPath());
        if ($cardinality == 1) {

          // Remove erroneous '/0/' index for single-value fields.
          $pointer = str_replace("/data/attributes/{$field_name}/0/", "/data/attributes/{$field_name}/", $pointer);
        }
        $error['source']['pointer'] = $pointer;
        $errors[] = $error;
      }
    }
    return $errors;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
HttpExceptionNormalizer::$currentUser protected property The current user making the request.
HttpExceptionNormalizer::getInfoUrl public static function Return a string to the common problem type.
HttpExceptionNormalizer::normalize public function Normalizes an object into a set of arrays/scalars.
HttpExceptionNormalizer::__construct public function HttpExceptionNormalizer constructor.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::$formats protected property The formats that the Normalizer can handle. 4
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization
UnprocessableHttpEntityExceptionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides HttpExceptionNormalizer::$supportedInterfaceOrClass
UnprocessableHttpEntityExceptionNormalizer::buildErrorObjects protected function Builds the normalized JSON API error objects for the response. Overrides HttpExceptionNormalizer::buildErrorObjects