Validator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $refl = $meta->getReflectionClass();
  81. if (!$config['filePathField']) {
  82. throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFilePath field.',
  83. $meta->name
  84. ));
  85. }
  86. if (!$config['fileInfoProperty']) {
  87. throw new InvalidMappingException(sprintf('Class "%s" must define a "fileInfoProperty".',
  88. $meta->name
  89. ));
  90. } else {
  91. if (!$refl->hasProperty($config['fileInfoProperty'])) {
  92. throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have property "%s"!',
  93. $meta->name,
  94. $config['fileInfoProperty']
  95. ));
  96. }
  97. }
  98. if ($config['path'] === '' && $config['pathMethod'] === '') {
  99. $msg = 'You need to define the path in the %s annotation, or add a method with %s annotation.';
  100. throw new InvalidMappingException(sprintf($msg,
  101. self::UPLOADABLE,
  102. self::UPLOADABLE_PATH
  103. ));
  104. } else if ($config['pathMethod'] !== '') {
  105. if (!$refl->hasMethod($config['pathMethod'])) {
  106. throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
  107. $meta->name,
  108. $config['pathMethod']
  109. ));
  110. }
  111. }
  112. if ($config['fileMimeTypeField']) {
  113. self::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
  114. }
  115. if ($config['fileSizeField']) {
  116. self::validateFileSizeField($meta, $config['fileSizeField']);
  117. }
  118. self::validateFilePathField($meta, $config['filePathField']);
  119. }
  120. }