FormErrorHandler.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Serializer\Handler;
  18. use JMS\SerializerBundle\Serializer\GenericSerializationVisitor;
  19. use Symfony\Component\Form\Form;
  20. use Symfony\Component\Form\FormError;
  21. use Symfony\Component\Translation\TranslatorInterface;
  22. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  23. use JMS\SerializerBundle\Serializer\VisitorInterface;
  24. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  25. class FormErrorHandler implements SerializationHandlerInterface
  26. {
  27. private $translator;
  28. public function __construct(TranslatorInterface $translator)
  29. {
  30. $this->translator = $translator;
  31. }
  32. public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
  33. {
  34. if ($data instanceof Form) {
  35. if ($visitor instanceof XmlSerializationVisitor) {
  36. $handled = true;
  37. if (null === $visitor->document) {
  38. $visitor->document = $visitor->createDocument(null, null, false);
  39. $visitor->document->appendChild($formNode = $visitor->document->createElement('form'));
  40. $visitor->setCurrentNode($formNode);
  41. } else {
  42. $visitor->getCurrentNode()->appendChild(
  43. $formNode = $visitor->document->createElement('form')
  44. );
  45. }
  46. $formNode->setAttribute('name', $data->getName());
  47. $formNode->appendChild($errorsNode = $visitor->document->createElement('errors'));
  48. foreach ($data->getErrors() as $error) {
  49. $errorNode = $visitor->document->createElement('entry');
  50. $errorNode->appendChild($this->serialize($visitor, $error, null, $visited));
  51. $errorsNode->appendChild($errorNode);
  52. }
  53. foreach ($data->getChildren() as $child) {
  54. if (null !== $node = $this->serialize($visitor, $child, null, $visited)) {
  55. $formNode->appendChild($node);
  56. }
  57. }
  58. return;
  59. } else if ($visitor instanceof GenericSerializationVisitor) {
  60. $handled = true;
  61. $isRoot = null === $visitor->getRoot();
  62. $form = $errors = array();
  63. foreach ($data->getErrors() as $error) {
  64. $errors[] = $this->serialize($visitor, $error, null, $visited);
  65. }
  66. if ($errors) {
  67. $form['errors'] = $errors;
  68. }
  69. $children = array();
  70. foreach ($data->getChildren() as $child) {
  71. $children[$child->getName()] = $this->serialize($visitor, $child, null, $visited);
  72. }
  73. if ($children) {
  74. $form['children'] = $children;
  75. }
  76. if ($isRoot) {
  77. $visitor->setRoot($form);
  78. }
  79. return $form;
  80. }
  81. } else if ($data instanceof FormError) {
  82. $handled = true;
  83. $message = $this->translator->trans($data->getMessageTemplate(), $data->getMessageParameters(), 'validators');
  84. if ($visitor instanceof XmlSerializationVisitor) {
  85. if (null === $visitor->document) {
  86. $visitor->document = $visitor->createDocument(null, null, true);
  87. }
  88. return $visitor->document->createCDATASection($message);
  89. }
  90. return $message;
  91. }
  92. return null;
  93. }
  94. }