YamlDriver.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace JMS\SerializerBundle\Metadata\Driver;
  3. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  4. use Metadata\MethodMetadata;
  5. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  6. use JMS\SerializerBundle\Metadata\ClassMetadata;
  7. use Symfony\Component\Yaml\Yaml;
  8. use Metadata\Driver\AbstractFileDriver;
  9. class YamlDriver extends AbstractFileDriver
  10. {
  11. protected function loadMetadataFromFile(\ReflectionClass $class, $file)
  12. {
  13. $config = Yaml::parse(file_get_contents($file));
  14. if (!isset($config[$name = $class->getName()])) {
  15. throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->getName(), $file));
  16. }
  17. $config = $config[$name];
  18. $metadata = new ClassMetadata($name);
  19. $metadata->fileResources[] = $file;
  20. $metadata->fileResources[] = $class->getFileName();
  21. $exclusionPolicy = isset($config['exclusion_policy']) ? $config['exclusion_policy'] : 'NONE';
  22. $excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false;
  23. if (isset($config['xml_root_name'])) {
  24. $metadata->xmlRootName = (string) $config['xml_root_name'];
  25. }
  26. if (!$excludeAll) {
  27. foreach ($class->getProperties() as $property) {
  28. if ($name !== $property->getDeclaringClass()->getName()) {
  29. continue;
  30. }
  31. $pMetadata = new PropertyMetadata($name, $pName = $property->getName());
  32. $isExclude = $isExpose = false;
  33. if (isset($config['properties'][$pName])) {
  34. $pConfig = $config['properties'][$pName];
  35. if (isset($pConfig['exclude'])) {
  36. $isExclude = (Boolean) $pConfig['exclude'];
  37. }
  38. if (isset($pConfig['expose'])) {
  39. $isExpose = (Boolean) $pConfig['expose'];
  40. }
  41. if (isset($pConfig['since_version'])) {
  42. $pMetadata->sinceVersion = (string) $pConfig['since_version'];
  43. }
  44. if (isset($pConfig['until_version'])) {
  45. $pMetadata->untilVersion = (string) $pConfig['until_version'];
  46. }
  47. if (isset($pConfig['serialized_name'])) {
  48. $pMetadata->serializedName = (string) $pConfig['serialized_name'];
  49. }
  50. if (isset($pConfig['type'])) {
  51. $pMetadata->type = (string) $pConfig['type'];
  52. }
  53. if (isset($pConfig['xml_list'])) {
  54. $pMetadata->xmlCollection = true;
  55. $colConfig = $pConfig['xml_list'];
  56. if (isset($colConfig['inline'])) {
  57. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  58. }
  59. if (isset($colConfig['entry_name'])) {
  60. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  61. }
  62. }
  63. if (isset($pConfig['xml_map'])) {
  64. $pMetadata->xmlCollection = true;
  65. $colConfig = $pConfig['xml_map'];
  66. if (isset($colConfig['inline'])) {
  67. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  68. }
  69. if (isset($colConfig['entry_name'])) {
  70. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  71. }
  72. if (isset($colConfig['key_attribute_name'])) {
  73. $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
  74. }
  75. }
  76. if (isset($pConfig['xml_attribute'])) {
  77. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
  78. }
  79. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  80. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  81. $metadata->addPropertyMetadata($pMetadata);
  82. }
  83. }
  84. }
  85. }
  86. if (isset($config['callback_methods'])) {
  87. $cConfig = $config['callback_methods'];
  88. if (isset($cConfig['pre_serialize'])) {
  89. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  90. }
  91. if (isset($cConfig['post_serialize'])) {
  92. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  93. }
  94. if (isset($cConfig['post_deserialize'])) {
  95. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  96. }
  97. }
  98. return $metadata;
  99. }
  100. protected function getExtension()
  101. {
  102. return 'yml';
  103. }
  104. private function getCallbackMetadata(\ReflectionClass $class, $config)
  105. {
  106. if (is_string($config)) {
  107. $config = array($config);
  108. } else if (!is_array($config)) {
  109. throw new \RuntimeException(sprintf('callback methods expects a string, or an array of strings that represent method names, but got %s.', json_encode($cConfig['pre_serialize'])));
  110. }
  111. $methods = array();
  112. foreach ($config as $name) {
  113. if (!$class->hasMethod($name)) {
  114. throw new \RuntimeException(sprintf('The method %s does not exist in class %s.', $mName, $name));
  115. }
  116. $methods[] = new MethodMetadata($class->getName(), $name);
  117. }
  118. return $methods;
  119. }
  120. }