Annotation.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  4. use Gedmo\Uploadable\Mapping\Validator;
  5. /**
  6. * This is an annotation mapping driver for Uploadable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from Annotations specifically for Uploadable
  9. * extension.
  10. *
  11. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Annotation extends AbstractAnnotationDriver
  16. {
  17. /**
  18. * Annotation to define that this object is loggable
  19. */
  20. const UPLOADABLE = 'Gedmo\\Mapping\\Annotation\\Uploadable';
  21. const UPLOADABLE_FILE_MIME_TYPE = 'Gedmo\\Mapping\\Annotation\\UploadableFileMimeType';
  22. const UPLOADABLE_FILE_NAME = 'Gedmo\\Mapping\\Annotation\\UploadableFileName';
  23. const UPLOADABLE_FILE_PATH = 'Gedmo\\Mapping\\Annotation\\UploadableFilePath';
  24. const UPLOADABLE_FILE_SIZE = 'Gedmo\\Mapping\\Annotation\\UploadableFileSize';
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function readExtendedMetadata($meta, array &$config)
  29. {
  30. $class = $this->getMetaReflectionClass($meta);
  31. // class annotations
  32. if ($annot = $this->reader->getClassAnnotation($class, self::UPLOADABLE)) {
  33. $config['uploadable'] = true;
  34. $config['allowOverwrite'] = $annot->allowOverwrite;
  35. $config['appendNumber'] = $annot->appendNumber;
  36. $config['path'] = $annot->path;
  37. $config['pathMethod'] = $annot->pathMethod;
  38. $config['fileMimeTypeField'] = false;
  39. $config['fileNameField'] = false;
  40. $config['filePathField'] = false;
  41. $config['fileSizeField'] = false;
  42. $config['callback'] = $annot->callback;
  43. $config['filenameGenerator'] = $annot->filenameGenerator;
  44. $config['maxSize'] = (double) $annot->maxSize;
  45. $config['allowedTypes'] = $annot->allowedTypes;
  46. $config['disallowedTypes'] = $annot->disallowedTypes;
  47. foreach ($class->getProperties() as $prop) {
  48. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
  49. $config['fileMimeTypeField'] = $prop->getName();
  50. }
  51. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_NAME)) {
  52. $config['fileNameField'] = $prop->getName();
  53. }
  54. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
  55. $config['filePathField'] = $prop->getName();
  56. }
  57. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
  58. $config['fileSizeField'] = $prop->getName();
  59. }
  60. }
  61. Validator::validateConfiguration($meta, $config);
  62. }
  63. /*
  64. // Code in case we need to identify entities which are not Uploadables, but have associations
  65. // with other Uploadable entities
  66. } else {
  67. // We need to check if this class has a relation with Uploadable entities
  68. $associations = $meta->getAssociationMappings();
  69. foreach ($associations as $field => $association) {
  70. $refl = new \ReflectionClass($association['targetEntity']);
  71. if ($annot = $this->reader->getClassAnnotation($refl, self::UPLOADABLE)) {
  72. $config['hasUploadables'] = true;
  73. if (!isset($config['uploadables'])) {
  74. $config['uploadables'] = array();
  75. }
  76. $config['uploadables'][] = array(
  77. 'class' => $association['targetEntity'],
  78. 'property' => $association['fieldName']
  79. );
  80. }
  81. }
  82. }*/
  83. $this->validateFullMetadata($meta, $config);
  84. }
  85. }