StringLength.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_StringLength extends Zend_Validate_Abstract
  32. {
  33. const TOO_SHORT = 'stringLengthTooShort';
  34. const TOO_LONG = 'stringLengthTooLong';
  35. /**
  36. * @var array
  37. */
  38. protected $_messageTemplates = array(
  39. self::TOO_SHORT => "'%value%' is less than %min% characters long",
  40. self::TOO_LONG => "'%value%' is greater than %max% characters long"
  41. );
  42. /**
  43. * @var array
  44. */
  45. protected $_messageVariables = array(
  46. 'min' => '_min',
  47. 'max' => '_max'
  48. );
  49. /**
  50. * Minimum length
  51. *
  52. * @var integer
  53. */
  54. protected $_min;
  55. /**
  56. * Maximum length
  57. *
  58. * If null, there is no maximum length
  59. *
  60. * @var integer|null
  61. */
  62. protected $_max;
  63. /**
  64. * Encoding to use
  65. *
  66. * @var string|null
  67. */
  68. protected $_encoding;
  69. /**
  70. * Sets validator options
  71. *
  72. * @param integer $min
  73. * @param integer $max
  74. * @return void
  75. */
  76. public function __construct($min = 0, $max = null, $encoding = null)
  77. {
  78. $this->setMin($min);
  79. $this->setMax($max);
  80. $this->setEncoding($encoding);
  81. }
  82. /**
  83. * Returns the min option
  84. *
  85. * @return integer
  86. */
  87. public function getMin()
  88. {
  89. return $this->_min;
  90. }
  91. /**
  92. * Sets the min option
  93. *
  94. * @param integer $min
  95. * @throws Zend_Validate_Exception
  96. * @return Zend_Validate_StringLength Provides a fluent interface
  97. */
  98. public function setMin($min)
  99. {
  100. if (null !== $this->_max && $min > $this->_max) {
  101. /**
  102. * @see Zend_Validate_Exception
  103. */
  104. require_once 'Zend/Validate/Exception.php';
  105. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
  106. . " $this->_max");
  107. }
  108. $this->_min = max(0, (integer) $min);
  109. return $this;
  110. }
  111. /**
  112. * Returns the max option
  113. *
  114. * @return integer|null
  115. */
  116. public function getMax()
  117. {
  118. return $this->_max;
  119. }
  120. /**
  121. * Sets the max option
  122. *
  123. * @param integer|null $max
  124. * @throws Zend_Validate_Exception
  125. * @return Zend_Validate_StringLength Provides a fluent interface
  126. */
  127. public function setMax($max)
  128. {
  129. if (null === $max) {
  130. $this->_max = null;
  131. } else if ($max < $this->_min) {
  132. /**
  133. * @see Zend_Validate_Exception
  134. */
  135. require_once 'Zend/Validate/Exception.php';
  136. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
  137. . "$max < $this->_min");
  138. } else {
  139. $this->_max = (integer) $max;
  140. }
  141. return $this;
  142. }
  143. /**
  144. * Returns the actual encoding
  145. *
  146. * @return string
  147. */
  148. public function getEncoding()
  149. {
  150. return $this->_encoding;
  151. }
  152. /**
  153. * Sets a new encoding to use
  154. *
  155. * @param string $encoding
  156. * @return Zend_Validate_StringLength
  157. */
  158. public function setEncoding($encoding = null)
  159. {
  160. if ($encoding !== null) {
  161. $orig = iconv_get_encoding('internal_encoding');
  162. $result = iconv_set_encoding('internal_encoding', $encoding);
  163. if (!$result) {
  164. require_once 'Zend/Validate/Exception.php';
  165. throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
  166. }
  167. iconv_set_encoding('internal_encoding', $orig);
  168. }
  169. $this->_encoding = $encoding;
  170. return $this;
  171. }
  172. /**
  173. * Defined by Zend_Validate_Interface
  174. *
  175. * Returns true if and only if the string length of $value is at least the min option and
  176. * no greater than the max option (when the max option is not null).
  177. *
  178. * @param string $value
  179. * @return boolean
  180. */
  181. public function isValid($value)
  182. {
  183. $valueString = (string) $value;
  184. $this->_setValue($valueString);
  185. if ($this->_encoding !== null) {
  186. $length = iconv_strlen($valueString, $this->_encoding);
  187. } else {
  188. $length = iconv_strlen($valueString);
  189. }
  190. if ($length < $this->_min) {
  191. $this->_error(self::TOO_SHORT);
  192. }
  193. if (null !== $this->_max && $this->_max < $length) {
  194. $this->_error(self::TOO_LONG);
  195. }
  196. if (count($this->_messages)) {
  197. return false;
  198. } else {
  199. return true;
  200. }
  201. }
  202. }