Yaml.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Gedmo\Exception\InvalidMappingException,
  6. Gedmo\Uploadable\Mapping\Validator;
  7. /**
  8. * This is a yaml mapping driver for Uploadable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Uploadable
  11. * extension.
  12. *
  13. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Yaml extends File implements Driver
  18. {
  19. /**
  20. * File extension
  21. * @var string
  22. */
  23. protected $_extension = '.dcm.yml';
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function readExtendedMetadata($meta, array &$config)
  28. {
  29. $mapping = $this->_getMapping($meta->name);
  30. if (isset($mapping['gedmo'])) {
  31. $classMapping = $mapping['gedmo'];
  32. if (isset($classMapping['uploadable'])) {
  33. $uploadable = $classMapping['uploadable'];
  34. $config['uploadable'] = true;
  35. $config['allowOverwrite'] = isset($uploadable['allowOverwrite']) ?
  36. (bool) $uploadable['allowOverwrite'] : false;
  37. $config['appendNumber'] = isset($uploadable['appendNumber']) ?
  38. (bool) $uploadable['appendNumber'] : false;
  39. $config['path'] = isset($uploadable['path']) ? $uploadable['path'] : '';
  40. $config['pathMethod'] = isset($uploadable['pathMethod']) ? $uploadable['pathMethod'] : '';
  41. $config['callback'] = isset($uploadable['callback']) ? $uploadable['callback'] : '';
  42. $config['fileMimeTypeField'] = false;
  43. $config['filePathField'] = false;
  44. $config['fileSizeField'] = false;
  45. $config['filenameGenerator'] = isset($uploadable['filenameGenerator']) ?
  46. $uploadable['filenameGenerator'] :
  47. Validator::FILENAME_GENERATOR_NONE;
  48. $config['maxSize'] = isset($uploadable['maxSize']) ?
  49. (double) $uploadable['maxSize'] :
  50. (double) 0;
  51. $config['allowedTypes'] = isset($uploadable['allowedTypes']) ?
  52. $uploadable['allowedTypes'] :
  53. '';
  54. $config['disallowedTypes'] = isset($uploadable['disallowedTypes']) ?
  55. $uploadable['disallowedTypes'] :
  56. '';
  57. if (isset($mapping['fields'])) {
  58. foreach ($mapping['fields'] as $field => $info) {
  59. if (isset($info['gedmo']) && array_key_exists(0, $info['gedmo'])) {
  60. if ($info['gedmo'][0] === 'uploadableFileMimeType') {
  61. $config['fileMimeTypeField'] = $field;
  62. } else if ($info['gedmo'][0] === 'uploadableFileSize') {
  63. $config['fileSizeField'] = $field;
  64. } else if ($info['gedmo'][0] === 'uploadableFilePath') {
  65. $config['filePathField'] = $field;
  66. }
  67. }
  68. }
  69. }
  70. Validator::validateConfiguration($meta, $config);
  71. }
  72. }
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. protected function _loadMappingFile($file)
  78. {
  79. return \Symfony\Component\Yaml\Yaml::parse($file);
  80. }
  81. }