Completed
Pull Request — master (#78)
by Philip
08:54 queued 07:17
created

Kohana_Jam_Field_Range   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 72
ccs 35
cts 42
cp 0.8333
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 39 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 2
	public function get(Jam_Validated $model, $value, $is_loaded)
20
	{
21 2
		if ( ! $value)
22 2
			return NULL;
23
24 2
		return ($value instanceof Jam_Range) ? $value : new Jam_Range($value, $this->format);
25
	}
26
27 2
	public function set(Jam_Validated $model, $value, $is_changed)
28
	{
29 2
		list($value, $return) = $this->_default($model, $value);
30
31 2
		if ( ! $return AND ! ($value instanceof Jam_Range))
32 2
		{
33 1
			$value = new Jam_Range($value, $this->format);
34 1
		}
35
36 2
		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 2
	protected function _default(Jam_Validated $model, $value)
48
	{
49 2
		$return = FALSE;
50
51 2
		$value = $this->run_filters($model, $value);
52
53 2
		$min = NULL;
54 2
		$max = NULL;
55
56 2
		if (is_array($value))
57 2
		{
58 1
			$min = $value[0];
59 1
			$max = $value[1];
60 1
		}
61
62 2
		if (is_string($value) AND strstr($value, '|'))
63 2
		{
64 1
			$values = explode('|', $value);
65
66 1
			$min = isset($values[0]) ? $values[0] : NULL;
67 1
			$max = isset($values[1]) ? $values[1] : NULL;
68 1
		}
69
70
		// Convert empty values to NULL, if needed
71 2
		if ($this->convert_empty AND (empty($min) OR empty($max)))
72 2
		{
73
			$value  = $this->empty_value;
74
			$return = TRUE;
75
		}
76
77
		// Allow NULL values to pass through untouched by the field
78 2
		if ($this->allow_null AND ($min === NULL OR $max === NULL))
79 2
		{
80 1
			$value  = NULL;
81 1
			$return = TRUE;
82 1
		}
83
84 2
		return array($value, $return);
85
	}
86
}
87