Code Duplication    Length = 68-69 lines in 3 locations

src/Rule/ExactLength.php 1 location

@@ 23-90 (lines=68) @@
20
 *
21
 * @since 2.0
22
 */
23
class ExactLength extends AbstractRule
24
{
25
26
	/**
27
	 * Default failure message
28
	 *
29
	 * @var string
30
	 */
31
	protected $message = 'The length of the field is not exactly equal to the length specified.';
32
33
	/**
34
	 * @param mixed  $value
35
	 * @param string $field
36
	 * @param array  $allFields
37
	 *
38
	 * @return bool
39
	 *
40
	 * @since 2.0
41
	 */
42
	public function validate($value, $field = null, $allFields = null)
43
	{
44
		if ( (is_object($value) and ! method_exists($value, '__toString')) or $this->getParameter() === null )
45
		{
46
			return false;
47
		}
48
49
		mb_internal_encoding('UTF-8');
50
51
		return (mb_strlen(( string ) $value) == $this->getParameter());
52
	}
53
54
	/**
55
	 * Sets the value to check against
56
	 *
57
	 * @param string $params If an array the first value will be used
58
	 *
59
	 * @return $this
60
	 *
61
	 * @since 2.0
62
	 */
63
	public function setParameter($params)
64
	{
65
		// Ensure we have only a single thing to match against
66
		if (is_array($params))
67
		{
68
			$params = array_shift($params);
69
		}
70
71
		return parent::setParameter($params);
72
	}
73
74
	/**
75
	 * Returns
76
	 *
77
	 * array(
78
	 * 		'length' => <target length>
79
	 * );
80
	 *
81
	 * @return string[]
82
	 */
83
	public function getMessageParameters()
84
	{
85
		return array(
86
			'length' => $this->getParameter(),
87
		);
88
	}
89
90
}
91

src/Rule/MaxLength.php 1 location

@@ 23-90 (lines=68) @@
20
 *
21
 * @since 2.0
22
 */
23
class MaxLength extends AbstractRule
24
{
25
26
	/**
27
	 * Default failure message
28
	 *
29
	 * @var string
30
	 */
31
	protected $message = 'The field is longer than the allowed maximum length.';
32
33
	/**
34
	 * @param mixed  $value
35
	 * @param string $field
36
	 * @param array  $allFields
37
	 *
38
	 * @return bool
39
	 *
40
	 * @since 2.0
41
	 */
42
	public function validate($value, $field = null, $allFields = null)
43
	{
44
		if ( (is_object($value) and ! method_exists($value, '__toString')) or $this->getParameter() === null)
45
		{
46
			return false;
47
		}
48
49
		mb_internal_encoding('UTF-8');
50
51
		return (mb_strlen(( string ) $value) <= $this->getParameter());
52
	}
53
54
	/**
55
	 * Sets the value to check against
56
	 *
57
	 * @param string $params If an array the first value will be used
58
	 *
59
	 * @return $this
60
	 *
61
	 * @since 2.0
62
	 */
63
	public function setParameter($params)
64
	{
65
		// Ensure we have only a single thing to match against
66
		if (is_array($params))
67
		{
68
			$params = array_shift($params);
69
		}
70
71
		return parent::setParameter($params);
72
	}
73
74
	/**
75
	 * Returns
76
	 *
77
	 * array(
78
	 * 		'maxLength' => <target length>
79
	 * );
80
	 *
81
	 * @return string[]
82
	 */
83
	public function getMessageParameters()
84
	{
85
		return array(
86
			'maxLength' => $this->getParameter(),
87
		);
88
	}
89
90
}
91

src/Rule/MinLength.php 1 location

@@ 23-91 (lines=69) @@
20
 *
21
 * @since	2.0
22
 */
23
class MinLength extends AbstractRule
24
{
25
26
	/**
27
	 * Contains the rule failure message
28
	 *
29
	 * @var string
30
	 */
31
	protected $message = 'The field does not satisfy the minimum length requirement.';
32
33
	/**
34
	 * @param mixed  $value Value to be validated
35
	 * @param string $field Unused by this rule
36
	 * @param array  $allFields Unused by this rule
37
	 *
38
	 * @return bool
39
	 *
40
	 * @since 2.0
41
	 */
42
	public function validate($value, $field = null, $allFields = null)
43
	{
44
45
		if ( (is_object($value) and ! method_exists($value, '__toString')) or $this->getParameter() === null )
46
		{
47
			return false;
48
		}
49
50
		mb_internal_encoding('UTF-8');
51
52
		return (mb_strlen(( string ) $value) >= $this->getParameter());
53
	}
54
55
	/**
56
	 * Sets the value to check against
57
	 *
58
	 * @param string $params If an array the first value will be used
59
	 *
60
	 * @return $this
61
	 *
62
	 * @since 2.0
63
	 */
64
	public function setParameter($params)
65
	{
66
		// Ensure we have only a single thing to match against
67
		if (is_array($params))
68
		{
69
			$params = array_shift($params);
70
		}
71
72
		return parent::setParameter($params);
73
	}
74
75
	/**
76
	 * Returns
77
	 *
78
	 * array(
79
	 * 		'minLength' => <target length>
80
	 * );
81
	 *
82
	 * @return string[]
83
	 */
84
	public function getMessageParameters()
85
	{
86
		return array(
87
			'minLength' => $this->getParameter(),
88
		);
89
	}
90
91
}
92