PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Validate/StringLength.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 253 lines | 119 code | 27 blank | 107 comment | 24 complexity | 3f0ca24931ceb91a1e49e3c3df55810e 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. * @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. $options = func_get_args();
  83. $temp['min'] = array_shift($options);
  84. if (!empty($options)) {
  85. $temp['max'] = array_shift($options);
  86. }
  87. if (!empty($options)) {
  88. $temp['encoding'] = array_shift($options);
  89. }
  90. $options = $temp;
  91. }
  92. if (!array_key_exists('min', $options)) {
  93. $options['min'] = 0;
  94. }
  95. $this->setMin($options['min']);
  96. if (array_key_exists('max', $options)) {
  97. $this->setMax($options['max']);
  98. }
  99. if (array_key_exists('encoding', $options)) {
  100. $this->setEncoding($options['encoding']);
  101. }
  102. }
  103. /**
  104. * Returns the min option
  105. *
  106. * @return integer
  107. */
  108. public function getMin()
  109. {
  110. return $this->_min;
  111. }
  112. /**
  113. * Sets the min option
  114. *
  115. * @param integer $min
  116. * @throws Zend_Validate_Exception
  117. * @return Zend_Validate_StringLength Provides a fluent interface
  118. */
  119. public function setMin($min)
  120. {
  121. if (null !== $this->_max && $min > $this->_max) {
  122. /**
  123. * @see Zend_Validate_Exception
  124. */
  125. require_once 'Zend/Validate/Exception.php';
  126. throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum length, but $min >"
  127. . " $this->_max");
  128. }
  129. $this->_min = max(0, (integer) $min);
  130. return $this;
  131. }
  132. /**
  133. * Returns the max option
  134. *
  135. * @return integer|null
  136. */
  137. public function getMax()
  138. {
  139. return $this->_max;
  140. }
  141. /**
  142. * Sets the max option
  143. *
  144. * @param integer|null $max
  145. * @throws Zend_Validate_Exception
  146. * @return Zend_Validate_StringLength Provides a fluent interface
  147. */
  148. public function setMax($max)
  149. {
  150. if (null === $max) {
  151. $this->_max = null;
  152. } else if ($max < $this->_min) {
  153. /**
  154. * @see Zend_Validate_Exception
  155. */
  156. require_once 'Zend/Validate/Exception.php';
  157. throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum length, but "
  158. . "$max < $this->_min");
  159. } else {
  160. $this->_max = (integer) $max;
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Returns the actual encoding
  166. *
  167. * @return string
  168. */
  169. public function getEncoding()
  170. {
  171. return $this->_encoding;
  172. }
  173. /**
  174. * Sets a new encoding to use
  175. *
  176. * @param string $encoding
  177. * @return Zend_Validate_StringLength
  178. */
  179. public function setEncoding($encoding = null)
  180. {
  181. if ($encoding !== null) {
  182. $orig = iconv_get_encoding('internal_encoding');
  183. $result = iconv_set_encoding('internal_encoding', $encoding);
  184. if (!$result) {
  185. require_once 'Zend/Validate/Exception.php';
  186. throw new Zend_Validate_Exception('Given encoding not supported on this OS!');
  187. }
  188. iconv_set_encoding('internal_encoding', $orig);
  189. }
  190. $this->_encoding = $encoding;
  191. return $this;
  192. }
  193. /**
  194. * Defined by Zend_Validate_Interface
  195. *
  196. * Returns true if and only if the string length of $value is at least the min option and
  197. * no greater than the max option (when the max option is not null).
  198. *
  199. * @param string $value
  200. * @return boolean
  201. */
  202. public function isValid($value)
  203. {
  204. if (!is_string($value)) {
  205. $this->_error(self::INVALID);
  206. return false;
  207. }
  208. $this->_setValue($value);
  209. if ($this->_encoding !== null) {
  210. $length = iconv_strlen($value, $this->_encoding);
  211. } else {
  212. $length = iconv_strlen($value);
  213. }
  214. if ($length < $this->_min) {
  215. $this->_error(self::TOO_SHORT);
  216. }
  217. if (null !== $this->_max && $this->_max < $length) {
  218. $this->_error(self::TOO_LONG);
  219. }
  220. if (count($this->_messages)) {
  221. return false;
  222. } else {
  223. return true;
  224. }
  225. }
  226. }