Completed
Push — count-subquery-clone ( d5815f...6cc328 )
by Haralan
03:48
created

Kohana_Jam_Validator_Rule_Numeric   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.92%

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 23
c 3
b 2
f 1
lcom 1
cbo 2
dl 0
loc 87
ccs 47
cts 49
cp 0.9592
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
F validate() 0 52 21
A html5_validation() 0 13 2
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_Numeric extends Jam_Validator_Rule {
12
13
	public $greater_than_or_equal_to;
14
15
	public $greater_than;
16
17
	public $equal_to;
18
19
	public $less_than;
20
21
	public $less_than_or_equal_to;
22
23
	public $between;
24
25
	public $odd;
26
27
	public $even;
28
29
	public $only_integer;
30
31 30
	public function validate(Jam_Validated $model, $attribute, $value)
32
	{
33 30
		if ( ! is_numeric($value))
34 30
		{
35 2
			$model->errors()->add($attribute, 'numeric');
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
36 2
		}
37
38 30
		if ($this->only_integer !== NULL AND ! (filter_var($value, FILTER_VALIDATE_INT) !== FALSE))
39 30
		{
40 2
			$model->errors()->add($attribute, 'numeric_only_integer');
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
41 2
		}
42
43 30
		if ($this->greater_than_or_equal_to !== NULL AND ! ($value >= $this->greater_than_or_equal_to))
44 30
		{
45 1
			$model->errors()->add($attribute, 'numeric_greater_than_or_equal_to', array(':greater_than_or_equal_to' => $this->greater_than_or_equal_to));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
46 1
		}
47
48 30
		if ($this->greater_than !== NULL AND ! ($value > $this->greater_than))
49 30
		{
50 2
			$model->errors()->add($attribute, 'numeric_greater_than', array(':greater_than' => $this->greater_than));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
51 2
		}
52
53 30
		if ($this->equal_to !== NULL AND ! ($value == $this->equal_to))
54 30
		{
55 2
			$model->errors()->add($attribute, 'numeric_equal_to', array(':equal_to' => $this->equal_to));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
56 2
		}
57
58 30
		if ($this->less_than !== NULL AND ! ($value < $this->less_than))
59 30
		{
60 2
			$model->errors()->add($attribute, 'numeric_less_than', array(':less_than' => $this->less_than));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
61 2
		}
62
63 30
		if ($this->less_than_or_equal_to !== NULL AND ! ($value <= $this->less_than_or_equal_to))
64 30
		{
65 1
			$model->errors()->add($attribute, 'numeric_less_than_or_equal_to', array(':less_than_or_equal_to' => $this->less_than_or_equal_to));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66 1
		}
67
68 30
		if ($this->between !== NULL AND ! ($value >= $this->between[0] AND $value <= $this->between[1]))
69 30
		{
70
			$model->errors()->add($attribute, 'numeric_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1]));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
71
		}
72
73 30
		if ($this->odd === TRUE AND ! ($value % 2 == 0))
74 30
		{
75 2
			$model->errors()->add($attribute, 'numeric_odd', array(':odd' => $this->odd));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
76 2
		}
77
78 30
		if ($this->even === TRUE AND ! ($value % 2 == 1))
79 30
		{
80 2
			$model->errors()->add($attribute, 'numeric_even', array(':even' => $this->even));
0 ignored issues
show
Bug introduced by
The method add cannot be called on $model->errors() (of type array|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
81 2
		}
82 30
	}
83
84 30
	public function html5_validation()
85
	{
86 30
		if ($this->only_integer)
87 30
			return array(
88 6
				'pattern' => '-?\d+',
89
				'title' => 'Integer numbers'
90 6
			);
91
92
		return array(
93 24
			'pattern' => '-?\d+(\.\d+)?',
94
			'title' => 'Numbers with an optional floating point'
95 24
		);
96
	}
97
}
98