Completed
Push — master ( 84afd6...650043 )
by Evstati
14s
created

Kohana_Jam_Field_Boolean   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 59
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 11 2
A __construct() 0 11 2
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Handles boolean values
4
 *
5
 * No special processing is added for this field other
6
 * than a validation rule that ensures the email address is valid.
7
 *
8
 * @package    Jam
9
 * @category   Fields
10
 * @author     Jonathan Geiger
11
 * @copyright  (c) 2010-2011 Jonathan Geiger
12
 * @license    http://www.opensource.org/licenses/isc-license.txt
13
 */
14
abstract class Kohana_Jam_Field_Boolean extends Jam_Field {
15
16
	/**
17
	 * @var  mixed  how TRUE is represented in the database
18
	 */
19
	public $true = 1;
20
21
	/**
22
	 * @var mixed  how FALSE is represented in the database
23
	 */
24
	public $false = 0;
25
26
	/**
27
	 * @var  boolean  null values are not allowed
28
	 */
29
	public $allow_null = FALSE;
30
31
	/**
32
	 * @var  boolean  default value is FALSE, since NULL isn't allowed
33
	 */
34
	public $default = FALSE;
35
36
	/**
37
	 * Ensures convert_empty is not set on the field, as it prevents FALSE
38
	 * from ever being set on the field.
39
	 *
40
	 * @param   array  $options
41
	 */
42 6
	public function __construct($options = array())
43
	{
44 6
		parent::__construct($options);
45
46
		// Ensure convert_empty is FALSE
47 6
		if ($this->convert_empty)
48
		{
49 1
			throw new Kohana_Exception(':class cannot have convert_empty set to TRUE', array(
50 1
				':class' => get_class($this)));
51
		}
52 5
	}
53
54
	/**
55
	 * Validates a boolean out of the value with filter_var.
56
	 *
57
	 * @param   mixed  $value
58
	 * @return  void
59
	 */
60 27
	public function set(Jam_Validated $model, $value, $is_changed)
61
	{
62 27
		list($value, $return) = $this->_default($model, $value);
63
64 27
		if ( ! $return)
65
		{
66 26
			$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
67
		}
68
69 27
		return $value;
70
	}
71
72
} // End Kohana_Jam_Field_Boolean
73