1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: namespace Jyxo\Input\Validator;
15:
16: /**
17: * Validates string length to be lower than the given length.
18: *
19: * @category Jyxo
20: * @package Jyxo\Input
21: * @subpackage Validator
22: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
23: * @license https://github.com/jyxo/php/blob/master/license.txt
24: * @author Jan Pěček
25: */
26: class StringLengthLessThan extends \Jyxo\Input\Validator\AbstractValidator
27: {
28: /**
29: * Maximal string length.
30: *
31: * @var integer
32: */
33: protected $max;
34:
35: /**
36: * Constructor.
37: *
38: * @param integer $max Maximal string length (value length must be lower)
39: */
40: public function __construct($max)
41: {
42: $this->setMax($max);
43: }
44:
45: /**
46: * Sets the maximal string length.
47: *
48: * @param integer $max Maximal string length
49: * @return \Jyxo\Input\Validator\StringLengthLessThan
50: * @throws \InvalidArgumentException If the maximal length is negative or zero
51: */
52: public function setMax($max)
53: {
54: $max = (int) $max;
55:
56: if ($max <= 0) {
57: throw new \InvalidArgumentException('Length of string must be greater than zero.');
58: }
59:
60: $this->max = $max;
61:
62: return $this;
63: }
64:
65: /**
66: * Returns the maximal string length.
67: *
68: * @return integer
69: */
70: public function getMax()
71: {
72: return $this->max;
73: }
74:
75: /**
76: * Validates a value.
77: *
78: * @param mixed $value Input value
79: * @return boolean
80: */
81: public function isValid($value)
82: {
83: return mb_strlen((string) $value, 'utf-8') < $this->getMax();
84: }
85: }
86: