StringLength.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ Kd1ungvZB4yzI/+VveWY0SUHxDR1K0FyWjR+CEYITIs=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.01]
  11. */
  12. /**
  13. * checks if the variable is greater than a set value (with option to check if greater or equal)
  14. */
  15. namespace Cube\Validate;
  16. class StringLength extends AbstractValidate
  17. {
  18. const NO_STRING = 1;
  19. const TOO_SHORT = 2;
  20. const TOO_LONG = 3;
  21. protected $_messages = array(
  22. self::NO_STRING => "'%s' expects a string, invalid type given.",
  23. self::TOO_SHORT => "'%s' must contain at least %value% characters.",
  24. self::TOO_LONG => "'%s' must contain no more than %value% characters.",
  25. );
  26. /**
  27. *
  28. * minimum characters allowed
  29. *
  30. * @var int
  31. */
  32. private $_min;
  33. /**
  34. *
  35. * maximum characters allowed
  36. *
  37. * @var int
  38. */
  39. private $_max;
  40. /**
  41. *
  42. * class constructor
  43. *
  44. * initialize the minimum and maximum values allowed
  45. *
  46. * @param array $data data[0] -> min value;
  47. * data[1] -> max value;
  48. */
  49. public function __construct(array $data = null)
  50. {
  51. $this->setMin($data[0])
  52. ->setMax($data[1]);
  53. }
  54. /**
  55. *
  56. * get the minimum characters allowed
  57. *
  58. * @return int
  59. */
  60. public function getMin()
  61. {
  62. return $this->_min;
  63. }
  64. /**
  65. *
  66. * set minimum characters allowed
  67. *
  68. * @param int $min
  69. *
  70. * @return $this
  71. */
  72. public function setMin($min)
  73. {
  74. $this->_min = (integer)$min;
  75. return $this;
  76. }
  77. /**
  78. *
  79. * get max characters allowed
  80. *
  81. * @return int
  82. */
  83. public function getMax()
  84. {
  85. return $this->_max;
  86. }
  87. /**
  88. *
  89. * set max number of characters allowed
  90. *
  91. * @param int $max
  92. *
  93. * @return $this
  94. */
  95. public function setMax($max)
  96. {
  97. $this->_max = (integer)$max;
  98. return $this;
  99. }
  100. /**
  101. *
  102. * checks if the string length is within the allowed values
  103. * if the value is empty it will return true as the NotEmpty class should check that.
  104. *
  105. * @return bool return true if the validation is successful
  106. */
  107. public function isValid()
  108. {
  109. $value = $this->getValue();
  110. if (empty($value)) {
  111. return true;
  112. }
  113. $min = $this->getMin();
  114. $max = $this->getMax();
  115. if (!is_string($value)) {
  116. $this->setMessage($this->_messages[self::NO_STRING]);
  117. return false;
  118. }
  119. else if (strlen($value) < $min) {
  120. $this->setMessage($this->_messages[self::TOO_SHORT]);
  121. $this->setMessage(
  122. str_replace('%value%', $min, $this->getMessage()));
  123. return false;
  124. }
  125. else if (strlen($value) > $max &&
  126. $max > $min
  127. ) {
  128. $this->setMessage($this->_messages[self::TOO_LONG]);
  129. $this->setMessage(
  130. str_replace('%value%', $max, $this->getMessage()));
  131. return false;
  132. }
  133. return true;
  134. }
  135. }