/lib/Zend/Validate/StringLength.php

https://github.com/gryzz/crystal_magento · PHP · 229 lines · 95 code · 22 blank · 112 comment · 16 complexity · 1eb6469b33bd3e2729ea31f4565f7a4d MD5 · raw file

  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: StringLength.php 16223 2009-06-21 20:04:53Z thomas $
  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 $min
  75. * @param integer $max
  76. * @return void
  77. */
  78. public function __construct($min = 0, $max = null, $encoding = null)
  79. {
  80. $this->setMin($min);
  81. $this->setMax($max);
  82. $this->setEncoding($encoding);
  83. }
  84. /**
  85. * Returns the min option
  86. *
  87. * @return integer
  88. */
  89. public function getMin()
  90. {
  91. return $this->_min;
  92. }
  93. /**
  94. * Sets the min option
  95. *
  96. * @param integer $min
  97. * @throws Zend_Validate_Exception
  98. * @return Zend_Validate_StringLength Provides a fluent interface
  99. */
  100. public function setMin($min)
  101. {
  102. if (null !== $this->_max && $min > $this->_max) {
  103. /**
  104. * @see Zend_Validate_Exception
  105. */
  106. #require_once 'Zend/Validate/Exception.php';
  107. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
  108. . " $this->_max");
  109. }
  110. $this->_min = max(0, (integer) $min);
  111. return $this;
  112. }
  113. /**
  114. * Returns the max option
  115. *
  116. * @return integer|null
  117. */
  118. public function getMax()
  119. {
  120. return $this->_max;
  121. }
  122. /**
  123. * Sets the max option
  124. *
  125. * @param integer|null $max
  126. * @throws Zend_Validate_Exception
  127. * @return Zend_Validate_StringLength Provides a fluent interface
  128. */
  129. public function setMax($max)
  130. {
  131. if (null === $max) {
  132. $this->_max = null;
  133. } else if ($max < $this->_min) {
  134. /**
  135. * @see Zend_Validate_Exception
  136. */
  137. #require_once 'Zend/Validate/Exception.php';
  138. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
  139. . "$max < $this->_min");
  140. } else {
  141. $this->_max = (integer) $max;
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Returns the actual encoding
  147. *
  148. * @return string
  149. */
  150. public function getEncoding()
  151. {
  152. return $this->_encoding;
  153. }
  154. /**
  155. * Sets a new encoding to use
  156. *
  157. * @param string $encoding
  158. * @return Zend_Validate_StringLength
  159. */
  160. public function setEncoding($encoding = null)
  161. {
  162. if ($encoding !== null) {
  163. $orig = iconv_get_encoding('internal_encoding');
  164. $result = iconv_set_encoding('internal_encoding', $encoding);
  165. if (!$result) {
  166. #require_once 'Zend/Validate/Exception.php';
  167. throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
  168. }
  169. iconv_set_encoding('internal_encoding', $orig);
  170. }
  171. $this->_encoding = $encoding;
  172. return $this;
  173. }
  174. /**
  175. * Defined by Zend_Validate_Interface
  176. *
  177. * Returns true if and only if the string length of $value is at least the min option and
  178. * no greater than the max option (when the max option is not null).
  179. *
  180. * @param string $value
  181. * @return boolean
  182. */
  183. public function isValid($value)
  184. {
  185. if (!is_string($value)) {
  186. $this->_error(self::INVALID);
  187. return false;
  188. }
  189. $this->_setValue($value);
  190. if ($this->_encoding !== null) {
  191. $length = iconv_strlen($value, $this->_encoding);
  192. } else {
  193. $length = iconv_strlen($value);
  194. }
  195. if ($length < $this->_min) {
  196. $this->_error(self::TOO_SHORT);
  197. }
  198. if (null !== $this->_max && $this->_max < $length) {
  199. $this->_error(self::TOO_LONG);
  200. }
  201. if (count($this->_messages)) {
  202. return false;
  203. } else {
  204. return true;
  205. }
  206. }
  207. }