Completed
Push — master ( 82694e...d7794c )
by Svetlozar
02:27
created

classes/Kohana/Jam/Field.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Core class that all fields must extend
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 extends Jam_Attribute {
12
13
	/**
14
	 * @var  string  the model's name
15
	 */
16
	public $model;
17
18
	/**
19
	 * @var  string  the column's name in the database
20
	 */
21
	public $column;
22
23
	/**
24
	 * @var  string  a pretty name for the field
25
	 */
26
	public $label;
27
28
	/**
29
	 * @var  string  the field's name in the form
30
	 */
31
	public $name;
32
33
	/**
34
	* @var  boolean  a primary key field.
35
	*/
36
	public $primary = FALSE;
37
38
	/**
39
	* @var  boolean  the column is present in the database table. Default: TRUE
40
	*/
41
	public $in_db = TRUE;
42
43
	/**
44
	* @var  mixed  default value
45
	*/
46
	public $default = NULL;
47
48
	/**
49
	 * @var  boolean  whether or not empty() values should be converted to NULL
50
	 */
51
	public $convert_empty = FALSE;
52
53
	/**
54
	 * @var  mixed  the value to convert empty values to. This is only used if convert_empty is TRUE
55
	 */
56
	public $empty_value = NULL;
57
58
	/**
59
	 * @var  boolean  whether or not NULL values are allowed
60
	 */
61
	public $allow_null = TRUE;
62
63
	/**
64
	* @var  array  filters are called whenever data is set on the field
65
	*/
66
	public $filters = array();
67
68
	/**
69
	 * Sets all options.
70
	 *
71
	 * @param  array  $options
72
	 */
73 49
	public function __construct($options = array())
74
	{
75
		// Assume it's the column name
76 49
		if (is_string($options))
77 49
		{
78
			$this->column = $options;
79
		}
80 49
		elseif (is_array($options))
81
		{
82
			// Just throw them into the class as public variables
83 49
			foreach ($options as $name => $value)
84
			{
85 43
				$this->$name = $value;
86 49
			}
87 49
		}
88 1
		elseif ($options !== NULL)
89
		{
90
			throw new Kohana_Exception("Jam_Field options must be either string or an array of options");
91
		}
92
93
		// See if we need to allow_null values because of convert_empty
94 49
		if ($this->convert_empty AND $this->empty_value === NULL)
95 49
		{
96 42
			$this->allow_null = TRUE;
97 42
		}
98
99
		// Default value is going to be NULL if null is true
100
		// to mimic the SQL defaults.
101 49
		if ( ! array_key_exists('default', (array) $options) AND $this->allow_null)
102 49
		{
103 27
			$this->default = NULL;
104 27
		}
105
106
		// Default the empty value to NULL when allow_null is TRUE, but be careful not
107
		// to override a programmer-configured empty_value.
108 49
		if ( ! empty($options['allow_null']) AND ! array_key_exists('empty_value', (array) $options))
109 49
		{
110 38
			$this->empty_value = NULL;
111 38
		}
112
113 49
		if ( ! empty($options['filters']))
114 49
		{
115
			$this->filters = $options['filters'];
116
		}
117 49
	}
118
119
	/**
120
	 * This is called after construction so that fields can finish
121
	 * constructing themselves with a copy of the column it represents.
122
	 *
123
	 * @param   string  $model
124
	 * @param   string  $column
125
	 * @return  void
126
	 **/
127 2
	public function initialize(Jam_Meta $meta, $name)
128
	{
129 2
		parent::initialize($meta, $name);
130
131 2
		if ( ! $this->column)
132 2
		{
133 2
			$this->column = $name;
134 2
		}
135 2
	}
136
137
	/**
138
	 * Sets a particular value processed according
139
	 * to the class's standards.
140
	 *
141
	 * @param   mixed  $value
142
	 * @return  mixed
143
	 **/
144 8
	public function set(Jam_Validated $model, $value, $is_changed)
145
	{
146 8
		list($value, $return) = $this->_default($model, $value);
147
148 8
		return $value;
149
	}
150
151
	/**
152
	 * Returns a particular value processed according
153
	 * to the class's standards.
154
	 *
155
	 * @param   Jam_Model  $model
156
	 * @param   mixed        $value
157
	 * @return  mixed
158
	 **/
159 169
	public function get(Jam_Validated $model, $value, $is_changed)
160
	{
161 169
		return $value;
162
	}
163
164
	/**
165
	 * Called just before saving.
166
	 *
167
	 * If $in_db, it is expected to return a value suitable for insertion
168
	 * into the database.
169
	 *
170
	 * @param   Jam_Model  $model
171
	 * @param   mixed        $value
172
	 * @param   bool         $loaded
173
	 * @return  mixed
174
	 */
175 10
	public function convert(Jam_Validated $model, $value, $is_loaded)
0 ignored issues
show
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
	{
177 10
		return $value;
178
	}
179
180
	/**
181
	 * Shortcut for setting a filter
182
	 * @param  string $filter_name
183
	 * @param  array $values    values, defaults to array(':value')
184
	 * @return Jam_Field            $this
185
	 */
186
	public function filter($filter_name, $values = NULL)
187
	{
188
		$this->filters[] = array($filter_name, $values);
189
		return $this;
190
	}
191
192
	/**
193
	 * Potentially converts the value to NULL or default depending on
194
	 * the fields configuration. An array is returned with the first
195
	 * element being the new value and the second being a boolean
196
	 * as to whether the field should return the value provided or
197
	 * continue processing it.
198
	 *
199
	 * @param   mixed  $value
200
	 * @return  array
201
	 */
202 212
	protected function _default(Jam_Validated $model, $value)
203
	{
204 212
		$return = FALSE;
205
206 212
		$value = $this->run_filters($model, $value);
207
208
		// Convert empty values to NULL, if needed
209 212
		if ($this->convert_empty AND empty($value))
210 212
		{
211 16
			$value  = $this->empty_value;
212 16
			$return = TRUE;
213 16
		}
214
215
		// Allow NULL values to pass through untouched by the field
216 212
		if ($this->allow_null AND $value === NULL)
217 212
		{
218 23
			$value  = NULL;
219 23
			$return = TRUE;
220 23
		}
221
222
223 212
		return array($value, $return);
224
	}
225
226 3
	public function is_empty($value)
227
	{
228 3
		if ($this->convert_empty AND $this->empty_value !== NULL)
229 3
			return $value === $this->empty_value;
230
231 3
		return ($value === NULL OR $value === $this->default);
232
	}
233
234 212
	public function run_filters(Jam_Validated $model, $value)
235
	{
236 212
		if ( ! empty($this->filters))
237 212
		{
238 3
			foreach ($this->filters as $filter => $arguments)
239
			{
240 3
				if (is_numeric($filter))
241 3
				{
242 2
					$filter = $arguments;
243 2
					$arguments = array();
244 2
				}
245
246 3
				$value = $this->run_filter($model, $value, $filter, $arguments);
247 3
			}
248 3
		}
249 212
		return $value;
250
	}
251
252 3
	public function run_filter(Jam_Validated $model, $value, $filter, array $arguments = array())
253
	{
254
		$bound = array(
255 3
			':model' => $model,
256 3
			':field' => $this->name,
257 3
			':value' => $value,
258 3
		);
259
260 3
		$arguments = $arguments ? $arguments : array(':value');
261
262 3
		foreach ($arguments as & $argument)
263
		{
264 3
			if (is_string($argument) AND array_key_exists($argument, $bound))
265 3
			{
266
				// Replace with bound value
267 3
				$argument = $bound[$argument];
268 3
			}
269 3
		}
270
271 3
		$value = call_user_func_array($filter, $arguments);
272
273
274 3
		return $value;
275
	}
276
} // End Kohana_Jam_Field
277