ConstraintViolationHandler.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Validator\ConstraintViolation;
  19. use Symfony\Component\Validator\ConstraintViolationList;
  20. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  21. use JMS\SerializerBundle\Serializer\VisitorInterface;
  22. use JMS\SerializerBundle\Serializer\GenericSerializationVisitor;
  23. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  24. class ConstraintViolationHandler implements SerializationHandlerInterface
  25. {
  26. function serialize(VisitorInterface $visitor, $data, $type, &$handled)
  27. {
  28. if ($data instanceof ConstraintViolationList) {
  29. if ($visitor instanceof XmlSerializationVisitor) {
  30. $handled = true;
  31. if (null === $visitor->document) {
  32. $visitor->document = $visitor->createDocument();
  33. }
  34. foreach ($data as $violation) {
  35. $this->serialize($visitor, $violation, null, $visited);
  36. }
  37. }
  38. } else if ($data instanceof ConstraintViolation) {
  39. if ($visitor instanceof XmlSerializationVisitor) {
  40. $handled = true;
  41. if (null === $visitor->document) {
  42. $visitor->document = $visitor->createDocument(null, null, false);
  43. $visitor->document->appendChild($violationNode = $visitor->document->createElement('violation'));
  44. $visitor->setCurrentNode($violationNode);
  45. }
  46. else {
  47. $visitor->getCurrentNode()->appendChild(
  48. $violationNode = $visitor->document->createElement('violation')
  49. );
  50. }
  51. $violationNode->setAttribute('property_path', $data->getPropertyPath());
  52. $violationNode->appendChild($messageNode = $visitor->document->createElement('message'));
  53. $messageNode->appendChild($visitor->document->createCDATASection($data->getMessage()));
  54. return;
  55. } else if ($visitor instanceof GenericSerializationVisitor) {
  56. $handled = true;
  57. $violation = array(
  58. 'property_path' => $data->getPropertyPath(),
  59. 'message' =>$data->getMessage()
  60. );
  61. if (null === $visitor->getRoot()) {
  62. $visitor->setRoot($violation);
  63. }
  64. return $violation;
  65. }
  66. }
  67. return;
  68. }
  69. }