Completed
Pull Request — master (#69)
by
unknown
04:14
created

Kohana_Jam_Field_Integer::_default()   C

Complexity

Conditions 9
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9
Metric Value
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 6.412
cc 9
eloc 10
nc 4
nop 2
crap 9
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Handles integer data-types
4
 *
5
 * @package    Jam
6
 * @category   Fields
7
 * @author     Jonathan Geiger
8
 * @copyright  (c) 2010-2011 Jonathan Geiger
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
abstract class Kohana_Jam_Field_Integer extends Jam_Field {
12
13
	/**
14
	 * @var  int  default value is 0, per the SQL standard
15
	 */
16
	public $default = 0;
17
18
	/**
19
	 * Converts the value to an integer.
20
	 *
21
	 * @param   mixed  $value
22
	 * @return  int
23
	 */
24 63
	public function set(Jam_Validated $model, $value, $is_changed)
25
	{
26 63
		list($value, $return) = $this->_default($model, $value);
27
28 63
		if ( ! $return AND ! $value instanceof Database_Expression)
29 63
		{
30 59
			$value = (int) $value;
31 59
		}
32
33 63
		return $value;
34
	}
35
	
36
	/**
37
	 * Like Jam_Field::_default, but it won't convert 0 and 0.0 to empty_value
38
	 *
39
	 * @param  Jam_Validated $model
40
	 * @param  mixed $value
41
	 * @return array 1st element the converted value; 2nd element a boolean
42
	 * indicating if the value should not be processed further
43
	 */
44 63
	protected function _default(Jam_Validated $model, $value)
45
	{
46 63
		$return = FALSE;
47
48 63
		$value = $this->run_filters($model, $value);
49
50
		// Convert empty values to NULL, if needed
51 63
		if ($this->convert_empty AND empty($value) AND $value !== 0 AND $value !== 0.0 AND $value !== "0" AND $value !== "0.0")
52 63
		{
53 9
			$value  = $this->empty_value;
54 9
			$return = TRUE;
55 9
		}
56
57
		// Allow NULL values to pass through untouched by the field
58 63
		if ($this->allow_null AND $value === NULL)
59 63
		{
60 10
			$value  = NULL;
61 10
			$return = TRUE;
62 10
		}
63
64 63
		return array($value, $return);
65
	}
66
67
} // End Kohana_Jam_Field_Integer
68