Kohana_Jam_Validator   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 4
dl 0
loc 95
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 5
B condition_met() 0 25 6
A html5_validation() 0 15 6
C validate_model() 0 19 12
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Jam Validation
4
 *
5
 * Jam_Validation overrides Kohana's core validation class in order to add a few
6
 * Jam-specific features.
7
 *
8
 * @package    Jam
9
 * @category   Security
10
 * @author     Ivan Kerin
11
 * @copyright  (c) 2011-2012 Despark Ltd.
12
 * @license    http://www.opensource.org/licenses/isc-license.txt
13
 */
14
abstract class Kohana_Jam_Validator {
15
16
	public $attributes = array();
17
18
	public $rules = array();
19
20
	public $condition = NULL;
21
	public $condition_negative = FALSE;
22
23 31
	function __construct(array $attributes, array $options)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
	{
25 31
		$this->attributes = $attributes;
26
27 31
		if (isset($options['if']))
28
		{
29 17
			$this->condition = $options['if'];
30 17
			unset($options['if']);
31
		}
32
33 31
		if (isset($options['unless']))
34
		{
35 13
			$this->condition = $options['unless'];
36 13
			$this->condition_negative = TRUE;
37 13
			unset($options['unless']);
38
		}
39
40 31
		foreach ($options as $rule => $params)
41
		{
42 27
			$this->rules[] = ($params instanceof Jam_Validator_Rule) ? $params : Jam::validator_rule($rule, $params);
43
		}
44 31
	}
45
46 37
	public function condition_met(Jam_Validated $model)
47
	{
48 37
		if ( ! $this->condition)
49 12
			return TRUE;
50
51
52 25
		if (is_string($this->condition) AND strpos($this->condition, '::') === FALSE)
53
		{
54 21
			if (substr($this->condition, -2) == '()')
55
			{
56 4
				$method_name = substr($this->condition, 0, -2);
57 4
				$result = $model->$method_name();
58
			}
59
			else
60
			{
61 21
				$result = $model->{$this->condition};
62
			}
63
		}
64
		else
65
		{
66 4
			$result = call_user_func($this->condition, $model, $this->attributes);
67
		}
68
69 25
		return $this->condition_negative ? ! $result : (bool) $result;
70
	}
71
72 11
	public function html5_validation(Jam_Validated $model, $name)
73
	{
74 11
		$attributes = array();
75 11
		if (in_array($name, $this->attributes) AND $model instanceof Jam_Validated AND $this->condition_met($model))
76
		{
77 7
			foreach ($this->rules as $rule)
78
			{
79 7
				if ($rule_attributes = $rule->html5_validation($name))
80
				{
81 7
					$attributes = Arr::merge($attributes, $rule_attributes);
82
				}
83
			}
84
		}
85 11
		return $attributes;
86
	}
87
88 13
	public function validate_model(Jam_Validated $model, $force = FALSE)
89
	{
90 13
		foreach ($this->attributes as $attribute)
91
		{
92 13
			if ($model instanceof Jam_Validated AND $this->condition_met($model))
93
			{
94 12
				if ($force OR ($model instanceof Jam_Model AND ! $model->loaded()) OR $model->changed($attribute) OR $model->unmapped($attribute) OR ! $model->{$attribute})
95
				{
96 12
					foreach ($this->rules as $rule)
97
					{
98 12
						if ($rule->is_processable_attribute($model, $attribute))
99
						{
100 13
							$rule->validate($model, $attribute, $model->$attribute);
101
						}
102
					}
103
				}
104
			}
105
		}
106 13
	}
107
108
} // End Jam_Validation
109