vendor/friendsofsymfony/rest-bundle/Serializer/Normalizer/FormErrorHandler.php line 87

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. use JMS\Serializer\Context;
  12. use JMS\Serializer\Handler\FormErrorHandler as JMSFormErrorHandler;
  13. use JMS\Serializer\Handler\SubscribingHandlerInterface;
  14. use JMS\Serializer\JsonSerializationVisitor;
  15. use JMS\Serializer\Visitor\SerializationVisitorInterface;
  16. use JMS\Serializer\XmlSerializationVisitor;
  17. use Symfony\Component\Form\Form;
  18. use JMS\Serializer\YamlSerializationVisitor;
  19. /**
  20. * Extend the JMS FormErrorHandler to include more information when using the ViewHandler.
  21. *
  22. * @internal
  23. */
  24. class FormErrorHandler implements SubscribingHandlerInterface
  25. {
  26. private $formErrorHandler;
  27. public function __construct(JMSFormErrorHandler $formErrorHandler)
  28. {
  29. $this->formErrorHandler = $formErrorHandler;
  30. }
  31. public static function getSubscribingMethods(): array
  32. {
  33. return JMSFormErrorHandler::getSubscribingMethods();
  34. }
  35. public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
  36. {
  37. if ($context) {
  38. if ($context->hasAttribute('status_code')) {
  39. $document = $visitor->getDocument(true);
  40. if (!$visitor->getCurrentNode()) {
  41. $visitor->createRoot();
  42. }
  43. $codeNode = $document->createElement('code');
  44. $visitor->getCurrentNode()->appendChild($codeNode);
  45. $codeNode->appendChild($context->getNavigator()->accept($context->getAttribute('status_code'), null, $context));
  46. $messageNode = $document->createElement('message');
  47. $visitor->getCurrentNode()->appendChild($messageNode);
  48. $messageNode->appendChild($context->getNavigator()->accept('Validation Failed', null, $context));
  49. $errorsNode = $document->createElement('errors');
  50. $visitor->getCurrentNode()->appendChild($errorsNode);
  51. $visitor->setCurrentNode($errorsNode);
  52. $errorNodes = $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
  53. $errorsNode->appendChild($errorNodes);
  54. $visitor->revertCurrentNode();
  55. return $visitor->getCurrentNode();
  56. }
  57. }
  58. return $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
  59. }
  60. public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
  61. {
  62. $isRoot = !interface_exists(SerializationVisitorInterface::class) && null === $visitor->getRoot();
  63. $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToJson($visitor, $form, $type), $context);
  64. if ($isRoot) {
  65. $visitor->setRoot($result);
  66. }
  67. return $result;
  68. }
  69. public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
  70. {
  71. $isRoot = null === $visitor->getRoot();
  72. $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToYml($visitor, $form, $type), $context);
  73. if ($isRoot) {
  74. $visitor->setRoot($result);
  75. }
  76. return $result;
  77. }
  78. public function __call($name, $arguments)
  79. {
  80. return call_user_func_array([$this->formErrorHandler, $name], $arguments);
  81. }
  82. private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null)
  83. {
  84. $statusCode = $this->getStatusCode($context);
  85. if (null !== $statusCode) {
  86. return [
  87. 'code' => $statusCode,
  88. 'message' => 'Validation Failed',
  89. 'errors' => $serializedForm,
  90. ];
  91. }
  92. return $serializedForm;
  93. }
  94. private function getStatusCode(Context $context = null)
  95. {
  96. if (null === $context) {
  97. return;
  98. }
  99. if ($context->hasAttribute('status_code')) {
  100. return $context->getAttribute('status_code');
  101. }
  102. }
  103. }