ConstraintViolationHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\YamlSerializationVisitor;
  19. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  20. use JMS\SerializerBundle\Serializer\GraphNavigator;
  21. use Symfony\Component\Validator\ConstraintViolation;
  22. use Symfony\Component\Validator\ConstraintViolationList;
  23. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  24. class ConstraintViolationHandler implements SubscribingHandlerInterface
  25. {
  26. public static function getSubscribingMethods()
  27. {
  28. $methods = array();
  29. $formats = array('xml', 'json', 'yml');
  30. $types = array('Symfony\Component\Validator\ConstraintViolationList' => 'serializeList', 'Symfony\Component\Validator\ConstraintViolation' => 'serializeViolation');
  31. foreach ($types as $type => $method) {
  32. foreach ($formats as $format) {
  33. $methods[] = array(
  34. 'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
  35. 'type' => $type,
  36. 'format' => $format,
  37. 'method' => $method.'To'.$format,
  38. );
  39. }
  40. }
  41. return $methods;
  42. }
  43. public function serializeListToXml(XmlSerializationVisitor $visitor, ConstraintViolationList $list, array $type)
  44. {
  45. if (null === $visitor->document) {
  46. $visitor->document = $visitor->createDocument();
  47. }
  48. foreach ($list as $violation) {
  49. $this->serializeViolationToXml($visitor, $violation);
  50. }
  51. }
  52. public function serializeListToJson(JsonSerializationVisitor $visitor, ConstraintViolationList $list, array $type)
  53. {
  54. return $visitor->visitArray(iterator_to_array($list), $type);
  55. }
  56. public function serializeListToYml(YamlSerializationVisitor $visitor, ConstraintViolationList $list, array $type)
  57. {
  58. return $visitor->visitArray(iterator_to_array($list), $type);
  59. }
  60. public function serializeViolationToXml(XmlSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
  61. {
  62. if (null === $visitor->document) {
  63. $visitor->document = $visitor->createDocument(null, null, false);
  64. $visitor->document->appendChild($violationNode = $visitor->document->createElement('violation'));
  65. $visitor->setCurrentNode($violationNode);
  66. } else {
  67. $visitor->getCurrentNode()->appendChild(
  68. $violationNode = $visitor->document->createElement('violation')
  69. );
  70. }
  71. $violationNode->setAttribute('property_path', $violation->getPropertyPath());
  72. $violationNode->appendChild($messageNode = $visitor->document->createElement('message'));
  73. $messageNode->appendChild($visitor->document->createCDATASection($violation->getMessage()));
  74. }
  75. public function serializeViolationToJson(JsonSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
  76. {
  77. $data = array(
  78. 'property_path' => $violation->getPropertyPath(),
  79. 'message' => $violation->getMessage()
  80. );
  81. if (null === $visitor->getRoot()) {
  82. $visitor->setRoot($data);
  83. }
  84. return $data;
  85. }
  86. public function serializeViolationToYml(YamlSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
  87. {
  88. return array(
  89. 'property_path' => $violation->getPropertyPath(),
  90. 'message' => $violation->getMessage(),
  91. );
  92. }
  93. }