/vendor/zendframework/zend-validator/src/StringLength.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 211 lines · 112 code · 27 blank · 72 comment · 13 complexity · e4f44ddb0504e975bf98591f43f5ed2f MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator;
  10. use Zend\Stdlib\StringUtils;
  11. use Zend\Stdlib\StringWrapper\StringWrapperInterface as StringWrapper;
  12. class StringLength extends AbstractValidator
  13. {
  14. const INVALID = 'stringLengthInvalid';
  15. const TOO_SHORT = 'stringLengthTooShort';
  16. const TOO_LONG = 'stringLengthTooLong';
  17. /**
  18. * @var array
  19. */
  20. protected $messageTemplates = array(
  21. self::INVALID => "Invalid type given. String expected",
  22. self::TOO_SHORT => "The input is less than %min% characters long",
  23. self::TOO_LONG => "The input is more than %max% characters long",
  24. );
  25. /**
  26. * @var array
  27. */
  28. protected $messageVariables = array(
  29. 'min' => array('options' => 'min'),
  30. 'max' => array('options' => 'max'),
  31. );
  32. protected $options = array(
  33. 'min' => 0, // Minimum length
  34. 'max' => null, // Maximum length, null if there is no length limitation
  35. 'encoding' => 'UTF-8', // Encoding to use
  36. );
  37. protected $stringWrapper;
  38. /**
  39. * Sets validator options
  40. *
  41. * @param int|array|\Traversable $options
  42. */
  43. public function __construct($options = array())
  44. {
  45. if (!is_array($options)) {
  46. $options = func_get_args();
  47. $temp['min'] = array_shift($options);
  48. if (!empty($options)) {
  49. $temp['max'] = array_shift($options);
  50. }
  51. if (!empty($options)) {
  52. $temp['encoding'] = array_shift($options);
  53. }
  54. $options = $temp;
  55. }
  56. parent::__construct($options);
  57. }
  58. /**
  59. * Returns the min option
  60. *
  61. * @return int
  62. */
  63. public function getMin()
  64. {
  65. return $this->options['min'];
  66. }
  67. /**
  68. * Sets the min option
  69. *
  70. * @param int $min
  71. * @throws Exception\InvalidArgumentException
  72. * @return StringLength Provides a fluent interface
  73. */
  74. public function setMin($min)
  75. {
  76. if (null !== $this->getMax() && $min > $this->getMax()) {
  77. throw new Exception\InvalidArgumentException(
  78. "The minimum must be less than or equal to the maximum length, but {$min} > {$this->getMax()}"
  79. );
  80. }
  81. $this->options['min'] = max(0, (int) $min);
  82. return $this;
  83. }
  84. /**
  85. * Returns the max option
  86. *
  87. * @return int|null
  88. */
  89. public function getMax()
  90. {
  91. return $this->options['max'];
  92. }
  93. /**
  94. * Sets the max option
  95. *
  96. * @param int|null $max
  97. * @throws Exception\InvalidArgumentException
  98. * @return StringLength Provides a fluent interface
  99. */
  100. public function setMax($max)
  101. {
  102. if (null === $max) {
  103. $this->options['max'] = null;
  104. } elseif ($max < $this->getMin()) {
  105. throw new Exception\InvalidArgumentException(
  106. "The maximum must be greater than or equal to the minimum length, but {$max} < {$this->getMin()}"
  107. );
  108. } else {
  109. $this->options['max'] = (int) $max;
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Get the string wrapper to detect the string length
  115. *
  116. * @return StringWrapper
  117. */
  118. public function getStringWrapper()
  119. {
  120. if (!$this->stringWrapper) {
  121. $this->stringWrapper = StringUtils::getWrapper($this->getEncoding());
  122. }
  123. return $this->stringWrapper;
  124. }
  125. /**
  126. * Set the string wrapper to detect the string length
  127. *
  128. * @param StringWrapper $stringWrapper
  129. * @return StringLength
  130. */
  131. public function setStringWrapper(StringWrapper $stringWrapper)
  132. {
  133. $stringWrapper->setEncoding($this->getEncoding());
  134. $this->stringWrapper = $stringWrapper;
  135. }
  136. /**
  137. * Returns the actual encoding
  138. *
  139. * @return string
  140. */
  141. public function getEncoding()
  142. {
  143. return $this->options['encoding'];
  144. }
  145. /**
  146. * Sets a new encoding to use
  147. *
  148. * @param string $encoding
  149. * @return StringLength
  150. * @throws Exception\InvalidArgumentException
  151. */
  152. public function setEncoding($encoding)
  153. {
  154. $this->stringWrapper = StringUtils::getWrapper($encoding);
  155. $this->options['encoding'] = $encoding;
  156. return $this;
  157. }
  158. /**
  159. * Returns true if and only if the string length of $value is at least the min option and
  160. * no greater than the max option (when the max option is not null).
  161. *
  162. * @param string $value
  163. * @return bool
  164. */
  165. public function isValid($value)
  166. {
  167. if (!is_string($value)) {
  168. $this->error(self::INVALID);
  169. return false;
  170. }
  171. $this->setValue($value);
  172. $length = $this->getStringWrapper()->strlen($value);
  173. if ($length < $this->getMin()) {
  174. $this->error(self::TOO_SHORT);
  175. }
  176. if (null !== $this->getMax() && $this->getMax() < $length) {
  177. $this->error(self::TOO_LONG);
  178. }
  179. if (count($this->getMessages())) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. }