vendor/friendsofsymfony/rest-bundle/Serializer/Normalizer/ExceptionHandler.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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. namespace FOS\RestBundle\Serializer\Normalizer;
  11. @trigger_error(sprintf('The %s\ExceptionHandler class is deprecated since FOSRestBundle 2.8.'__NAMESPACE__), E_USER_DEPRECATED);
  12. use JMS\Serializer\Context;
  13. use JMS\Serializer\GraphNavigatorInterface;
  14. use JMS\Serializer\Handler\SubscribingHandlerInterface;
  15. use JMS\Serializer\JsonSerializationVisitor;
  16. use JMS\Serializer\XmlSerializationVisitor;
  17. /**
  18.  * @deprecated since 2.8
  19.  */
  20. class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface
  21. {
  22.     public static function getSubscribingMethods()
  23.     {
  24.         return [
  25.             [
  26.                 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
  27.                 'format' => 'json',
  28.                 'type' => \Error::class,
  29.                 'method' => 'serializeErrorToJson',
  30.             ],
  31.             [
  32.                 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
  33.                 'format' => 'json',
  34.                 'type' => \Exception::class,
  35.                 'method' => 'serializeToJson',
  36.             ],
  37.             [
  38.                 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
  39.                 'format' => 'xml',
  40.                 'type' => \Error::class,
  41.                 'method' => 'serializeErrorToXml',
  42.             ],
  43.             [
  44.                 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
  45.                 'format' => 'xml',
  46.                 'type' => \Exception::class,
  47.                 'method' => 'serializeToXml',
  48.             ],
  49.         ];
  50.     }
  51.     public function serializeToJson(
  52.         JsonSerializationVisitor $visitor,
  53.         \Exception $exception,
  54.         array $type,
  55.         Context $context
  56.     ) {
  57.         $data $this->convertToArray($exception$context);
  58.         return $visitor->visitArray($data$type$context);
  59.     }
  60.     public function serializeErrorToJson(
  61.         JsonSerializationVisitor $visitor,
  62.         \Throwable $exception,
  63.         array $type,
  64.         Context $context
  65.     ) {
  66.         $data $this->convertThrowableToArray($exception$context);
  67.         return $visitor->visitArray($data$type$context);
  68.     }
  69.     public function serializeToXml(
  70.         XmlSerializationVisitor $visitor,
  71.         \Exception $exception,
  72.         array $type,
  73.         Context $context
  74.     ) {
  75.         $data $this->convertToArray($exception$context);
  76.         $document $visitor->getDocument(true);
  77.         if (!$visitor->getCurrentNode()) {
  78.             $visitor->createRoot();
  79.         }
  80.         foreach ($data as $key => $value) {
  81.             $entryNode $document->createElement($key);
  82.             $visitor->getCurrentNode()->appendChild($entryNode);
  83.             $visitor->setCurrentNode($entryNode);
  84.             $node $context->getNavigator()->accept($valuenull$context);
  85.             if (null !== $node) {
  86.                 $visitor->getCurrentNode()->appendChild($node);
  87.             }
  88.             $visitor->revertCurrentNode();
  89.         }
  90.     }
  91.     public function serializeErrorToXml(
  92.         XmlSerializationVisitor $visitor,
  93.         \Throwable $exception,
  94.         array $type,
  95.         Context $context
  96.     ) {
  97.         $data $this->convertThrowableToArray($exception$context);
  98.         $document $visitor->getDocument(true);
  99.         if (!$visitor->getCurrentNode()) {
  100.             $visitor->createRoot();
  101.         }
  102.         foreach ($data as $key => $value) {
  103.             $entryNode $document->createElement($key);
  104.             $visitor->getCurrentNode()->appendChild($entryNode);
  105.             $visitor->setCurrentNode($entryNode);
  106.             $node $context->getNavigator()->accept($valuenull$context);
  107.             if (null !== $node) {
  108.                 $visitor->getCurrentNode()->appendChild($node);
  109.             }
  110.             $visitor->revertCurrentNode();
  111.         }
  112.     }
  113.     protected function convertToArray(\Exception $exceptionContext $context)
  114.     {
  115.         return $this->convertThrowableToArray($exception$context);
  116.     }
  117.     private function convertThrowableToArray(\Throwable $throwableContext $context): array
  118.     {
  119.         $data = [];
  120.         if ($context->hasAttribute('template_data')) {
  121.             $templateData $context->getAttribute('template_data');
  122.             if (array_key_exists('status_code'$templateData)) {
  123.                 $data['code'] = $statusCode $templateData['status_code'];
  124.             } elseif ($context->hasAttribute('status_code')) {
  125.                 $data['code'] = $context->getAttribute('status_code');
  126.             }
  127.         } elseif ($context->hasAttribute('status_code')) {
  128.             $data['code'] = $context->getAttribute('status_code');
  129.         }
  130.         $data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode null);
  131.         return $data;
  132.     }
  133. }