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; must be between the given bounds.
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 Jakub Tománek
23: */
24: class Jyxo_Input_Validator_StringLengthBetween extends Jyxo_Input_Validator_AbstractValidator
25: {
26: /**
27: * Minimal string length.
28: *
29: * @var integer
30: */
31: protected $min;
32:
33: /**
34: * Maximal string length.
35: *
36: * @var integer
37: */
38: protected $max;
39:
40: /**
41: * Constructor.
42: *
43: * Sets both maximal and minimal string length.
44: *
45: * @param integer $min Minimal length (string length must be greater of equal)
46: * @param integer $max Maximal length (string length must be less or equal)
47: * @throws InvalidArgumentException Arguments are invalid (less than zero or minimum is greater than maximum)
48: */
49: public function __construct($min, $max)
50: {
51: $this->setMax($max);
52: $this->setMin($min);
53: }
54:
55: /**
56: * Sets the minimal string length.
57: *
58: * @param integer $min Minimal string length
59: * @return Jyxo_Input_Validator_StringLengthBetween
60: * @throws InvalidArgumentException If the minimal length is negative or greater than the maximal length
61: */
62: public function setMin($min)
63: {
64: $min = (int) $min;
65: if ($min < 0) {
66: throw new InvalidArgumentException('Length of string must be greater than zero.');
67: }
68: if ($min > $this->getMax()) {
69: throw new InvalidArgumentException('Min length must be lower or equal to max length.');
70: }
71:
72: $this->min = $min;
73: return $this;
74: }
75:
76: /**
77: * Return the minimal string length.
78: *
79: * @return integer
80: */
81: public function getMin()
82: {
83: return $this->min;
84: }
85:
86: /**
87: * Sets the maximal string length.
88: *
89: * @param integer $max Maximal string length
90: * @return Jyxo_Input_Validator_StringLengthBetween
91: * @throws InvalidArgumentException If the maximal length is negative or lower than the minimal length
92: */
93: public function setMax($max)
94: {
95: $max = (int) $max;
96: if ($max <= 0) {
97: throw new InvalidArgumentException('Length of string must be greater than zero.');
98: }
99: if ($max < $this->getMin()) {
100: throw new InvalidArgumentException('Min length must be lower or equal to max length.');
101: }
102:
103: $this->max = $max;
104:
105: return $this;
106: }
107:
108: /**
109: * Returns the maximum string length.
110: *
111: * @return integer
112: */
113: public function getMax()
114: {
115: return $this->max;
116: }
117:
118: /**
119: * Validates a value.
120: *
121: * @param string $value Input value
122: * @return boolean
123: */
124: public function isValid($value)
125: {
126: $length = mb_strlen((string) $value, 'utf-8');
127: return ($length >= $this->getMin()) && ($length <= $this->getMax());
128: }
129: }
130: