PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/jms/serializer/src/JMS/Serializer/Metadata/Driver/XmlDriver.php

https://gitlab.com/freebird/WebApp
PHP | 301 lines | 220 code | 65 blank | 16 comment | 50 complexity | 33372745044eafb7badbe78bd2c46c05 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2013 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\Serializer\Metadata\Driver;
  18. use JMS\Serializer\GraphNavigator;
  19. use JMS\Serializer\Exception\RuntimeException;
  20. use JMS\Serializer\Exception\XmlErrorException;
  21. use JMS\Serializer\Annotation\ExclusionPolicy;
  22. use JMS\Serializer\Metadata\PropertyMetadata;
  23. use JMS\Serializer\Metadata\VirtualPropertyMetadata;
  24. use Metadata\MethodMetadata;
  25. use JMS\Serializer\Metadata\ClassMetadata;
  26. use Metadata\Driver\AbstractFileDriver;
  27. class XmlDriver extends AbstractFileDriver
  28. {
  29. protected function loadMetadataFromFile(\ReflectionClass $class, $path)
  30. {
  31. $previous = libxml_use_internal_errors(true);
  32. $elem = simplexml_load_file($path);
  33. libxml_use_internal_errors($previous);
  34. if (false === $elem) {
  35. throw new XmlErrorException(libxml_get_last_error());
  36. }
  37. $metadata = new ClassMetadata($name = $class->name);
  38. if (!$elems = $elem->xpath("./class[@name = '".$name."']")) {
  39. throw new RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
  40. }
  41. $elem = reset($elems);
  42. $metadata->fileResources[] = $path;
  43. $metadata->fileResources[] = $class->getFileName();
  44. $exclusionPolicy = strtoupper($elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
  45. $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower($exclude) : false;
  46. $classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
  47. $propertiesMetadata = array();
  48. $propertiesNodes = array();
  49. if (null !== $accessorOrder = $elem->attributes()->{'accessor-order'}) {
  50. $metadata->setAccessorOrder((string) $accessorOrder, preg_split('/\s*,\s*/', (string) $elem->attributes()->{'custom-accessor-order'}));
  51. }
  52. if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
  53. $metadata->xmlRootName = (string) $xmlRootName;
  54. }
  55. if (null !== $xmlRootNamespace = $elem->attributes()->{'xml-root-namespace'}) {
  56. $metadata->xmlRootNamespace = (string) $xmlRootNamespace;
  57. }
  58. $readOnlyClass = 'true' === strtolower($elem->attributes()->{'read-only'});
  59. $discriminatorFieldName = (string) $elem->attributes()->{'discriminator-field-name'};
  60. $discriminatorMap = array();
  61. foreach ($elem->xpath('./discriminator-class') as $entry) {
  62. if ( ! isset($entry->attributes()->value)) {
  63. throw new RuntimeException('Each discriminator-class element must have a "value" attribute.');
  64. }
  65. $discriminatorMap[(string) $entry->attributes()->value] = (string) $entry;
  66. }
  67. if ('true' === (string) $elem->attributes()->{'discriminator-disabled'}) {
  68. $metadata->discriminatorDisabled = true;
  69. } elseif ( ! empty($discriminatorFieldName) || ! empty($discriminatorMap)) {
  70. $metadata->setDiscriminator($discriminatorFieldName, $discriminatorMap);
  71. }
  72. foreach ($elem->xpath('./xml-namespace') as $xmlNamespace) {
  73. if (!isset($xmlNamespace->attributes()->uri)) {
  74. throw new RuntimeException('The prefix attribute must be set for all xml-namespace elements.');
  75. }
  76. if (isset($xmlNamespace->attributes()->prefix)) {
  77. $prefix = (string) $xmlNamespace->attributes()->prefix;
  78. } else {
  79. $prefix = null;
  80. }
  81. $metadata->registerNamespace((string) $xmlNamespace->attributes()->uri, $prefix);
  82. }
  83. foreach ($elem->xpath('./virtual-property') as $method) {
  84. if (!isset($method->attributes()->method)) {
  85. throw new RuntimeException('The method attribute must be set for all virtual-property elements.');
  86. }
  87. $virtualPropertyMetadata = new VirtualPropertyMetadata( $name, (string) $method->attributes()->method );
  88. $propertiesMetadata[] = $virtualPropertyMetadata;
  89. $propertiesNodes[] = $method;
  90. }
  91. if (!$excludeAll) {
  92. foreach ($class->getProperties() as $property) {
  93. if ($name !== $property->class) {
  94. continue;
  95. }
  96. $propertiesMetadata[] = new PropertyMetadata($name, $pName = $property->getName());
  97. $pElems = $elem->xpath("./property[@name = '".$pName."']");
  98. $propertiesNodes[] = $pElems ? reset( $pElems ) : null;
  99. }
  100. foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
  101. $isExclude = false;
  102. $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
  103. $pElem = $propertiesNodes[$propertyKey];
  104. if (!empty( $pElem )) {
  105. if (null !== $exclude = $pElem->attributes()->exclude) {
  106. $isExclude = 'true' === strtolower($exclude);
  107. }
  108. if (null !== $expose = $pElem->attributes()->expose) {
  109. $isExpose = 'true' === strtolower($expose);
  110. }
  111. if (null !== $version = $pElem->attributes()->{'since-version'}) {
  112. $pMetadata->sinceVersion = (string) $version;
  113. }
  114. if (null !== $version = $pElem->attributes()->{'until-version'}) {
  115. $pMetadata->untilVersion = (string) $version;
  116. }
  117. if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
  118. $pMetadata->serializedName = (string) $serializedName;
  119. }
  120. if (null !== $type = $pElem->attributes()->type) {
  121. $pMetadata->setType((string) $type);
  122. } elseif (isset($pElem->type)) {
  123. $pMetadata->setType((string) $pElem->type);
  124. }
  125. if (null !== $groups = $pElem->attributes()->groups) {
  126. $pMetadata->groups = preg_split('/\s*,\s*/', (string) $groups);
  127. }
  128. if (isset($pElem->{'xml-list'})) {
  129. $pMetadata->xmlCollection = true;
  130. $colConfig = $pElem->{'xml-list'};
  131. if (isset($colConfig->attributes()->inline)) {
  132. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  133. }
  134. if (isset($colConfig->attributes()->{'entry-name'})) {
  135. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  136. }
  137. }
  138. if (isset($pElem->{'xml-map'})) {
  139. $pMetadata->xmlCollection = true;
  140. $colConfig = $pElem->{'xml-map'};
  141. if (isset($colConfig->attributes()->inline)) {
  142. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  143. }
  144. if (isset($colConfig->attributes()->{'entry-name'})) {
  145. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  146. }
  147. if (isset($colConfig->attributes()->{'key-attribute-name'})) {
  148. $pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'};
  149. }
  150. }
  151. if (isset($pElem->{'xml-element'})) {
  152. $colConfig = $pElem->{'xml-element'};
  153. if (isset($colConfig->attributes()->cdata)) {
  154. $pMetadata->xmlElementCData = 'true' === (string) $colConfig->attributes()->cdata;
  155. }
  156. if (isset($colConfig->attributes()->namespace)) {
  157. $pMetadata->xmlNamespace = (string) $colConfig->attributes()->namespace;
  158. }
  159. }
  160. if (isset($pElem->attributes()->{'xml-attribute'})) {
  161. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
  162. }
  163. if (isset($pElem->attributes()->{'xml-attribute-map'})) {
  164. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute-map'};
  165. }
  166. if (isset($pElem->attributes()->{'xml-value'})) {
  167. $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'};
  168. }
  169. if (isset($pElem->attributes()->{'xml-key-value-pairs'})) {
  170. $pMetadata->xmlKeyValuePairs = 'true' === (string) $pElem->attributes()->{'xml-key-value-pairs'};
  171. }
  172. if (isset($pElem->attributes()->{'max-depth'})) {
  173. $pMetadata->maxDepth = (int) $pElem->attributes()->{'max-depth'};
  174. }
  175. //we need read-only before setter and getter set, because that method depends on flag being set
  176. if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
  177. $pMetadata->readOnly = 'true' === strtolower($readOnly);
  178. } else {
  179. $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass;
  180. }
  181. $getter = $pElem->attributes()->{'accessor-getter'};
  182. $setter = $pElem->attributes()->{'accessor-setter'};
  183. $pMetadata->setAccessor(
  184. (string) ($pElem->attributes()->{'access-type'} ?: $classAccessType),
  185. $getter ? (string) $getter : null,
  186. $setter ? (string) $setter : null
  187. );
  188. if (null !== $inline = $pElem->attributes()->inline) {
  189. $pMetadata->inline = 'true' === strtolower($inline);
  190. }
  191. }
  192. if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
  193. || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
  194. $metadata->addPropertyMetadata($pMetadata);
  195. }
  196. }
  197. }
  198. foreach ($elem->xpath('./callback-method') as $method) {
  199. if (!isset($method->attributes()->type)) {
  200. throw new RuntimeException('The type attribute must be set for all callback-method elements.');
  201. }
  202. if (!isset($method->attributes()->name)) {
  203. throw new RuntimeException('The name attribute must be set for all callback-method elements.');
  204. }
  205. switch ((string) $method->attributes()->type) {
  206. case 'pre-serialize':
  207. $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  208. break;
  209. case 'post-serialize':
  210. $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  211. break;
  212. case 'post-deserialize':
  213. $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  214. break;
  215. case 'handler':
  216. if ( ! isset($method->attributes()->format)) {
  217. throw new RuntimeException('The format attribute must be set for "handler" callback methods.');
  218. }
  219. if ( ! isset($method->attributes()->direction)) {
  220. throw new RuntimeException('The direction attribute must be set for "handler" callback methods.');
  221. }
  222. $direction = GraphNavigator::parseDirection((string) $method->attributes()->direction);
  223. $format = (string) $method->attributes()->format;
  224. $metadata->addHandlerCallback($direction, $format, (string) $method->attributes()->name);
  225. break;
  226. default:
  227. throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
  228. }
  229. }
  230. return $metadata;
  231. }
  232. protected function getExtension()
  233. {
  234. return 'xml';
  235. }
  236. }