ConstraintViolationHandler.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. public 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. } else {
  46. $visitor->getCurrentNode()->appendChild(
  47. $violationNode = $visitor->document->createElement('violation')
  48. );
  49. }
  50. $violationNode->setAttribute('property_path', $data->getPropertyPath());
  51. $violationNode->appendChild($messageNode = $visitor->document->createElement('message'));
  52. $messageNode->appendChild($visitor->document->createCDATASection($data->getMessage()));
  53. return;
  54. } else if ($visitor instanceof GenericSerializationVisitor) {
  55. $handled = true;
  56. $violation = array(
  57. 'property_path' => $data->getPropertyPath(),
  58. 'message' =>$data->getMessage()
  59. );
  60. if (null === $visitor->getRoot()) {
  61. $visitor->setRoot($violation);
  62. }
  63. return $violation;
  64. }
  65. }
  66. return;
  67. }
  68. }