Completed
Push — master ( 650043...fc1ed5 )
by Evstati
13s
created

is_empty_price()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * Jam Validatior Rule
5
 *
6
 * @package    Jam
7
 * @category   Validation
8
 * @author     Ivan Kerin
9
 * @copyright  (c) 2011-2012 Despark Ltd.
10
 * @license    http://www.opensource.org/licenses/isc-license.txt
11
 */
12
class Kohana_Jam_Validator_Rule_Present extends Jam_Validator_Rule {
13
14
	public $validate_empty = TRUE;
15
16
	public $allow_zero = FALSE;
17
18 19
	public function validate(Jam_Validated $model, $attribute, $value)
19
	{
20 19
		if (Jam_Validator_Rule_Present::is_empty_value($value, $this->allow_zero)
21 15
			OR Jam_Validator_Rule_Present::is_empty_string($value)
22 14
			OR Jam_Validator_Rule_Present::is_empty_countable($value)
23 14
			OR Jam_Validator_Rule_Present::is_empty_upload_file($value)
24 14
			OR Jam_Validator_Rule_Present::is_empty_range($value)
25 19
			OR Jam_Validator_Rule_Present::is_empty_price($value,  $this->allow_zero))
26
		{
27 8
			$model->errors()->add($attribute, 'present');
28
		}
29 19
	}
30
31 19
	public static function is_empty_value($value, $allow_zero = FALSE)
32
	{
33 19
		return $allow_zero ? ( ! is_numeric($value) AND ! $value) : ! $value;
34
	}
35
36 12
	public static function is_empty_price($value, $allow_zero = FALSE)
37
	{
38 12
		if (class_exists('Jam_Price') AND $value instanceof Jam_Price) {
39 3
			return Jam_Validator_Rule_Present::is_empty_value($value->amount(), $allow_zero);
40
		}
41
42 9
		return false;
43
	}
44 14
	public static function is_empty_countable($value)
45
	{
46 14
		return ($value instanceof Countable AND ! count($value));
47
	}
48
49 14
	public static function is_empty_upload_file($value)
50
	{
51 14
		return (($value instanceof Upload_File) AND $value->is_empty());
52
	}
53
54 14
	public static function is_empty_range($value)
55
	{
56 14
		return (($value instanceof Jam_Range)
57 14
			AND ($value->min() === NULL OR $value->min() === '')
58 14
			AND ($value->max() === NULL OR $value->max() === ''));
59
	}
60
61 15
	public static function is_empty_string($value)
62
	{
63 15
		return (is_string($value) AND strlen(trim($value)) === 0);
64
	}
65
66 25
	public function html5_validation()
67
	{
68 25
		return array('required' => TRUE);
69
	}
70
}
71