Kohana_Jam_Validator_Rule_Format   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 80
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validate() 0 37 15
B html5_validation() 0 23 6
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