XmlDriver.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace JMS\SerializerBundle\Metadata\Driver;
  3. use JMS\SerializerBundle\Exception\XmlErrorException;
  4. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  5. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  6. use Metadata\MethodMetadata;
  7. use JMS\SerializerBundle\Metadata\ClassMetadata;
  8. use Metadata\Driver\AbstractFileDriver;
  9. class XmlDriver extends AbstractFileDriver
  10. {
  11. protected function loadMetadataFromFile(\ReflectionClass $class, $path)
  12. {
  13. $previous = libxml_use_internal_errors(true);
  14. $elem = simplexml_load_file($path);
  15. libxml_use_internal_errors($previous);
  16. if (false === $elem) {
  17. throw new XmlErrorException(libxml_get_last_error());
  18. }
  19. $metadata = new ClassMetadata($name = $class->getName());
  20. if (!$elems = $elem->xpath("./class[@name = '".$name."']")) {
  21. throw new \RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
  22. }
  23. $elem = reset($elems);
  24. $metadata->fileResources[] = $path;
  25. $metadata->fileResources[] = $class->getFileName();
  26. $exclusionPolicy = $elem->attributes()->{'exclusion-policy'} ?: 'NONE';
  27. $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === (string) $exclude : false;
  28. if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
  29. $metadata->xmlRootName = (string) $xmlRootName;
  30. }
  31. if (!$excludeAll) {
  32. foreach ($class->getProperties() as $property) {
  33. if ($name !== $property->getDeclaringClass()->getName()) {
  34. continue;
  35. }
  36. $pMetadata = new PropertyMetadata($name, $pName = $property->getName());
  37. $isExclude = $isExpose = false;
  38. $pElems = $elem->xpath("./property[@name = '".$pName."']");
  39. if ($pElems) {
  40. $pElem = reset($pElems);
  41. if (null !== $exclude = $pElem->attributes()->exclude) {
  42. $isExclude = 'true' === (string) $exclude;
  43. }
  44. if (null !== $expose = $pElem->attributes()->expose) {
  45. $isExpose = 'true' === (string) $expose;
  46. }
  47. if (null !== $version = $pElem->attributes()->{'since-version'}) {
  48. $pMetadata->sinceVersion = (string) $version;
  49. }
  50. if (null !== $version = $pElem->attributes()->{'until-version'}) {
  51. $pMetadata->untilVersion = (string) $version;
  52. }
  53. if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
  54. $pMetadata->serializedName = (string) $serializedName;
  55. }
  56. if (null !== $type = $pElem->attributes()->type) {
  57. $pMetadata->type = (string) $type;
  58. } else if (isset($pElem->type)) {
  59. $pMetadata->type = (string) $pElem->type;
  60. }
  61. if (isset($pElem->{'xml-list'})) {
  62. $pMetadata->xmlCollection = true;
  63. $colConfig = $pElem->{'xml-list'};
  64. if (isset($colConfig->attributes()->inline)) {
  65. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  66. }
  67. if (isset($colConfig->attributes()->{'entry-name'})) {
  68. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  69. }
  70. }
  71. if (isset($pElem->{'xml-map'})) {
  72. $pMetadata->xmlCollection = true;
  73. $colConfig = $pElem->{'xml-map'};
  74. if (isset($colConfig->attributes()->inline)) {
  75. $pMetadata->xmlCollectionInline = 'true' === $colConfig->attributes()->inline;
  76. }
  77. if (isset($colConfig->attributes()->{'entry-name'})) {
  78. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  79. }
  80. if (isset($colConfig->attributes()->{'key-attribute-name'})) {
  81. $pMetadata->xmlKeyAttribute = $colConfig->attributes()->{'key-attribute-name'};
  82. }
  83. }
  84. if (isset($pElem->attributes()->{'xml-attribute'})) {
  85. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
  86. }
  87. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  88. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  89. $metadata->addPropertyMetadata($pMetadata);
  90. }
  91. }
  92. }
  93. }
  94. foreach ($elem->xpath('./callback-method') as $method) {
  95. if (!isset($method->attributes()->type)) {
  96. throw new \RuntimeException('The type attribute must be set for all callback-method elements.');
  97. }
  98. if (!isset($method->attributes()->name)) {
  99. throw new \RuntimeException('The name attribute must be set for all callback-method elements.');
  100. }
  101. switch ((string) $method->attributes()->type) {
  102. case 'pre-serialize':
  103. $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  104. break;
  105. case 'post-serialize':
  106. $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  107. break;
  108. case 'post-deserialize':
  109. $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  110. break;
  111. default:
  112. throw new \RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
  113. }
  114. }
  115. return $metadata;
  116. }
  117. protected function getExtension()
  118. {
  119. return 'xml';
  120. }
  121. }