Completed
Push — update-defaults-for-range ( 30ccb8 )
by Philip
07:15
created

Kohana_Jam_Field_Range   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 3
A set() 0 11 3
A convert() 0 7 2
C _default() 0 37 12
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Handles serialized data
4
 *
5
 * When set, the field attempts to unserialize the data into it's
6
 * actual PHP representation. When the model is saved, the value
7
 * is serialized back and saved as a string into the column.
8
 *
9
 * @package    Jam
10
 * @category   Fields
11
 * @author     Jonathan Geiger
12
 * @copyright  (c) 2010-2011 Jonathan Geiger
13
 * @license    http://www.opensource.org/licenses/isc-license.txt
14
 */
15
abstract class Kohana_Jam_Field_Range extends Jam_Field {
16
17
	public $format;
18
19
	public function get(Jam_Validated $model, $value, $is_loaded)
20
	{
21
		if ( ! $value)
22
			return NULL;
23
24
		return ($value instanceof Jam_Range) ? $value : new Jam_Range($value, $this->format);
25
	}
26
27
	public function set(Jam_Validated $model, $value, $is_changed)
28
	{
29
		list($value, $return) = $this->_default($model, $value);
30
31
		if ( ! $return AND ! ($value instanceof Jam_Range))
32
		{
33
			$value = new Jam_Range($value, $this->format);
34
		}
35
36
		return $value;
37
	}
38
39
	public function convert(Jam_Validated $model, $value, $is_loaded)
40
	{
41
		if ($value === NULL)
42
			return NULL;
43
44
		return $value->__toString();
45
	}
46
47
	protected function _default(Jam_Validated $model, $value)
48
	{
49
		$return = FALSE;
50
51
		$value = $this->run_filters($model, $value);
52
53
		$min = NULL;
54
		$max = NULL;
55
56
		if (is_array($value))
57
		{
58
			$min = $value[0];
59
			$max = $value[1];
60
		}
61
62
		if (is_string($value) AND strstr($value, '|'))
63
		{
64
			$min = isset(explode('|', $value)[0]) ? explode('|', $value)[0] : NULL;
65
			$max = isset(explode('|', $value)[1]) ? explode('|', $value)[1] : NULL;
66
		}
67
68
		// Convert empty values to NULL, if needed
69
		if ($this->convert_empty AND (empty($min) OR empty($max)))
70
		{
71
			$value  = $this->empty_value;
72
			$return = TRUE;
73
		}
74
75
		// Allow NULL values to pass through untouched by the field
76
		if ($this->allow_null AND ($min === NULL OR $max === NULL))
77
		{
78
			$value  = NULL;
79
			$return = TRUE;
80
		}
81
82
		return array($value, $return);
83
	}
84
}
85