Validator.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping;
  3. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  4. use Gedmo\Exception\InvalidMappingException;
  5. use Gedmo\Exception\UploadableCantWriteException;
  6. use Doctrine\ORM\Mapping\ClassMetadata;
  7. /**
  8. * This class is used to validate mapping information
  9. *
  10. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo.Uploadable.Mapping
  13. * @subpackage Validator
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Validator
  18. {
  19. const UPLOADABLE_FILE_MIME_TYPE = 'UploadableFileMimeType';
  20. const UPLOADABLE_FILE_PATH = 'UploadableFilePath';
  21. const UPLOADABLE_FILE_SIZE = 'UploadableFileSize';
  22. const FILENAME_GENERATOR_SHA1 = 'SHA1';
  23. const FILENAME_GENERATOR_ALPHANUMERIC = 'ALPHANUMERIC';
  24. const FILENAME_GENERATOR_NONE = 'NONE';
  25. /**
  26. * List of types which are valid for UploadableFileMimeType field
  27. *
  28. * @var array
  29. */
  30. public static $validFileMimeTypeTypes = array(
  31. 'string'
  32. );
  33. /**
  34. * List of types which are valid for UploadableFilePath field
  35. *
  36. * @var array
  37. */
  38. public static $validFilePathTypes = array(
  39. 'string'
  40. );
  41. /**
  42. * List of types which are valid for UploadableFileSize field
  43. *
  44. * @var array
  45. */
  46. public static $validFileSizeTypes = array(
  47. 'decimal'
  48. );
  49. public static function validateFileMimeTypeField(ClassMetadataInfo $meta, $field)
  50. {
  51. self::validateField($meta, $field, self::UPLOADABLE_FILE_MIME_TYPE, self::$validFileMimeTypeTypes);
  52. }
  53. public static function validateFilePathField(ClassMetadataInfo $meta, $field)
  54. {
  55. self::validateField($meta, $field, self::UPLOADABLE_FILE_PATH, self::$validFilePathTypes);
  56. }
  57. public static function validateFileSizeField(ClassMetadataInfo $meta, $field)
  58. {
  59. self::validateField($meta, $field, self::UPLOADABLE_FILE_SIZE, self::$validFileSizeTypes);
  60. }
  61. public static function validateField($meta, $field, $uploadableField, $validFieldTypes)
  62. {
  63. $fieldMapping = $meta->getFieldMapping($field);
  64. if (!in_array($fieldMapping['type'], $validFieldTypes)) {
  65. $msg = 'Field "%s" to work as an "%s" field must be of one of the following types: "%s".';
  66. throw new InvalidMappingException(sprintf($msg,
  67. $field,
  68. $uploadableField,
  69. explode(', ', $validFieldTypes)
  70. ));
  71. }
  72. }
  73. public static function validatePath($path)
  74. {
  75. if (!is_dir($path) || !is_writable($path)) {
  76. throw new UploadableCantWriteException(sprintf('Directory "%s" does not exist or is not writable',
  77. $path
  78. ));
  79. }
  80. }
  81. public static function validateConfiguration(ClassMetadata $meta, array $config)
  82. {
  83. $refl = $meta->getReflectionClass();
  84. if (!$config['filePathField']) {
  85. throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFilePath field.',
  86. $meta->name
  87. ));
  88. }
  89. if ($config['pathMethod'] !== '') {
  90. if (!$refl->hasMethod($config['pathMethod'])) {
  91. throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
  92. $meta->name,
  93. $config['pathMethod']
  94. ));
  95. }
  96. }
  97. if ($config['callback'] !== '') {
  98. if (!$refl->hasMethod($config['callback'])) {
  99. throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
  100. $meta->name,
  101. $config['callback']
  102. ));
  103. }
  104. }
  105. if ($config['fileMimeTypeField']) {
  106. self::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
  107. }
  108. if ($config['fileSizeField']) {
  109. self::validateFileSizeField($meta, $config['fileSizeField']);
  110. }
  111. switch ((string) $config['filenameGenerator']) {
  112. case self::FILENAME_GENERATOR_ALPHANUMERIC:
  113. case self::FILENAME_GENERATOR_SHA1:
  114. case self::FILENAME_GENERATOR_NONE:
  115. break;
  116. default:
  117. $ok = false;
  118. if (is_class($config['filenameGenerator'])) {
  119. $refl = new \ReflectionClass($config['filenameGenerator']);
  120. if ($refl->implementsInterface('Gedmo\Uploadable\FilenameGeneratorInterface')) {
  121. $ok = true;
  122. }
  123. }
  124. if (!$ok) {
  125. $msg = 'Class "%s" needs a valid value for filenameGenerator. It can be: SHA1, ALPHANUMERIC, NONE or ';
  126. $msg .= 'a class implementing FileGeneratorInterface.';
  127. throw new InvalidMappingException(sprintf($msg,
  128. $meta->name
  129. ));
  130. }
  131. }
  132. self::validateFilePathField($meta, $config['filePathField']);
  133. }
  134. }