XmlDriver.php 7.2 KB

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