|
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
|
58 |
|
public function __construct($options = array()) |
|
74
|
|
|
{ |
|
75
|
|
|
// Assume it's the column name |
|
76
|
58 |
|
if (is_string($options)) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->column = $options; |
|
79
|
|
|
} |
|
80
|
58 |
|
elseif (is_array($options)) |
|
81
|
|
|
{ |
|
82
|
|
|
// Just throw them into the class as public variables |
|
83
|
58 |
|
foreach ($options as $name => $value) |
|
84
|
|
|
{ |
|
85
|
58 |
|
$this->$name = $value; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
6 |
|
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
|
58 |
|
if ($this->convert_empty AND $this->empty_value === NULL) |
|
95
|
|
|
{ |
|
96
|
51 |
|
$this->allow_null = TRUE; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Default value is going to be NULL if null is true |
|
100
|
|
|
// to mimic the SQL defaults. |
|
101
|
58 |
|
if ( ! array_key_exists('default', (array) $options) AND $this->allow_null) |
|
102
|
|
|
{ |
|
103
|
32 |
|
$this->default = NULL; |
|
104
|
|
|
} |
|
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
|
58 |
|
if ( ! empty($options['allow_null']) AND ! array_key_exists('empty_value', (array) $options)) |
|
109
|
|
|
{ |
|
110
|
47 |
|
$this->empty_value = NULL; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
58 |
|
if ( ! empty($options['filters'])) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->filters = $options['filters']; |
|
116
|
|
|
} |
|
117
|
58 |
|
} |
|
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
|
7 |
|
public function initialize(Jam_Meta $meta, $name) |
|
128
|
|
|
{ |
|
129
|
7 |
|
parent::initialize($meta, $name); |
|
130
|
|
|
|
|
131
|
7 |
|
if ( ! $this->column) |
|
132
|
|
|
{ |
|
133
|
7 |
|
$this->column = $name; |
|
134
|
|
|
} |
|
135
|
7 |
|
} |
|
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
|
7 |
|
public function set(Jam_Validated $model, $value, $is_changed) |
|
145
|
|
|
{ |
|
146
|
7 |
|
list($value, $return) = $this->_default($model, $value); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
7 |
|
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
|
207 |
|
public function get(Jam_Validated $model, $value, $is_changed) |
|
160
|
|
|
{ |
|
161
|
207 |
|
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
|
18 |
|
public function convert(Jam_Validated $model, $value, $is_loaded) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
18 |
|
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
|
283 |
|
protected function _default(Jam_Validated $model, $value) |
|
203
|
|
|
{ |
|
204
|
283 |
|
$return = FALSE; |
|
205
|
|
|
|
|
206
|
283 |
|
$value = $this->run_filters($model, $value); |
|
207
|
|
|
|
|
208
|
|
|
// Convert empty values to NULL, if needed |
|
209
|
283 |
|
if ($this->convert_empty AND empty($value)) |
|
210
|
|
|
{ |
|
211
|
40 |
|
$value = $this->empty_value; |
|
212
|
40 |
|
$return = TRUE; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
// Allow NULL values to pass through untouched by the field |
|
216
|
283 |
|
if ($this->allow_null AND $value === NULL) |
|
217
|
|
|
{ |
|
218
|
47 |
|
$value = NULL; |
|
219
|
47 |
|
$return = TRUE; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
283 |
|
return array($value, $return); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
11 |
|
public function is_empty($value) |
|
227
|
|
|
{ |
|
228
|
11 |
|
if ($this->convert_empty AND $this->empty_value !== NULL) |
|
229
|
|
|
return $value === $this->empty_value; |
|
230
|
|
|
|
|
231
|
11 |
|
return ($value === NULL OR $value === $this->default); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
283 |
|
public function run_filters(Jam_Validated $model, $value) |
|
235
|
|
|
{ |
|
236
|
283 |
|
if ( ! empty($this->filters)) |
|
237
|
|
|
{ |
|
238
|
3 |
|
foreach ($this->filters as $filter => $arguments) |
|
239
|
|
|
{ |
|
240
|
3 |
|
if (is_numeric($filter)) |
|
241
|
|
|
{ |
|
242
|
2 |
|
$filter = $arguments; |
|
243
|
2 |
|
$arguments = array(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
3 |
|
$value = $this->run_filter($model, $value, $filter, $arguments); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
283 |
|
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
|
|
|
); |
|
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
|
|
|
{ |
|
266
|
|
|
// Replace with bound value |
|
267
|
3 |
|
$argument = $bound[$argument]; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
3 |
|
$value = call_user_func_array($filter, $arguments); |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
3 |
|
return $value; |
|
275
|
|
|
} |
|
276
|
|
|
} // End Kohana_Jam_Field |
|
277
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.