Completed
Push — master ( 2f347b...82694e )
by Haralan
12s
created

Kohana_Jam_Field_Serialized   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 26.32%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 93
ccs 10
cts 38
cp 0.2632
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 2
A set() 0 14 4
A convert() 0 9 3
B serialize() 0 17 5
B unserialize() 0 18 5
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_Serialized extends Jam_Field {
16
17
	public static $allowed = array('native', 'json', 'csv', 'query');
18
19
	public $method = 'native';
20
21
	public function initialize(Jam_Meta $meta, $name)
22
	{
23
		parent::initialize($meta, $name);
24
25
		if ( ! in_array($this->method, Jam_Field_Serialized::$allowed))
26
			throw new Kohana_Exception("Unnown serialization method ':method', can use only :allowed", array(':method' => $this->method, ':allowed' => Jam_Field_Serialized::$allowed));
27
	}
28
29
	/**
30
	 * Unserializes data as soon as it comes in.
31
	 *
32
	 * Incoming data that isn't actually serialized will not be harmed.
33
	 *
34
	 * @param   mixed  $value
35
	 * @return  mixed
36
	 */
37 3
	public function set(Jam_Validated $model, $value, $is_changed)
38
	{
39 3
		list($value, $return) = $this->_default($model, $value);
40
41 3
		if ( ! $return)
42
		{
43 3
		 	if (is_string($value) AND ($new_value = $this->unserialize($value)) !== FALSE)
44
			{
45 2
				$value = $new_value;
46
			}
47
		}
48
49 3
		return $value;
50
	}
51
52
	/**
53
	 * Saves the value as a serialized string.
54
	 *
55
	 * @param   Jam_Model  $model
56
	 * @param   mixed        $value
57
	 * @param   boolean      $loaded
0 ignored issues
show
Documentation introduced by
There is no parameter named $loaded. Did you maybe mean $is_loaded?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
58
	 * @return  null|string
59
	 */
60
	public function convert(Jam_Validated $model, $value, $is_loaded)
61
	{
62
		if ($this->allow_null AND $value === NULL)
63
		{
64
			return NULL;
65
		}
66
67
		return $this->serialize($value);
68
	}
69
70
	public function serialize($value)
71
	{
72
		switch ($this->method)
73
		{
74
			case 'native':
75
				return serialize($value);
76
77
			case 'csv':
78
				return join(',', $value);
79
80
			case 'json':
81
				return json_encode($value);
82
83
			case 'query':
84
				return http_build_query($value);
85
		}
86
	}
87
88 2
	public function unserialize($value)
89
	{
90 2
		switch ($this->method)
91
		{
92 2
			case 'native':
93 2
				return unserialize($value);
94
95
			case 'csv':
96
				return explode(',', $value);
97
98
			case 'json':
99
				return json_decode($value, TRUE);
100
101
			case 'query':
102
				parse_str($value, $value);
103
				return $value;
104
		}
105
	}
106
107
} // Kohana_Jam_Field_Serialized
108