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