XmlDriver.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = strtoupper($elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
  43. $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower($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' === strtolower($exclude);
  63. }
  64. if (null !== $expose = $pElem->attributes()->expose) {
  65. $isExpose = 'true' === strtolower($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. $getter = $pElem->attributes()->{'accessor-getter'};
  111. $setter = $pElem->attributes()->{'accessor-setter'};
  112. $pMetadata->setAccessor(
  113. (string) ($pElem->attributes()->{'access-type'} ?: $classAccessType),
  114. $getter ? (string) $getter : null,
  115. $setter ? (string) $setter : null
  116. );
  117. if (null !== $inline = $pElem->attributes()->inline) {
  118. $pMetadata->inline = 'true' === strtolower($inline);
  119. }
  120. }
  121. if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
  122. || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
  123. $metadata->addPropertyMetadata($pMetadata);
  124. }
  125. }
  126. }
  127. foreach ($elem->xpath('./callback-method') as $method) {
  128. if (!isset($method->attributes()->type)) {
  129. throw new RuntimeException('The type attribute must be set for all callback-method elements.');
  130. }
  131. if (!isset($method->attributes()->name)) {
  132. throw new RuntimeException('The name attribute must be set for all callback-method elements.');
  133. }
  134. switch ((string) $method->attributes()->type) {
  135. case 'pre-serialize':
  136. $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  137. break;
  138. case 'post-serialize':
  139. $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  140. break;
  141. case 'post-deserialize':
  142. $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  143. break;
  144. default:
  145. throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
  146. }
  147. }
  148. return $metadata;
  149. }
  150. protected function getExtension()
  151. {
  152. return 'xml';
  153. }
  154. }