XmlDriver.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  23. use Metadata\MethodMetadata;
  24. use JMS\SerializerBundle\Metadata\ClassMetadata;
  25. use Metadata\Driver\AbstractFileDriver;
  26. class XmlDriver extends AbstractFileDriver
  27. {
  28. protected function loadMetadataFromFile(\ReflectionClass $class, $path)
  29. {
  30. $previous = libxml_use_internal_errors(true);
  31. $elem = simplexml_load_file($path);
  32. libxml_use_internal_errors($previous);
  33. if (false === $elem) {
  34. throw new XmlErrorException(libxml_get_last_error());
  35. }
  36. $metadata = new ClassMetadata($name = $class->getName());
  37. if (!$elems = $elem->xpath("./class[@name = '".$name."']")) {
  38. throw new RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
  39. }
  40. $elem = reset($elems);
  41. $metadata->fileResources[] = $path;
  42. $metadata->fileResources[] = $class->getFileName();
  43. $exclusionPolicy = strtoupper($elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
  44. $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower($exclude) : false;
  45. $classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
  46. $propertiesMetadata = array();
  47. $propertiesNodes = array();
  48. if (null !== $accessorOrder = $elem->attributes()->{'accessor-order'}) {
  49. $metadata->setAccessorOrder((string) $accessorOrder, preg_split('/\s*,\s*/', (string) $elem->attributes()->{'custom-accessor-order'}));
  50. }
  51. if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
  52. $metadata->xmlRootName = (string) $xmlRootName;
  53. }
  54. foreach ($elem->xpath('./virtual-property') as $method) {
  55. if (!isset($method->attributes()->method)) {
  56. throw new RuntimeException('The method attribute must be set for all virtual-property elements.');
  57. }
  58. $virtualPropertyMetadata = new VirtualPropertyMetadata( $name, (string) $method->attributes()->method );
  59. $propertiesMetadata[] = $virtualPropertyMetadata;
  60. $propertiesNodes[] = $method;
  61. }
  62. if (!$excludeAll) {
  63. foreach ($class->getProperties() as $property) {
  64. if ($name !== $property->getDeclaringClass()->getName()) {
  65. continue;
  66. }
  67. $propertiesMetadata[] = new PropertyMetadata($name, $pName = $property->getName());
  68. $pElems = $elem->xpath("./property[@name = '".$pName."']");
  69. $propertiesNodes[] = $pElems ? reset( $pElems ) : null;
  70. }
  71. foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
  72. $isExclude = false;
  73. $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
  74. $pElem = $propertiesNodes[$propertyKey];
  75. if (!empty( $pElem )) {
  76. if (null !== $exclude = $pElem->attributes()->exclude) {
  77. $isExclude = 'true' === strtolower($exclude);
  78. }
  79. if (null !== $expose = $pElem->attributes()->expose) {
  80. $isExpose = 'true' === strtolower($expose);
  81. }
  82. if (null !== $version = $pElem->attributes()->{'since-version'}) {
  83. $pMetadata->sinceVersion = (string) $version;
  84. }
  85. if (null !== $version = $pElem->attributes()->{'until-version'}) {
  86. $pMetadata->untilVersion = (string) $version;
  87. }
  88. if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
  89. $pMetadata->serializedName = (string) $serializedName;
  90. }
  91. if (null !== $type = $pElem->attributes()->type) {
  92. $pMetadata->type = (string) $type;
  93. } else if (isset($pElem->type)) {
  94. $pMetadata->type = (string) $pElem->type;
  95. }
  96. if (null !== $groups = $pElem->attributes()->groups) {
  97. $pMetadata->groups = preg_split('/\s*,\s*/', (string) $groups);
  98. }
  99. if (isset($pElem->{'xml-list'})) {
  100. $pMetadata->xmlCollection = true;
  101. $colConfig = $pElem->{'xml-list'};
  102. if (isset($colConfig->attributes()->inline)) {
  103. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  104. }
  105. if (isset($colConfig->attributes()->{'entry-name'})) {
  106. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  107. }
  108. }
  109. if (isset($pElem->{'xml-map'})) {
  110. $pMetadata->xmlCollection = true;
  111. $colConfig = $pElem->{'xml-map'};
  112. if (isset($colConfig->attributes()->inline)) {
  113. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  114. }
  115. if (isset($colConfig->attributes()->{'entry-name'})) {
  116. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  117. }
  118. if (isset($colConfig->attributes()->{'key-attribute-name'})) {
  119. $pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'};
  120. }
  121. }
  122. if (isset($pElem->attributes()->{'xml-attribute'})) {
  123. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
  124. }
  125. if (isset($pElem->attributes()->{'xml-value'})) {
  126. $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'};
  127. }
  128. if (isset($pElem->attributes()->{'xml-key-value-pairs'})) {
  129. $pMetadata->xmlKeyValuePairs = 'true' === (string) $pElem->attributes()->{'xml-key-value-pairs'};
  130. }
  131. $getter = $pElem->attributes()->{'accessor-getter'};
  132. $setter = $pElem->attributes()->{'accessor-setter'};
  133. $pMetadata->setAccessor(
  134. (string) ($pElem->attributes()->{'access-type'} ?: $classAccessType),
  135. $getter ? (string) $getter : null,
  136. $setter ? (string) $setter : null
  137. );
  138. if (null !== $inline = $pElem->attributes()->inline) {
  139. $pMetadata->inline = 'true' === strtolower($inline);
  140. }
  141. if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
  142. $pMetadata->readOnly = 'true' === strtolower($readOnly);
  143. }
  144. }
  145. if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
  146. || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
  147. $metadata->addPropertyMetadata($pMetadata);
  148. }
  149. }
  150. }
  151. foreach ($elem->xpath('./callback-method') as $method) {
  152. if (!isset($method->attributes()->type)) {
  153. throw new RuntimeException('The type attribute must be set for all callback-method elements.');
  154. }
  155. if (!isset($method->attributes()->name)) {
  156. throw new RuntimeException('The name attribute must be set for all callback-method elements.');
  157. }
  158. switch ((string) $method->attributes()->type) {
  159. case 'pre-serialize':
  160. $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  161. break;
  162. case 'post-serialize':
  163. $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  164. break;
  165. case 'post-deserialize':
  166. $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  167. break;
  168. default:
  169. throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
  170. }
  171. }
  172. return $metadata;
  173. }
  174. protected function getExtension()
  175. {
  176. return 'xml';
  177. }
  178. }