vendor/api-platform/core/src/Hydra/Serializer/ErrorNormalizer.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Hydra\Serializer;
  12. use ApiPlatform\Api\UrlGeneratorInterface;
  13. use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
  14. use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
  15. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  16. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  17. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  18. /**
  19.  * Converts {@see \Exception} or {@see FlattenException} or {@see LegacyFlattenException} to a Hydra error representation.
  20.  *
  21.  * @author Kévin Dunglas <dunglas@gmail.com>
  22.  * @author Samuel ROZE <samuel.roze@gmail.com>
  23.  */
  24. final class ErrorNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  25. {
  26.     use ErrorNormalizerTrait;
  27.     public const FORMAT 'jsonld';
  28.     public const TITLE 'title';
  29.     private $urlGenerator;
  30.     private $debug;
  31.     private $defaultContext = [self::TITLE => 'An error occurred'];
  32.     public function __construct(UrlGeneratorInterface $urlGeneratorbool $debug false, array $defaultContext = [])
  33.     {
  34.         $this->urlGenerator $urlGenerator;
  35.         $this->debug $debug;
  36.         $this->defaultContext array_merge($this->defaultContext$defaultContext);
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      *
  41.      * @return array|string|int|float|bool|\ArrayObject|null
  42.      */
  43.     public function normalize($object$format null, array $context = [])
  44.     {
  45.         $data = [
  46.             '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
  47.             '@type' => 'hydra:Error',
  48.             'hydra:title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
  49.             'hydra:description' => $this->getErrorMessage($object$context$this->debug),
  50.         ];
  51.         if ($this->debug && null !== $trace $object->getTrace()) {
  52.             $data['trace'] = $trace;
  53.         }
  54.         return $data;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function supportsNormalization($data$format null, array $context = []): bool
  60.     {
  61.         return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException || $data instanceof LegacyFlattenException);
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function hasCacheableSupportsMethod(): bool
  67.     {
  68.         return true;
  69.     }
  70. }
  71. class_alias(ErrorNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\ErrorNormalizer::class);