upload_of.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php namespace System\Validation\Rules;
  2. use System\Input;
  3. use System\Validation\Rule;
  4. class Upload_Of extends Rule {
  5. /**
  6. * The acceptable file extensions.
  7. *
  8. * @var array
  9. */
  10. public $extensions;
  11. /**
  12. * The maximum file size in bytes.
  13. *
  14. * @var int
  15. */
  16. public $maximum;
  17. /**
  18. * Evaluate the validity of an attribute.
  19. *
  20. * @param string $attribute
  21. * @param array $attributes
  22. * @return void
  23. */
  24. public function check($attribute, $attributes)
  25. {
  26. if ( ! array_key_exists($attribute, Input::file()))
  27. {
  28. return true;
  29. }
  30. $file = Input::file($attribute);
  31. if ( ! is_null($this->maximum) and $file['size'] > $this->maximum)
  32. {
  33. return false;
  34. }
  35. if ( ! is_null($this->extensions) and ! in_array(File::extension($file['name']), $this->extensions))
  36. {
  37. return false;
  38. }
  39. return true;
  40. }
  41. /**
  42. * Set the acceptable file extensions.
  43. *
  44. * @return Upload_Of
  45. */
  46. public function has_extension()
  47. {
  48. $this->extensions = func_get_args();
  49. return $this;
  50. }
  51. /**
  52. * Set the maximum file size in bytes.
  53. *
  54. * @param int $maximum
  55. * @return Upload_Of
  56. */
  57. public function less_than($maximum)
  58. {
  59. $this->maximum = $maximum;
  60. return $this;
  61. }
  62. }