2
0

StringLength.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-2009 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-2009 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 INVALID = 'stringLengthInvalid';
  34. const TOO_SHORT = 'stringLengthTooShort';
  35. const TOO_LONG = 'stringLengthTooLong';
  36. /**
  37. * @var array
  38. */
  39. protected $_messageTemplates = array(
  40. self::INVALID => "Invalid type given, value should be a string",
  41. self::TOO_SHORT => "'%value%' is less than %min% characters long",
  42. self::TOO_LONG => "'%value%' is greater than %max% characters long"
  43. );
  44. /**
  45. * @var array
  46. */
  47. protected $_messageVariables = array(
  48. 'min' => '_min',
  49. 'max' => '_max'
  50. );
  51. /**
  52. * Minimum length
  53. *
  54. * @var integer
  55. */
  56. protected $_min;
  57. /**
  58. * Maximum length
  59. *
  60. * If null, there is no maximum length
  61. *
  62. * @var integer|null
  63. */
  64. protected $_max;
  65. /**
  66. * Encoding to use
  67. *
  68. * @var string|null
  69. */
  70. protected $_encoding;
  71. /**
  72. * Sets validator options
  73. *
  74. * @param integer|array|Zend_Config $options
  75. * @return void
  76. */
  77. public function __construct($options = array())
  78. {
  79. if ($options instanceof Zend_Config) {
  80. $options = $options->toArray();
  81. } else if (!is_array($options)) {
  82. $count = func_num_args();
  83. if ($count > 1) {
  84. // @todo: Preperation for 2.0... needs to be cleared with the dev-team
  85. // trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
  86. }
  87. $options = func_get_args();
  88. $temp['min'] = array_shift($options);
  89. if (!empty($options)) {
  90. $temp['max'] = array_shift($options);
  91. }
  92. if (!empty($options)) {
  93. $temp['encoding'] = array_shift($options);
  94. }
  95. $options = $temp;
  96. }
  97. if (!array_key_exists('min', $options)) {
  98. $options['min'] = 0;
  99. }
  100. $this->setMin($options['min']);
  101. if (array_key_exists('max', $options)) {
  102. $this->setMax($options['max']);
  103. }
  104. if (array_key_exists('encoding', $options)) {
  105. $this->setEncoding($options['encoding']);
  106. }
  107. }
  108. /**
  109. * Returns the min option
  110. *
  111. * @return integer
  112. */
  113. public function getMin()
  114. {
  115. return $this->_min;
  116. }
  117. /**
  118. * Sets the min option
  119. *
  120. * @param integer $min
  121. * @throws Zend_Validate_Exception
  122. * @return Zend_Validate_StringLength Provides a fluent interface
  123. */
  124. public function setMin($min)
  125. {
  126. if (null !== $this->_max && $min > $this->_max) {
  127. /**
  128. * @see Zend_Validate_Exception
  129. */
  130. require_once 'Zend/Validate/Exception.php';
  131. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
  132. . " $this->_max");
  133. }
  134. $this->_min = max(0, (integer) $min);
  135. return $this;
  136. }
  137. /**
  138. * Returns the max option
  139. *
  140. * @return integer|null
  141. */
  142. public function getMax()
  143. {
  144. return $this->_max;
  145. }
  146. /**
  147. * Sets the max option
  148. *
  149. * @param integer|null $max
  150. * @throws Zend_Validate_Exception
  151. * @return Zend_Validate_StringLength Provides a fluent interface
  152. */
  153. public function setMax($max)
  154. {
  155. if (null === $max) {
  156. $this->_max = null;
  157. } else if ($max < $this->_min) {
  158. /**
  159. * @see Zend_Validate_Exception
  160. */
  161. require_once 'Zend/Validate/Exception.php';
  162. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
  163. . "$max < $this->_min");
  164. } else {
  165. $this->_max = (integer) $max;
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Returns the actual encoding
  171. *
  172. * @return string
  173. */
  174. public function getEncoding()
  175. {
  176. return $this->_encoding;
  177. }
  178. /**
  179. * Sets a new encoding to use
  180. *
  181. * @param string $encoding
  182. * @return Zend_Validate_StringLength
  183. */
  184. public function setEncoding($encoding = null)
  185. {
  186. if ($encoding !== null) {
  187. $orig = iconv_get_encoding('internal_encoding');
  188. $result = iconv_set_encoding('internal_encoding', $encoding);
  189. if (!$result) {
  190. require_once 'Zend/Validate/Exception.php';
  191. throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
  192. }
  193. iconv_set_encoding('internal_encoding', $orig);
  194. }
  195. $this->_encoding = $encoding;
  196. return $this;
  197. }
  198. /**
  199. * Defined by Zend_Validate_Interface
  200. *
  201. * Returns true if and only if the string length of $value is at least the min option and
  202. * no greater than the max option (when the max option is not null).
  203. *
  204. * @param string $value
  205. * @return boolean
  206. */
  207. public function isValid($value)
  208. {
  209. if (!is_string($value)) {
  210. $this->_error(self::INVALID);
  211. return false;
  212. }
  213. $this->_setValue($value);
  214. if ($this->_encoding !== null) {
  215. $length = iconv_strlen($value, $this->_encoding);
  216. } else {
  217. $length = iconv_strlen($value);
  218. }
  219. if ($length < $this->_min) {
  220. $this->_error(self::TOO_SHORT);
  221. }
  222. if (null !== $this->_max && $this->_max < $length) {
  223. $this->_error(self::TOO_LONG);
  224. }
  225. if (count($this->_messages)) {
  226. return false;
  227. } else {
  228. return true;
  229. }
  230. }
  231. }