Completed
Push — master ( 12b9a9...920beb )
by Steve
02:42
created

Enum::setStrict()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 if a value is one of given values
17
 *
18
 * @package Fuel\Validation\Rule
19
 * @author  Fuel Development Team
20
 *
21
 * @since   2.0
22
 */
23
class Enum extends AbstractRule
24
{
25
26
	/**
27
	 * Contains the rule failure message
28
	 *
29
	 * @var string
30
	 */
31
	protected $message = 'The field is not one of the given value(s).';
32
33
	/**
34
	 * Strict check mode
35
	 *
36
	 * @var bool
37
	 */
38
	protected $strict = false;
39
40
	/**
41
	 * Performs validation on the given value
42
	 *
43
	 * @param mixed  $value Value to validate
44
	 * @param string $field Unused by this rule
45
	 * @param array  $allFields Unused by this rule
46
	 *
47
	 * @return bool
48
	 *
49
	 * @since 2.0
50
	 */
51 8
	public function validate($value, $field = null, $allFields = null)
52
	{
53 8
		$values = $this->getParameter();
54
55 8
		return in_array($value, $values, $this->strict);
56
	}
57
58
	/**
59
	 * Sets the value(s) and mode to check against
60
	 *
61
	 * @param string $params
62
	 *
63
	 * @return $this
64
	 *
65
	 * @since 2.0
66
	 */
67 11
	public function setParameter($params)
68
	{
69 11
		if (is_array($params) === false)
70 11
		{
71 11
			$params = (array) $params;
72 11
		}
73 6
		elseif (array_key_exists('strict', $params) and array_key_exists('values', $params))
74
		{
75 5
			$this->strict = (bool) $params['strict'];
76 5
			$params = $params['values'];
77 5
		}
78
79 11
		return parent::setParameter($params);
80
	}
81
82
	/**
83
	 * Check strict mode
84
	 *
85
	 * @return bool
86
	 *
87
	 * @since 2.0
88
	 */
89 1
	public function isStrict()
90
	{
91 1
		return $this->strict;
92
	}
93
94
	/**
95
	 * Set strict mode
96
	 *
97
	 * @param bool $strict
98
	 *
99
	 * @return $this
100
	 *
101
	 * @since 2.0
102
	 */
103 1
	public function setStrict($strict)
104
	{
105 1
		$this->strict = (bool) $strict;
106
107 1
		return $this;
108
	}
109
110
}
111