XmlDriver.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
  45. if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
  46. $metadata->xmlRootName = (string) $xmlRootName;
  47. }
  48. if (!$excludeAll) {
  49. foreach ($class->getProperties() as $property) {
  50. if ($name !== $property->getDeclaringClass()->getName()) {
  51. continue;
  52. }
  53. $pMetadata = new PropertyMetadata($name, $pName = $property->getName());
  54. $isExclude = $isExpose = false;
  55. $pElems = $elem->xpath("./property[@name = '".$pName."']");
  56. if ($pElems) {
  57. $pElem = reset($pElems);
  58. if (null !== $exclude = $pElem->attributes()->exclude) {
  59. $isExclude = 'true' === (string) $exclude;
  60. }
  61. if (null !== $expose = $pElem->attributes()->expose) {
  62. $isExpose = 'true' === (string) $expose;
  63. }
  64. if (null !== $version = $pElem->attributes()->{'since-version'}) {
  65. $pMetadata->sinceVersion = (string) $version;
  66. }
  67. if (null !== $version = $pElem->attributes()->{'until-version'}) {
  68. $pMetadata->untilVersion = (string) $version;
  69. }
  70. if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
  71. $pMetadata->serializedName = (string) $serializedName;
  72. }
  73. if (null !== $type = $pElem->attributes()->type) {
  74. $pMetadata->type = (string) $type;
  75. } else if (isset($pElem->type)) {
  76. $pMetadata->type = (string) $pElem->type;
  77. }
  78. if (isset($pElem->{'xml-list'})) {
  79. $pMetadata->xmlCollection = true;
  80. $colConfig = $pElem->{'xml-list'};
  81. if (isset($colConfig->attributes()->inline)) {
  82. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  83. }
  84. if (isset($colConfig->attributes()->{'entry-name'})) {
  85. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  86. }
  87. }
  88. if (isset($pElem->{'xml-map'})) {
  89. $pMetadata->xmlCollection = true;
  90. $colConfig = $pElem->{'xml-map'};
  91. if (isset($colConfig->attributes()->inline)) {
  92. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  93. }
  94. if (isset($colConfig->attributes()->{'entry-name'})) {
  95. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  96. }
  97. if (isset($colConfig->attributes()->{'key-attribute-name'})) {
  98. $pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'};
  99. }
  100. }
  101. if (isset($pElem->attributes()->{'xml-attribute'})) {
  102. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
  103. }
  104. if (isset($pElem->attributes()->{'xml-value'})) {
  105. $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'};
  106. }
  107. $accessor = $pElem->attributes()->{'accessor'};
  108. $pMetadata->setAccessor(
  109. (string) ($pElem->attributes()->{'access-type'} ?: $classAccessType),
  110. $accessor ? (string) $accessor : null
  111. );
  112. if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
  113. || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
  114. $metadata->addPropertyMetadata($pMetadata);
  115. }
  116. }
  117. }
  118. }
  119. foreach ($elem->xpath('./callback-method') as $method) {
  120. if (!isset($method->attributes()->type)) {
  121. throw new RuntimeException('The type attribute must be set for all callback-method elements.');
  122. }
  123. if (!isset($method->attributes()->name)) {
  124. throw new RuntimeException('The name attribute must be set for all callback-method elements.');
  125. }
  126. switch ((string) $method->attributes()->type) {
  127. case 'pre-serialize':
  128. $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  129. break;
  130. case 'post-serialize':
  131. $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  132. break;
  133. case 'post-deserialize':
  134. $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  135. break;
  136. default:
  137. throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
  138. }
  139. }
  140. return $metadata;
  141. }
  142. protected function getExtension()
  143. {
  144. return 'xml';
  145. }
  146. }