Kohana_Jam_Validator_Rule_Format::validate()   C
last analyzed

Complexity

Conditions 15
Paths 128

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 15

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 16
cts 16
cp 1
rs 5.6833
c 0
b 0
f 0
cc 15
nc 128
nop 3
crap 15

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Jam Validatior Rule
4
 *
5
 * @package    Jam
6
 * @category   Validation
7
 * @author     Ivan Kerin
8
 * @copyright  (c) 2011-2012 Despark Ltd.
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
class Kohana_Jam_Validator_Rule_Format extends Jam_Validator_Rule {
12
13
	public $regex;
14
15
	public $filter;
16
17
	public $flag;
18
19
	public $email;
20
21
	public $url;
22
23
	public $ip;
24
25
	public $credit_card;
26
27
	public $date;
28
29 36
	public function validate(Jam_Validated $model, $attribute, $value)
30
	{
31 36
		if ($this->regex !== NULL AND ! (preg_match($this->regex, $value)))
32
		{
33 1
			$model->errors()->add($attribute, 'format_regex', array(':regex' => $this->regex));
34
		}
35
36 36
		if ($this->filter !== NULL AND ! (filter_var($value, $this->filter, $this->flag) !== FALSE))
37
		{
38 8
			$model->errors()->add($attribute, 'format_filter', array(':filter' => $this->filter));
39
		}
40
41 36
		if ($this->ip === TRUE AND ! (Valid::ip($value)))
42
		{
43 1
			$model->errors()->add($attribute, 'format_ip');
44
		}
45
46 36
		if ($this->url === TRUE AND ! (Valid::url($value)))
47
		{
48 3
			$model->errors()->add($attribute, 'format_url');
49
		}
50
51 36
		if ($this->email === TRUE AND ! (Valid::email($value)))
52
		{
53 3
			$model->errors()->add($attribute, 'format_email');
54
		}
55
56 36
		if ($this->credit_card === TRUE AND ! (Valid::credit_card($value)))
57
		{
58 1
			$model->errors()->add($attribute, 'format_credit_card');
59
		}
60
61 36
		if ($this->date === TRUE AND strtotime($value) === FALSE)
62
		{
63 2
			$model->errors()->add($attribute, 'format_date');
64
		}
65 36
	}
66
67 36
	public function html5_validation()
68
	{
69 36
		if ($this->url)
70
		{
71 4
			return array('type' => 'url');
72
		}
73 32
		elseif ($this->email)
74
		{
75 4
			return array('type' => 'email');
76
		}
77 28
		elseif ($this->date)
78
		{
79 4
			return array('type' => 'date');
80
		}
81 24
		elseif ($this->regex)
82
		{
83 2
			return array('pattern' => trim($this->regex, '/'));
84
		}
85 22
		elseif ($this->credit_card)
86
		{
87 5
			return array('pattern' => '^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$');
88
		}
89 17
	}
90
}
91