FormErrorHandler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Symfony\Component\Form\Form;
  19. use Symfony\Component\Form\FormError;
  20. use Symfony\Component\Translation\TranslatorInterface;
  21. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  22. use JMS\SerializerBundle\Serializer\VisitorInterface;
  23. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  24. class FormErrorHandler implements SerializationHandlerInterface
  25. {
  26. private $translator;
  27. public function __construct(TranslatorInterface $translator)
  28. {
  29. $this->translator = $translator;
  30. }
  31. public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
  32. {
  33. if ($data instanceof Form) {
  34. $handled = true;
  35. if ($visitor instanceof XmlSerializationVisitor) {
  36. if (null === $visitor->document) {
  37. $visitor->document = $visitor->createDocument(null, null, false);
  38. }
  39. $formNode = $visitor->document->createElement('form');
  40. $formNode->setAttribute('name', $data->getName());
  41. if (null === $visitor->getCurrentNode()) {
  42. $visitor->document->appendChild($formNode);
  43. }
  44. else {
  45. $visitor->getCurrentNode()->appendChild($formNode);
  46. }
  47. // append errors node
  48. $formNode->appendChild($errorsNode = $visitor->document->createElement('errors'));
  49. $visitor->setCurrentNode($errorsNode);
  50. $visitor->visitArray($data->getErrors(), $type);
  51. $visitor->revertCurrentNode();
  52. // if has nested form, append children node
  53. if ($data->hasChildren()) {
  54. $formNode->appendChild($childrenNode = $visitor->document->createElement('children'));
  55. $visitor->setCurrentNode($childrenNode);
  56. foreach ($data->getChildren() as $child) {
  57. $this->serialize($visitor, $child, $type, $visited);
  58. }
  59. $visitor->revertCurrentNode();
  60. }
  61. return $formNode;
  62. }
  63. else {
  64. $form = array('errors' => $data->getErrors());
  65. if ($data->hasChildren()) {
  66. $form['children'] = $data->getChildren();
  67. }
  68. return $visitor->visitArray($form, $type);
  69. }
  70. }
  71. else if ($data instanceof FormError) {
  72. $handled = true;
  73. $message = $this->translator->trans($data->getMessageTemplate(), $data->getMessageParameters(), 'validators');
  74. if ($visitor instanceof XmlSerializationVisitor) {
  75. if (null === $visitor->document) {
  76. $visitor->document = $visitor->createDocument(null, null, true);
  77. }
  78. return $visitor->document->createCDATASection($message);
  79. }
  80. return $message;
  81. }
  82. return null;
  83. }
  84. }