StringLength.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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-2015 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-2015 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. String expected",
  41. self::TOO_SHORT => "'%value%' is less than %min% characters long",
  42. self::TOO_LONG => "'%value%' is more 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. */
  76. public function __construct($options = array())
  77. {
  78. if ($options instanceof Zend_Config) {
  79. $options = $options->toArray();
  80. } else if (!is_array($options)) {
  81. $options = func_get_args();
  82. $temp['min'] = array_shift($options);
  83. if (!empty($options)) {
  84. $temp['max'] = array_shift($options);
  85. }
  86. if (!empty($options)) {
  87. $temp['encoding'] = array_shift($options);
  88. }
  89. $options = $temp;
  90. }
  91. if (!array_key_exists('min', $options)) {
  92. $options['min'] = 0;
  93. }
  94. $this->setMin($options['min']);
  95. if (array_key_exists('max', $options)) {
  96. $this->setMax($options['max']);
  97. }
  98. if (array_key_exists('encoding', $options)) {
  99. $this->setEncoding($options['encoding']);
  100. }
  101. }
  102. /**
  103. * Returns the min option
  104. *
  105. * @return integer
  106. */
  107. public function getMin()
  108. {
  109. return $this->_min;
  110. }
  111. /**
  112. * Sets the min option
  113. *
  114. * @param integer $min
  115. * @throws Zend_Validate_Exception
  116. * @return Zend_Validate_StringLength Provides a fluent interface
  117. */
  118. public function setMin($min)
  119. {
  120. if (null !== $this->_max && $min > $this->_max) {
  121. /**
  122. * @see Zend_Validate_Exception
  123. */
  124. #require_once 'Zend/Validate/Exception.php';
  125. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
  126. . " $this->_max");
  127. }
  128. $this->_min = max(0, (integer) $min);
  129. return $this;
  130. }
  131. /**
  132. * Returns the max option
  133. *
  134. * @return integer|null
  135. */
  136. public function getMax()
  137. {
  138. return $this->_max;
  139. }
  140. /**
  141. * Sets the max option
  142. *
  143. * @param integer|null $max
  144. * @throws Zend_Validate_Exception
  145. * @return Zend_Validate_StringLength Provides a fluent interface
  146. */
  147. public function setMax($max)
  148. {
  149. if (null === $max) {
  150. $this->_max = null;
  151. } else if ($max < $this->_min) {
  152. /**
  153. * @see Zend_Validate_Exception
  154. */
  155. #require_once 'Zend/Validate/Exception.php';
  156. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
  157. . "$max < $this->_min");
  158. } else {
  159. $this->_max = (integer) $max;
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Returns the actual encoding
  165. *
  166. * @return string
  167. */
  168. public function getEncoding()
  169. {
  170. return $this->_encoding;
  171. }
  172. /**
  173. * Sets a new encoding to use
  174. *
  175. * @param string $encoding
  176. * @throws Zend_Validate_Exception
  177. * @return Zend_Validate_StringLength
  178. */
  179. public function setEncoding($encoding = null)
  180. {
  181. if ($encoding !== null) {
  182. $orig = PHP_VERSION_ID < 50600
  183. ? iconv_get_encoding('internal_encoding')
  184. : ini_get('default_charset');
  185. if (PHP_VERSION_ID < 50600) {
  186. if ($encoding) {
  187. $result = iconv_set_encoding('internal_encoding', $encoding);
  188. } else {
  189. $result = false;
  190. }
  191. } else {
  192. ini_set('default_charset', $encoding);
  193. $result = ini_get('default_charset');
  194. }
  195. if ($result === false) {
  196. #require_once 'Zend/Validate/Exception.php';
  197. throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
  198. }
  199. if (PHP_VERSION_ID < 50600) {
  200. iconv_set_encoding('internal_encoding', $orig);
  201. } else {
  202. ini_set('default_charset', $orig);
  203. }
  204. }
  205. $this->_encoding = $encoding;
  206. return $this;
  207. }
  208. /**
  209. * Defined by Zend_Validate_Interface
  210. *
  211. * Returns true if and only if the string length of $value is at least the min option and
  212. * no greater than the max option (when the max option is not null).
  213. *
  214. * @param string $value
  215. * @return boolean
  216. */
  217. public function isValid($value)
  218. {
  219. if (!is_string($value)) {
  220. $this->_error(self::INVALID);
  221. return false;
  222. }
  223. $this->_setValue($value);
  224. if ($this->_encoding !== null) {
  225. $length = iconv_strlen($value, $this->_encoding);
  226. } else {
  227. $length = iconv_strlen($value);
  228. }
  229. if ($length < $this->_min) {
  230. $this->_error(self::TOO_SHORT);
  231. }
  232. if (null !== $this->_max && $this->_max < $length) {
  233. $this->_error(self::TOO_LONG);
  234. }
  235. if (count($this->_messages)) {
  236. return false;
  237. } else {
  238. return true;
  239. }
  240. }
  241. }