Kohana_Jam_Validator_Rule_Range   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 60
ccs 18
cts 21
cp 0.8571
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 45 17
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_Range extends Jam_Validator_Rule {
12
13
	public $consecutive;
14
15
	public $minimum;
16
17
	public $minimum_or_equal_to;
18
19
	public $maximum;
20
21
	public $maximum_or_equal_to;
22
23
	public $between;
24
25 30
	public function validate(Jam_Validated $model, $attribute, $value)
26
	{
27 30
		if ( ! ($value instanceof Jam_Range))
28
			throw new Kohana_Exception('Range validation rule can only be applied to range');
29
30 30
		if ( ! is_numeric($value->min()) OR ! is_numeric($value->max()))
31
		{
32
			$model->errors()->add($attribute, 'range_numeric');
33
34
			return;
35
		}
36
37 30
		$min = min($value->min(), $value->max());
38 30
		$max = max($value->min(), $value->max());
39
40 30
		if ($this->minimum !== NULL AND ($min <= $this->minimum))
41
		{
42 4
			$model->errors()->add($attribute, 'range_minimum', array(':minimum' => $this->minimum));
43
		}
44
45 30
		if ($this->minimum_or_equal_to !== NULL AND ($min < $this->minimum_or_equal_to))
46
		{
47 2
			$model->errors()->add($attribute, 'range_minimum_or_equal_to', array(':minimum_or_equal_to' => $this->minimum_or_equal_to));
48
		}
49
50 30
		if ($this->maximum !== NULL AND ($max >= $this->maximum))
51
		{
52 4
			$model->errors()->add($attribute, 'range_maximum', array(':maximum' => $this->maximum));
53
		}
54
55 30
		if ($this->maximum_or_equal_to !== NULL AND ($max > $this->maximum_or_equal_to))
56
		{
57 2
			$model->errors()->add($attribute, 'range_maximum_or_equal_to', array(':maximum_or_equal_to' => $this->maximum_or_equal_to));
58
		}
59
60 30
		if ($this->between !== NULL AND ! ($min >= $this->between[0] AND $max <= $this->between[1]))
61
		{
62 3
			$model->errors()->add($attribute, 'range_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1]));
63
		}
64
65 30
		if ($this->consecutive !== NULL AND ($value->min() > $value->max()))
66
		{
67 1
			$model->errors()->add($attribute, 'range_consecutive');
68
		}
69 30
	}
70
}
71