3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Validator
;
12 use Zend\Stdlib\StringUtils
;
13 use Zend\Stdlib\StringWrapper\StringWrapperInterface
as StringWrapper
;
15 class StringLength
extends AbstractValidator
17 const INVALID
= 'stringLengthInvalid';
18 const TOO_SHORT
= 'stringLengthTooShort';
19 const TOO_LONG
= 'stringLengthTooLong';
24 protected $messageTemplates = array(
25 self
::INVALID
=> "Invalid type given. String expected",
26 self
::TOO_SHORT
=> "The input is less than %min% characters long",
27 self
::TOO_LONG
=> "The input is more than %max% characters long",
33 protected $messageVariables = array(
34 'min' => array('options' => 'min'),
35 'max' => array('options' => 'max'),
38 protected $options = array(
39 'min' => 0, // Minimum length
40 'max' => null, // Maximum length, null if there is no length limitation
41 'encoding' => 'UTF-8', // Encoding to use
44 protected $stringWrapper;
47 * Sets validator options
49 * @param int|array|\Traversable $options
51 public function __construct($options = array())
53 if (!is_array($options)) {
54 $options = func_get_args();
55 $temp['min'] = array_shift($options);
56 if (!empty($options)) {
57 $temp['max'] = array_shift($options);
60 if (!empty($options)) {
61 $temp['encoding'] = array_shift($options);
67 parent
::__construct($options);
71 * Returns the min option
75 public function getMin()
77 return $this->options
['min'];
84 * @throws Exception\InvalidArgumentException
85 * @return StringLength Provides a fluent interface
87 public function setMin($min)
89 if (null !== $this->getMax() && $min > $this->getMax()) {
90 throw new Exception\
InvalidArgumentException("The minimum must be less than or equal to the maximum length, but $min >"
91 . " " . $this->getMax());
94 $this->options
['min'] = max(0, (int) $min);
99 * Returns the max option
103 public function getMax()
105 return $this->options
['max'];
109 * Sets the max option
111 * @param int|null $max
112 * @throws Exception\InvalidArgumentException
113 * @return StringLength Provides a fluent interface
115 public function setMax($max)
118 $this->options
['max'] = null;
119 } elseif ($max < $this->getMin()) {
120 throw new Exception\
InvalidArgumentException("The maximum must be greater than or equal to the minimum length, but "
121 . "$max < " . $this->getMin());
123 $this->options
['max'] = (int) $max;
130 * Get the string wrapper to detect the string length
132 * @return StringWrapper
134 public function getStringWrapper()
136 if (!$this->stringWrapper
) {
137 $this->stringWrapper
= StringUtils
::getWrapper($this->getEncoding());
139 return $this->stringWrapper
;
143 * Set the string wrapper to detect the string length
145 * @param StringWrapper $stringWrapper
146 * @return StringLength
148 public function setStringWrapper(StringWrapper
$stringWrapper)
150 $stringWrapper->setEncoding($this->getEncoding());
151 $this->stringWrapper
= $stringWrapper;
155 * Returns the actual encoding
159 public function getEncoding()
161 return $this->options
['encoding'];
165 * Sets a new encoding to use
167 * @param string $encoding
168 * @return StringLength
169 * @throws Exception\InvalidArgumentException
171 public function setEncoding($encoding)
173 $this->stringWrapper
= StringUtils
::getWrapper($encoding);
174 $this->options
['encoding'] = $encoding;
179 * Returns true if and only if the string length of $value is at least the min option and
180 * no greater than the max option (when the max option is not null).
182 * @param string $value
185 public function isValid($value)
187 if (!is_string($value)) {
188 $this->error(self
::INVALID
);
192 $this->setValue($value);
194 $length = $this->getStringWrapper()->strlen($value);
195 if ($length < $this->getMin()) {
196 $this->error(self
::TOO_SHORT
);
199 if (null !== $this->getMax() && $this->getMax() < $length) {
200 $this->error(self
::TOO_LONG
);
203 if (count($this->getMessages())) {