XmlDriver.php 7.8 KB

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