PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/freebird/WebApp
PHP | 289 lines | 217 code | 56 blank | 16 comment | 58 complexity | 74da43e8cef25fcb24551be57d0fc8be 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\Annotation\ExclusionPolicy;
  21. use Metadata\MethodMetadata;
  22. use JMS\Serializer\Metadata\PropertyMetadata;
  23. use JMS\Serializer\Metadata\VirtualPropertyMetadata;
  24. use JMS\Serializer\Metadata\ClassMetadata;
  25. use Symfony\Component\Yaml\Yaml;
  26. use Metadata\Driver\AbstractFileDriver;
  27. class YamlDriver extends AbstractFileDriver
  28. {
  29. protected function loadMetadataFromFile(\ReflectionClass $class, $file)
  30. {
  31. $config = Yaml::parse(file_get_contents($file));
  32. if (!isset($config[$name = $class->name])) {
  33. throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file));
  34. }
  35. $config = $config[$name];
  36. $metadata = new ClassMetadata($name);
  37. $metadata->fileResources[] = $file;
  38. $metadata->fileResources[] = $class->getFileName();
  39. $exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE';
  40. $excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false;
  41. $classAccessType = isset($config['access_type']) ? $config['access_type'] : PropertyMetadata::ACCESS_TYPE_PROPERTY;
  42. $readOnlyClass = isset($config['read_only']) ? (Boolean) $config['read_only'] : false;
  43. $this->addClassProperties($metadata, $config);
  44. $propertiesMetadata = array();
  45. if (array_key_exists('virtual_properties', $config) ) {
  46. foreach ( $config['virtual_properties'] as $methodName => $propertySettings ) {
  47. if ( ! $class->hasMethod( $methodName ) ) {
  48. throw new RuntimeException('The method '.$methodName.' not found in class ' . $class->name);
  49. }
  50. $virtualPropertyMetadata = new VirtualPropertyMetadata( $name, $methodName );
  51. $propertiesMetadata[$methodName] = $virtualPropertyMetadata;
  52. $config['properties'][$methodName] = $propertySettings;
  53. }
  54. }
  55. if ( ! $excludeAll) {
  56. foreach ($class->getProperties() as $property) {
  57. if ($name !== $property->class) {
  58. continue;
  59. }
  60. $pName = $property->getName();
  61. $propertiesMetadata[$pName] = new PropertyMetadata($name, $pName);
  62. }
  63. foreach ($propertiesMetadata as $pName => $pMetadata) {
  64. $isExclude = false;
  65. $isExpose = $pMetadata instanceof VirtualPropertyMetadata
  66. || (isset($config['properties']) && array_key_exists($pName, $config['properties']));
  67. if (isset($config['properties'][$pName])) {
  68. $pConfig = $config['properties'][$pName];
  69. if (isset($pConfig['exclude'])) {
  70. $isExclude = (Boolean) $pConfig['exclude'];
  71. }
  72. if (isset($pConfig['expose'])) {
  73. $isExpose = (Boolean) $pConfig['expose'];
  74. }
  75. if (isset($pConfig['since_version'])) {
  76. $pMetadata->sinceVersion = (string) $pConfig['since_version'];
  77. }
  78. if (isset($pConfig['until_version'])) {
  79. $pMetadata->untilVersion = (string) $pConfig['until_version'];
  80. }
  81. if (isset($pConfig['serialized_name'])) {
  82. $pMetadata->serializedName = (string) $pConfig['serialized_name'];
  83. }
  84. if (isset($pConfig['type'])) {
  85. $pMetadata->setType((string) $pConfig['type']);
  86. }
  87. if (isset($pConfig['groups'])) {
  88. $pMetadata->groups = $pConfig['groups'];
  89. }
  90. if (isset($pConfig['xml_list'])) {
  91. $pMetadata->xmlCollection = true;
  92. $colConfig = $pConfig['xml_list'];
  93. if (isset($colConfig['inline'])) {
  94. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  95. }
  96. if (isset($colConfig['entry_name'])) {
  97. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  98. }
  99. }
  100. if (isset($pConfig['xml_map'])) {
  101. $pMetadata->xmlCollection = true;
  102. $colConfig = $pConfig['xml_map'];
  103. if (isset($colConfig['inline'])) {
  104. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  105. }
  106. if (isset($colConfig['entry_name'])) {
  107. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  108. }
  109. if (isset($colConfig['key_attribute_name'])) {
  110. $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
  111. }
  112. }
  113. if (isset($pConfig['xml_element'])) {
  114. $colConfig = $pConfig['xml_element'];
  115. if (isset($colConfig['cdata'])) {
  116. $pMetadata->xmlElementCData = (Boolean) $colConfig['cdata'];
  117. }
  118. if (isset($colConfig['namespace'])) {
  119. $pMetadata->xmlNamespace = (string) $colConfig['namespace'];
  120. }
  121. }
  122. if (isset($pConfig['xml_attribute'])) {
  123. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
  124. }
  125. if (isset($pConfig['xml_attribute_map'])) {
  126. $pMetadata->xmlAttributeMap = (Boolean) $pConfig['xml_attribute_map'];
  127. }
  128. if (isset($pConfig['xml_value'])) {
  129. $pMetadata->xmlValue = (Boolean) $pConfig['xml_value'];
  130. }
  131. if (isset($pConfig['xml_key_value_pairs'])) {
  132. $pMetadata->xmlKeyValuePairs = (Boolean) $pConfig['xml_key_value_pairs'];
  133. }
  134. //we need read_only before setter and getter set, because that method depends on flag being set
  135. if (isset($pConfig['read_only'])) {
  136. $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
  137. } else {
  138. $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass;
  139. }
  140. $pMetadata->setAccessor(
  141. isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
  142. isset($pConfig['accessor']['getter']) ? $pConfig['accessor']['getter'] : null,
  143. isset($pConfig['accessor']['setter']) ? $pConfig['accessor']['setter'] : null
  144. );
  145. if (isset($pConfig['inline'])) {
  146. $pMetadata->inline = (Boolean) $pConfig['inline'];
  147. }
  148. if (isset($pConfig['max_depth'])) {
  149. $pMetadata->maxDepth = (int) $pConfig['max_depth'];
  150. }
  151. }
  152. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  153. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  154. $metadata->addPropertyMetadata($pMetadata);
  155. }
  156. }
  157. }
  158. if (isset($config['handler_callbacks'])) {
  159. foreach ($config['handler_callbacks'] as $direction => $formats) {
  160. foreach ($formats as $format => $methodName) {
  161. $direction = GraphNavigator::parseDirection($direction);
  162. $metadata->addHandlerCallback($direction, $format, $methodName);
  163. }
  164. }
  165. }
  166. if (isset($config['callback_methods'])) {
  167. $cConfig = $config['callback_methods'];
  168. if (isset($cConfig['pre_serialize'])) {
  169. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  170. }
  171. if (isset($cConfig['post_serialize'])) {
  172. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  173. }
  174. if (isset($cConfig['post_deserialize'])) {
  175. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  176. }
  177. }
  178. return $metadata;
  179. }
  180. protected function getExtension()
  181. {
  182. return 'yml';
  183. }
  184. private function addClassProperties(ClassMetadata $metadata, array $config)
  185. {
  186. if (isset($config['custom_accessor_order']) && ! isset($config['accessor_order'])) {
  187. $config['accessor_order'] = 'custom';
  188. }
  189. if (isset($config['accessor_order'])) {
  190. $metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
  191. }
  192. if (isset($config['xml_root_name'])) {
  193. $metadata->xmlRootName = (string) $config['xml_root_name'];
  194. }
  195. if (isset($config['xml_root_namespace'])) {
  196. $metadata->xmlRootNamespace = (string) $config['xml_root_namespace'];
  197. }
  198. if (array_key_exists('xml_namespaces', $config) ) {
  199. foreach ( $config['xml_namespaces'] as $prefix => $uri) {
  200. $metadata->registerNamespace($uri, $prefix);
  201. }
  202. }
  203. if (isset($config['discriminator'])) {
  204. if (isset($config['discriminator']['disabled']) && true === $config['discriminator']['disabled']) {
  205. $metadata->discriminatorDisabled = true;
  206. } else {
  207. if ( ! isset($config['discriminator']['field_name'])) {
  208. throw new RuntimeException('The "field_name" attribute must be set for discriminators.');
  209. }
  210. if ( ! isset($config['discriminator']['map']) || ! is_array($config['discriminator']['map'])) {
  211. throw new RuntimeException('The "map" attribute must be set, and be an array for discriminators.');
  212. }
  213. $metadata->setDiscriminator($config['discriminator']['field_name'], $config['discriminator']['map']);
  214. }
  215. }
  216. }
  217. private function getCallbackMetadata(\ReflectionClass $class, $config)
  218. {
  219. if (is_string($config)) {
  220. $config = array($config);
  221. } elseif (!is_array($config)) {
  222. throw new RuntimeException(sprintf('callback methods expects a string, or an array of strings that represent method names, but got %s.', json_encode($config['pre_serialize'])));
  223. }
  224. $methods = array();
  225. foreach ($config as $name) {
  226. if (!$class->hasMethod($name)) {
  227. throw new RuntimeException(sprintf('The method %s does not exist in class %s.', $name, $class->name));
  228. }
  229. $methods[] = new MethodMetadata($class->name, $name);
  230. }
  231. return $methods;
  232. }
  233. }