size_of.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php namespace System\Validation\Rules;
  2. use System\Str;
  3. use System\Validation\Rule;
  4. class Size_Of extends Rule {
  5. /**
  6. * The exact size the attribute must be.
  7. *
  8. * @var int
  9. */
  10. public $length;
  11. /**
  12. * The maximum size of the attribute.
  13. *
  14. * @var int
  15. */
  16. public $maximum;
  17. /**
  18. * The minimum size of the attribute.
  19. *
  20. * @var int
  21. */
  22. public $minimum;
  23. /**
  24. * Evaluate the validity of an attribute.
  25. *
  26. * @param string $attribute
  27. * @param array $attributes
  28. * @return void
  29. */
  30. public function check($attribute, $attributes)
  31. {
  32. if ( ! array_key_exists($attribute, $attributes))
  33. {
  34. return true;
  35. }
  36. if (is_numeric($attributes[$attribute]))
  37. {
  38. return $this->check_number($attribute, $attributes);
  39. }
  40. else
  41. {
  42. return $this->check_string($attribute, $attributes);
  43. }
  44. }
  45. /**
  46. * Evaluate the validity of a numeric attribute.
  47. *
  48. * @param string $attribute
  49. * @param array $attributes
  50. * @return void
  51. */
  52. private function check_number($attribute, $attributes)
  53. {
  54. if ( ! is_null($this->length) and $attributes[$attribute] !== $this->length)
  55. {
  56. $this->error = 'number_wrong_size';
  57. return false;
  58. }
  59. if ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)
  60. {
  61. $this->error = 'number_too_big';
  62. return false;
  63. }
  64. if ( ! is_null($this->minimum and $attributes[$attribute] < $this->minimum))
  65. {
  66. $this->error = 'number_too_small';
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * Evaluate the validity of a string attribute.
  73. *
  74. * @param string $attribute
  75. * @param array $attributes
  76. * @return void
  77. */
  78. public function check_string($attribute, $attributes)
  79. {
  80. $value = trim((string) $attributes[$attribute]);
  81. if ( ! is_null($this->length) and Str::length($value) !== $this->length)
  82. {
  83. $this->error = 'string_wrong_size';
  84. return false;
  85. }
  86. if ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)
  87. {
  88. $this->error = 'string_too_big';
  89. return false;
  90. }
  91. if ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)
  92. {
  93. $this->error = 'string_too_small';
  94. return false;
  95. }
  96. return true;
  97. }
  98. /**
  99. * Set the exact size the attribute must be.
  100. *
  101. * @param int $length
  102. * @return Size_Of
  103. */
  104. public function is($length)
  105. {
  106. $this->length = $length;
  107. return $this;
  108. }
  109. /**
  110. * Set the minimum and maximum size of the attribute.
  111. *
  112. * @param int $minimum
  113. * @param int $maximum
  114. * @return Size_Of
  115. */
  116. public function between($minimum, $maximum)
  117. {
  118. $this->minimum = $minimum;
  119. $this->maximum = $maximum;
  120. return $this;
  121. }
  122. /**
  123. * Set the minimum size the attribute.
  124. *
  125. * @param int $minimum
  126. * @return Size_Of
  127. */
  128. public function at_least($minimum)
  129. {
  130. $this->minimum = $minimum;
  131. return $this;
  132. }
  133. /**
  134. * Set the maximum size the attribute.
  135. *
  136. * @param int $maximum
  137. * @return Size_Of
  138. */
  139. public function less_than($maximum)
  140. {
  141. $this->maximum = $maximum;
  142. return $this;
  143. }
  144. }