MinLength::setParameter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @package   Fuel\Validation
4
 * @version   2.0
5
 * @author    Fuel Development Team
6
 * @license   MIT License
7
 * @copyright 2010 - 2013 Fuel Development Team
8
 * @link      http://fuelphp.com
9
 */
10
11
namespace Fuel\Validation\Rule;
12
13
use Fuel\Validation\AbstractRule;
14
15
/**
16
 * Checks that the value is longer than the given minimum length.
17
 *
18
 * @package Fuel\Validation\Rule
19
 * @author  Fuel Development Team
20
 *
21
 * @since	2.0
22
 */
23 View Code Duplication
class MinLength extends AbstractRule
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 17
	public function validate($value, $field = null, $allFields = null)
43
	{
44
45 17
		if ( (is_object($value) and ! method_exists($value, '__toString')) or $this->getParameter() === null )
46
		{
47 6
			return false;
48
		}
49
50 11
		mb_internal_encoding('UTF-8');
51
52 11
		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 22
	public function setParameter($params)
65
	{
66
		// Ensure we have only a single thing to match against
67 22
		if (is_array($params))
68
		{
69 1
			$params = array_shift($params);
70
		}
71
72 22
		return parent::setParameter($params);
73
	}
74
75
	/**
76
	 * Returns
77
	 *
78
	 * array(
79
	 * 		'minLength' => <target length>
80
	 * );
81
	 *
82
	 * @return string[]
83
	 */
84 1
	public function getMessageParameters()
85
	{
86
		return array(
87 1
			'minLength' => $this->getParameter(),
88
		);
89
	}
90
91
}
92