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: /**
15: * Validates string length to be lower than the given length.
16: *
17: * @category Jyxo
18: * @package Jyxo_Input
19: * @subpackage Validator
20: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
21: * @license https://github.com/jyxo/php/blob/master/license.txt
22: * @author Jan Pěček
23: */
24: class Jyxo_Input_Validator_StringLengthLessThan extends Jyxo_Input_Validator_AbstractValidator
25: {
26: /**
27: * Maximal string length.
28: *
29: * @var integer
30: */
31: protected $max;
32:
33: /**
34: * Constructor.
35: *
36: * @param integer $max Maximal string length (value length must be lower)
37: */
38: public function __construct($max)
39: {
40: $this->setMax($max);
41: }
42:
43: /**
44: * Sets the maximal string length.
45: *
46: * @param integer $max Maximal string length
47: * @return Jyxo_Input_Validator_StringLengthLessThan
48: * @throws InvalidArgumentException If the maximal length is negative or zero
49: */
50: public function setMax($max)
51: {
52: $max = (int) $max;
53:
54: if ($max <= 0) {
55: throw new InvalidArgumentException('Length of string must be greater than zero.');
56: }
57:
58: $this->max = $max;
59:
60: return $this;
61: }
62:
63: /**
64: * Returns the maximal string length.
65: *
66: * @return integer
67: */
68: public function getMax()
69: {
70: return $this->max;
71: }
72:
73: /**
74: * Validates a value.
75: *
76: * @param mixed $value Input value
77: * @return boolean
78: */
79: public function isValid($value)
80: {
81: return mb_strlen((string) $value, 'utf-8') < $this->getMax();
82: }
83: }
84: