Validator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  23. * List of types which are valid for UploadableFileMimeType field
  24. *
  25. * @var array
  26. */
  27. public static $validFileMimeTypeTypes = array(
  28. 'string'
  29. );
  30. /**
  31. * List of types which are valid for UploadableFilePath field
  32. *
  33. * @var array
  34. */
  35. public static $validFilePathTypes = array(
  36. 'string'
  37. );
  38. /**
  39. * List of types which are valid for UploadableFileSize field
  40. *
  41. * @var array
  42. */
  43. public static $validFileSizeTypes = array(
  44. 'decimal'
  45. );
  46. public static function validateFileMimeTypeField(ClassMetadataInfo $meta, $field)
  47. {
  48. self::validateField($meta, $field, self::UPLOADABLE_FILE_MIME_TYPE, self::$validFileMimeTypeTypes);
  49. }
  50. public static function validateFilePathField(ClassMetadataInfo $meta, $field)
  51. {
  52. self::validateField($meta, $field, self::UPLOADABLE_FILE_PATH, self::$validFilePathTypes);
  53. }
  54. public static function validateFileSizeField(ClassMetadataInfo $meta, $field)
  55. {
  56. self::validateField($meta, $field, self::UPLOADABLE_FILE_SIZE, self::$validFileSizeTypes);
  57. }
  58. public static function validateField($meta, $field, $uploadableField, $validFieldTypes)
  59. {
  60. $fieldMapping = $meta->getFieldMapping($field);
  61. if (!in_array($fieldMapping['type'], $validFieldTypes)) {
  62. $msg = 'Field "%s" to work as an "%s" field must be of one of the following types: "%s".';
  63. throw new InvalidMappingException(sprintf($msg,
  64. $field,
  65. $uploadableField,
  66. explode(', ', $validFieldTypes)
  67. ));
  68. }
  69. }
  70. public static function validatePath($path)
  71. {
  72. if (!is_dir($path) || !is_writable($path)) {
  73. throw new UploadableCantWriteException(sprintf('Directory "%s" does not exist or is not writable',
  74. $path
  75. ));
  76. }
  77. }
  78. public static function validateConfiguration(ClassMetadata $meta, array $config)
  79. {
  80. if (!$config['filePathField']) {
  81. throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFilePath field.',
  82. $meta->name
  83. ));
  84. }
  85. if (!$config['fileInfoField']) {
  86. throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFileInfo field.',
  87. $meta->name
  88. ));
  89. }
  90. if ($config['path'] === '' && $config['pathMethod'] === '') {
  91. $msg = 'You need to define the path in the %s annotation, or add a method with %s annotation.';
  92. throw new InvalidMappingException(sprintf($msg,
  93. self::UPLOADABLE,
  94. self::UPLOADABLE_PATH
  95. ));
  96. } else if ($config['path'] !== '') {
  97. Validator::validatePath($config['path']);
  98. }
  99. }
  100. }