numericality_of.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php namespace System\Validation\Rules;
  2. use System\Validation\Rangable_Rule;
  3. class Numericality_Of extends Rangable_Rule {
  4. /**
  5. * Indicates that the attribute must be an integer.
  6. *
  7. * @var bool
  8. */
  9. public $only_integer = false;
  10. /**
  11. * The "not valid" error message.
  12. *
  13. * @var string
  14. */
  15. public $not_valid;
  16. /**
  17. * The "not integer" error message.
  18. *
  19. * @var string
  20. */
  21. public $not_integer;
  22. /**
  23. * Evaluate the validity of an attribute.
  24. *
  25. * @param string $attribute
  26. * @param array $attributes
  27. * @return bool
  28. */
  29. public function check($attribute, $attributes)
  30. {
  31. if ( ! is_null($nullable = parent::check($attribute, $attributes)))
  32. {
  33. return $nullable;
  34. }
  35. // ---------------------------------------------------------
  36. // Validate the attribute is a number.
  37. // ---------------------------------------------------------
  38. if ( ! is_numeric($attributes[$attribute]))
  39. {
  40. $this->error = 'number_not_valid';
  41. }
  42. // ---------------------------------------------------------
  43. // Validate the attribute is an integer.
  44. // ---------------------------------------------------------
  45. elseif ($this->only_integer and filter_var($attributes[$attribute], FILTER_VALIDATE_INT) === false)
  46. {
  47. $this->error = 'number_not_integer';
  48. }
  49. // ---------------------------------------------------------
  50. // Validate the exact size of the attribute.
  51. // ---------------------------------------------------------
  52. elseif ( ! is_null($this->size) and $attributes[$attribute] != $this->size)
  53. {
  54. $this->error = 'number_wrong_size';
  55. }
  56. // ---------------------------------------------------------
  57. // Validate the maximum size of the attribute.
  58. // ---------------------------------------------------------
  59. elseif ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
  60. {
  61. $this->error = 'number_too_big';
  62. }
  63. // ---------------------------------------------------------
  64. // Validate the minimum size of the attribute.
  65. // ---------------------------------------------------------
  66. elseif ( ! is_null($this->minimum) and $attributes[$attribute] < $this->minimum)
  67. {
  68. $this->error = 'number_too_small';
  69. }
  70. return is_null($this->error);
  71. }
  72. /**
  73. * Specify that the attribute must be an integer.
  74. *
  75. * @return Numericality_Of
  76. */
  77. public function only_integer()
  78. {
  79. $this->only_integer = true;
  80. return $this;
  81. }
  82. /**
  83. * Set the "not valid" error message.
  84. *
  85. * @param string $message
  86. * @return Numericality_Of
  87. */
  88. public function not_valid($message)
  89. {
  90. $this->not_valid = $message;
  91. return $this;
  92. }
  93. /**
  94. * Set the "not integer" error message.
  95. *
  96. * @param string $message
  97. * @return Numericality_Of
  98. */
  99. public function not_integer($message)
  100. {
  101. $this->not_integer = $message;
  102. return $this;
  103. }
  104. }