1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
/** |
3
|
|
|
* Jam Meta |
4
|
|
|
* |
5
|
|
|
* Jam_Meta objects act as a registry of information about a particular model. |
6
|
|
|
* |
7
|
|
|
* @package Jam |
8
|
|
|
* @category Meta |
9
|
|
|
* @author Ivan Kerin |
10
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
11
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
12
|
|
|
*/ |
13
|
|
|
abstract class Kohana_Jam_Meta { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var boolean If this is FALSE, properties can still be set on the meta object |
17
|
|
|
*/ |
18
|
|
|
protected $_initialized = FALSE; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string The model this meta object belongs to |
22
|
|
|
*/ |
23
|
|
|
protected $_model = NULL; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string The database key to use for connection |
27
|
|
|
*/ |
28
|
|
|
protected $_db; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string The table this model represents, defaults to the model name pluralized |
32
|
|
|
*/ |
33
|
|
|
protected $_table = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string The primary key, defaults to the first Field_Primary found. |
37
|
|
|
* This can be referenced in query building as :primary_key |
38
|
|
|
*/ |
39
|
|
|
protected $_primary_key = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string The title key. This can be referenced in query building as :name_key |
43
|
|
|
*/ |
44
|
|
|
protected $_name_key = 'name'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string The foreign key for use in other tables. This can be referenced in query building as :foreign_key |
48
|
|
|
*/ |
49
|
|
|
protected $_foreign_key = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string The method needed to get the item |
53
|
|
|
*/ |
54
|
|
|
protected $_unique_key = ''; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string The polymorphic key for the model tree. |
58
|
|
|
*/ |
59
|
|
|
protected $_polymorphic_key = NULL; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array An array of this model's children |
63
|
|
|
*/ |
64
|
|
|
protected $_children = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array An array of ordering options for SELECTs |
68
|
|
|
*/ |
69
|
|
|
protected $_sorting = array(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array A map to the models's fields and how to process each column. |
73
|
|
|
*/ |
74
|
|
|
protected $_fields = array(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array A map to the models's associations and how to process each column. |
78
|
|
|
*/ |
79
|
|
|
protected $_associations = array(); |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The message filename used for validation errors. |
84
|
|
|
* Defaults to Jam_Meta::$_model |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
protected $_errors_filename = NULL; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var array Default data for each field |
91
|
|
|
*/ |
92
|
|
|
protected $_defaults = array(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var Jam_Event Events attached to this model |
96
|
|
|
*/ |
97
|
|
|
protected $_events = array(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var array Behaviors attached to this model |
101
|
|
|
*/ |
102
|
|
|
protected $_behaviors = array(); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var string The parent model of this model |
106
|
|
|
*/ |
107
|
|
|
protected $_parent = NULL; |
108
|
|
|
|
109
|
|
|
protected $_validators = array(); |
110
|
|
|
|
111
|
|
|
protected $_with_options; |
112
|
|
|
|
113
|
|
|
protected $_collection = NULL; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The most basic initialization possible |
117
|
|
|
* @param string $model Model name |
118
|
|
|
*/ |
119
|
162 |
|
public function __construct($model) |
120
|
|
|
{ |
121
|
162 |
|
$this->_model = $model; |
122
|
|
|
|
123
|
|
|
// Set up event system |
124
|
162 |
|
$this->_events = new Jam_Event($model); |
125
|
162 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* This is called after initialization to |
129
|
|
|
* finalize any changes to the meta object. |
130
|
|
|
* |
131
|
|
|
* @param string $model |
132
|
|
|
*/ |
133
|
7 |
|
public function finalize($model) |
134
|
|
|
{ |
135
|
7 |
|
if ($this->_initialized) |
136
|
|
|
return; |
137
|
|
|
|
138
|
|
|
// Set the name of a possible behavior class |
139
|
7 |
|
$behavior_class = Jam::behavior_prefix().Jam::capitalize_class_name($model); |
140
|
|
|
|
141
|
|
|
// See if we have a special behavior class to use |
142
|
7 |
|
if (class_exists($behavior_class)) |
143
|
|
|
{ |
144
|
|
|
// Load behavior |
145
|
|
|
$behavior = new $behavior_class; |
146
|
|
|
|
147
|
|
|
if ( ! in_array($behavior, $this->_behaviors)) |
148
|
|
|
{ |
149
|
|
|
// Add to behaviors |
150
|
|
|
$this->_behaviors[] = $behavior; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
7 |
|
foreach ($this->_behaviors as $name => $behavior) |
155
|
|
|
{ |
156
|
6 |
|
if ( ! $behavior instanceof Jam_Behavior) |
157
|
|
|
throw new Kohana_Exception('Behavior at index [ :key ] is not an instance of Jam_Behavior, :type found.', array( |
158
|
|
|
':type' => is_object($behavior) ? ('instance of '.get_class($behavior)) : gettype($behavior), |
159
|
|
|
':key' => $name, |
160
|
|
|
)); |
161
|
|
|
|
162
|
|
|
// Initialize behavior |
163
|
6 |
|
$behavior->initialize($this, $name); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Allow modification of this meta object by the behaviors |
167
|
7 |
|
$this->_events->trigger('meta.before_finalize', $this); |
168
|
|
|
|
169
|
|
|
// Ensure certain fields are not overridden |
170
|
7 |
|
$this->_model = $model; |
171
|
7 |
|
$this->_defaults = array(); |
172
|
|
|
|
173
|
7 |
|
if ( ! $this->_errors_filename) |
174
|
|
|
{ |
175
|
|
|
// Default errors filename to the model's name |
176
|
7 |
|
$this->_errors_filename = 'validators/'.$this->_model; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Table should be a sensible default |
180
|
7 |
|
if (empty($this->_table)) |
181
|
|
|
{ |
182
|
6 |
|
$this->_table = Inflector::plural($model); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// Can we set a sensible foreign key? |
186
|
7 |
|
if (empty($this->_foreign_key)) |
187
|
|
|
{ |
188
|
6 |
|
$this->_foreign_key = $model.'_id'; |
189
|
|
|
} |
190
|
|
|
|
191
|
7 |
|
if (empty($this->_unique_key) AND method_exists(Jam::class_name($this->_model), 'unique_key')) |
192
|
|
|
{ |
193
|
|
|
$this->_unique_key = Jam::class_name($this->_model).'::unique_key'; |
194
|
|
|
} |
195
|
|
|
|
196
|
7 |
|
foreach ($this->_fields as $column => $field) |
197
|
|
|
{ |
198
|
|
|
// Ensure a default primary key is set |
199
|
7 |
|
if ($field instanceof Jam_Field AND $field->primary AND empty($this->_primary_key)) |
200
|
|
|
{ |
201
|
6 |
|
$this->_primary_key = $column; |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Ensure a default plymorphic key is set |
205
|
7 |
|
if ($field instanceof Jam_Field_Polymorphic AND empty($this->_polymorphic_key)) |
206
|
|
|
{ |
207
|
7 |
|
$this->_polymorphic_key = $column; |
|
|
|
|
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
7 |
|
foreach ($this->_associations as $column => & $association) |
212
|
|
|
{ |
213
|
6 |
|
$association->initialize($this, $column); |
214
|
|
|
} |
215
|
|
|
|
216
|
7 |
|
foreach ($this->_fields as $column => & $field) |
217
|
|
|
{ |
218
|
7 |
|
$field->initialize($this, $column); |
219
|
7 |
|
$this->_defaults[$column] = $field->default; |
220
|
|
|
} |
221
|
|
|
|
222
|
7 |
|
if ( ! $this->_collection AND ($class = Jam::collection_prefix().Jam::capitalize_class_name($this->_model)) AND class_exists($class)) |
223
|
|
|
{ |
224
|
5 |
|
$this->_collection = $class; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// Meta object is initialized and no longer writable |
228
|
7 |
|
$this->_initialized = TRUE; |
229
|
|
|
|
230
|
|
|
// Final meta callback |
231
|
7 |
|
$this->_events->trigger('meta.after_finalize', $this); |
232
|
7 |
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns a string representation of the meta object. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function __toString() |
240
|
|
|
{ |
241
|
|
|
return (string) get_class($this).': '.$this->_model; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns whether or not the meta object has finished initialization |
246
|
|
|
* |
247
|
|
|
* @return boolean |
248
|
|
|
*/ |
249
|
3 |
|
public function initialized() |
250
|
|
|
{ |
251
|
3 |
|
return $this->_initialized; |
252
|
|
|
} |
253
|
|
|
|
254
|
79 |
|
public function collection($value = NULL) |
255
|
|
|
{ |
256
|
79 |
|
if (func_num_args() !== 0) |
257
|
|
|
{ |
258
|
|
|
return $this->set('collection', $value); |
259
|
|
|
} |
260
|
79 |
|
return $this->_collection; |
261
|
|
|
} |
262
|
|
|
|
263
|
6 |
|
public function validator($field, $options) |
|
|
|
|
264
|
|
|
{ |
265
|
6 |
|
$fields = func_get_args(); |
266
|
6 |
|
$options = (array) array_pop($fields); |
267
|
|
|
|
268
|
6 |
|
if ($this->_with_options) |
269
|
|
|
{ |
270
|
1 |
|
$options = Arr::merge($options, $this->_with_options); |
271
|
|
|
} |
272
|
|
|
|
273
|
6 |
|
$this->_validators[] = new Jam_Validator($fields, $options); |
274
|
|
|
|
275
|
6 |
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
1 |
|
public function with_options($options) |
279
|
|
|
{ |
280
|
1 |
|
$this->_with_options = $options; |
281
|
1 |
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function end() |
285
|
|
|
{ |
286
|
|
|
$this->_with_options = NULL; |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
11 |
|
public function validators() |
291
|
|
|
{ |
292
|
11 |
|
return $this->_validators; |
293
|
|
|
} |
294
|
|
|
|
295
|
19 |
|
public function execute_validators(Jam_Validated $model, $force = FALSE) |
296
|
|
|
{ |
297
|
19 |
|
foreach ($this->_validators as $validator) |
298
|
|
|
{ |
299
|
13 |
|
$validator->validate_model($model, $force); |
300
|
|
|
} |
301
|
|
|
|
302
|
19 |
|
$model->validate(); |
303
|
|
|
|
304
|
19 |
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Allows setting a variable only when not initialized. |
309
|
|
|
* |
310
|
|
|
* @param string $key |
311
|
|
|
* @param mixed $value |
312
|
|
|
* @return $this |
313
|
|
|
*/ |
314
|
7 |
|
protected function set($key, $value) |
315
|
|
|
{ |
316
|
7 |
|
if ($this->_initialized) |
317
|
|
|
{ |
318
|
|
|
throw new Kohana_Exception(':class already initialized, cannot set :key to :value', array( |
319
|
|
|
':class' => Jam::class_name($this->_model), |
320
|
|
|
':key' => $key, |
321
|
|
|
':value' => $value, |
322
|
|
|
)); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
// Set key's value |
326
|
7 |
|
$this->{'_'.$key} = $value; |
327
|
|
|
|
328
|
7 |
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Allows appending an array to a variable only when not initialized. |
333
|
|
|
* |
334
|
|
|
* @param string $key |
335
|
|
|
* @param mixed $value |
336
|
|
|
* @return $this |
337
|
|
|
*/ |
338
|
6 |
|
protected function set_append($key, $value) |
339
|
|
|
{ |
340
|
6 |
|
if ($this->_initialized) |
341
|
|
|
{ |
342
|
|
|
// Throw exception |
343
|
|
|
throw new Kohana_Exception(':class already initialized, cannot append to :key', array( |
344
|
|
|
':class' => Jam::class_name($this->_model), |
345
|
|
|
':key' => $key, |
346
|
|
|
)); |
347
|
|
|
} |
348
|
|
|
|
349
|
6 |
|
if (is_array($value)) |
350
|
|
|
{ |
351
|
|
|
// Set key's value |
352
|
6 |
|
$this->{'_'.$key} = array_merge($this->{'_'.$key}, $value); |
353
|
|
|
} |
354
|
|
|
|
355
|
6 |
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Gets or sets the db group |
360
|
|
|
* |
361
|
|
|
* @param string $value |
362
|
|
|
* @return $this|string |
363
|
|
|
*/ |
364
|
161 |
|
public function db($value = NULL) |
365
|
|
|
{ |
366
|
161 |
|
if (func_num_args() !== 0) |
367
|
|
|
{ |
368
|
7 |
|
return $this->set('db', $value); |
369
|
|
|
} |
370
|
|
|
|
371
|
155 |
|
return $this->_db ? $this->_db : Database::$default; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Returns the model name this object is attached to |
376
|
|
|
* |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
267 |
|
public function model() |
380
|
|
|
{ |
381
|
267 |
|
return $this->_model; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Gets or sets the table |
386
|
|
|
* |
387
|
|
|
* @param string $value |
388
|
|
|
* @return $this|string |
389
|
|
|
*/ |
390
|
167 |
|
public function table($value = NULL) |
391
|
|
|
{ |
392
|
167 |
|
if (func_num_args() !== 0) |
393
|
|
|
{ |
394
|
6 |
|
return $this->set('table', $value); |
395
|
|
|
} |
396
|
|
|
|
397
|
167 |
|
return $this->_table; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Getter / setter for individual fields. |
402
|
|
|
* |
403
|
|
|
* @param string $name name of the field |
404
|
|
|
* @param mixed $field the field alias or object |
405
|
|
|
* @return Jam_Field|$this|null |
406
|
|
|
*/ |
407
|
261 |
|
public function field($name, $field = NULL, $prepend = FALSE) |
408
|
|
|
{ |
409
|
261 |
|
if ($field === NULL) |
410
|
|
|
{ |
411
|
|
|
// Get the association |
412
|
255 |
|
return Arr::get($this->_fields, $name); |
413
|
|
|
} |
414
|
|
|
|
415
|
7 |
|
if ($this->_initialized) |
416
|
|
|
{ |
417
|
|
|
// Cannot set after initialization |
418
|
|
|
throw new Kohana_Exception(':class already initialized, cannot set :field', array( |
419
|
|
|
':class' => Jam::class_name($this->_model), |
420
|
|
|
':field' => $name, |
421
|
|
|
)); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
// Set the field |
425
|
7 |
|
if ($prepend) |
426
|
|
|
{ |
427
|
|
|
$this->_fields = array($name => $field) + $this->_fields; |
428
|
|
|
} |
429
|
|
|
else |
430
|
|
|
{ |
431
|
7 |
|
$this->_fields[$name] = $field; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
// Return Jam_Meta |
435
|
7 |
|
return $this; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* The same as field method, but throws an exception if association does not exist |
440
|
|
|
* |
441
|
|
|
* @param string $name name of the field |
442
|
|
|
* @param mixed $field the field alias or object |
443
|
|
|
* @return Jam_Field|$this|null |
444
|
|
|
*/ |
445
|
|
|
public function field_insist($name, $field = NULL) |
446
|
|
|
{ |
447
|
|
|
if ( ! isset($this->_fields[$name])) |
448
|
|
|
throw new Kohana_Exception('The field :name for this model :model does not exist', array(':name' => $name, ':model' => $this->_model)); |
449
|
|
|
|
450
|
|
|
return $this->field($name, $field); |
451
|
|
|
} |
452
|
|
|
|
453
|
1 |
|
public function attribute($name) |
454
|
|
|
{ |
455
|
1 |
|
return Arr::get($this->_fields, $name, Arr::get($this->_associations, $name)); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
public function attribute_insist($name) |
459
|
|
|
{ |
460
|
|
|
$attribute = $this->attribute($name); |
461
|
|
|
|
462
|
|
|
if ( ! $attribute) |
463
|
|
|
throw new Kohana_Exception('The attrubute :name for this model :model does not exist', array(':name' => $name, ':model' => $this->_model)); |
464
|
|
|
|
465
|
|
|
return $attribute; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Gets and sets the fields for this object. |
471
|
|
|
* |
472
|
|
|
* Calling this multiple times will overwrite fields. |
473
|
|
|
* |
474
|
|
|
* @param array|null $fields |
475
|
|
|
* @return Jam_Field[]|$this |
476
|
|
|
*/ |
477
|
205 |
|
public function fields(array $fields = NULL) |
478
|
|
|
{ |
479
|
205 |
|
if ($fields === NULL) |
480
|
|
|
{ |
481
|
|
|
// Return the fields |
482
|
199 |
|
return $this->_fields; |
483
|
|
|
} |
484
|
|
|
|
485
|
7 |
|
foreach ($fields as $name => $field) |
486
|
|
|
{ |
487
|
|
|
// Set the field |
488
|
7 |
|
$this->field($name, $field); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
// Return Jam_Meta |
492
|
7 |
|
return $this; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Getter / setter for individual associations. |
497
|
|
|
* |
498
|
|
|
* @param string $name name of the association |
499
|
|
|
* @param mixed $association the association alias or object |
500
|
|
|
* @return Jam_Association|$this|null |
501
|
|
|
*/ |
502
|
754 |
|
public function association($name, $association = NULL, $prepend = FALSE) |
503
|
|
|
{ |
504
|
754 |
|
if ($association === NULL) |
505
|
|
|
{ |
506
|
|
|
// Get the association |
507
|
754 |
|
return Arr::get($this->_associations, $name); |
508
|
|
|
} |
509
|
|
|
|
510
|
6 |
|
if ($this->_initialized) |
511
|
|
|
{ |
512
|
|
|
// Cannot set after initialization |
513
|
|
|
throw new Kohana_Exception(':class already initialized, cannot set :association', array( |
514
|
|
|
':class' => Jam::class_name($this->_model), |
515
|
|
|
':association' => $name, |
516
|
|
|
)); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
// Set the association |
520
|
6 |
|
if ($prepend) |
521
|
|
|
{ |
522
|
|
|
$this->_associations = array($name => $association) + $this->_associations; |
523
|
|
|
} |
524
|
|
|
else |
525
|
|
|
{ |
526
|
6 |
|
$this->_associations[$name] = $association; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
// Return Jam_Meta |
530
|
6 |
|
return $this; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* The same as assocation method, but throws an exception if association does not exist |
535
|
|
|
* @param string $name name of the association |
536
|
|
|
* @param mixed $association the association alias or object |
537
|
|
|
* @return Jam_Association|$this|null |
538
|
|
|
* @see association |
539
|
|
|
*/ |
540
|
|
|
public function association_insist($name, $association = NULL) |
541
|
|
|
{ |
542
|
|
|
if ( ! isset($this->_associations[$name])) |
543
|
|
|
throw new Kohana_Exception('The association :name for this model :model does not exist', array(':name' => $name, ':model' => $this->_model)); |
544
|
|
|
|
545
|
|
|
return $this->association($name, $association); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Gets and sets the associations for this object. |
551
|
|
|
* |
552
|
|
|
* Calling this multiple times will overwrite associations. |
553
|
|
|
* |
554
|
|
|
* @param array|null $associations |
555
|
|
|
* @return Jam_Association[]|$this |
556
|
|
|
*/ |
557
|
6 |
|
public function associations(array $associations = NULL) |
558
|
|
|
{ |
559
|
6 |
|
if ($associations === NULL) |
560
|
|
|
{ |
561
|
|
|
// Return the associations |
562
|
|
|
return $this->_associations; |
563
|
|
|
} |
564
|
|
|
|
565
|
6 |
|
foreach ($associations as $name => $association) |
566
|
|
|
{ |
567
|
|
|
// Set the associations |
568
|
6 |
|
$this->association($name, $association); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
// Return Jam_Meta |
572
|
6 |
|
return $this; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Returns the defaults for the object. |
577
|
|
|
* |
578
|
|
|
* If $name is specified, then the defaults |
579
|
|
|
* for that field are returned. |
580
|
|
|
* |
581
|
|
|
* @param string|null $name |
582
|
|
|
* @return array|mixed|null |
583
|
|
|
*/ |
584
|
754 |
|
public function defaults($name = NULL) |
585
|
|
|
{ |
586
|
754 |
|
if ($name === NULL) |
587
|
|
|
{ |
588
|
754 |
|
return $this->_defaults; |
589
|
|
|
} |
590
|
|
|
|
591
|
1 |
|
return $this->field($name)->default; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Returns or sets the name of the file used for errors. |
596
|
|
|
* |
597
|
|
|
* @return $this|string |
598
|
|
|
*/ |
599
|
202 |
|
public function errors_filename($value = NULL) |
600
|
|
|
{ |
601
|
202 |
|
if (func_num_args() !== 0) |
602
|
|
|
{ |
603
|
|
|
return $this->set('errors_filename', $value); |
604
|
|
|
} |
605
|
|
|
|
606
|
202 |
|
return $this->_errors_filename; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Gets or sets the behaviors attached to the object. |
611
|
|
|
* |
612
|
|
|
* @param array|null $behaviors |
613
|
|
|
* @return Jam_Behavior[]|$this |
614
|
|
|
*/ |
615
|
6 |
|
public function behaviors(array $behaviors = NULL) |
616
|
|
|
{ |
617
|
6 |
|
if (func_num_args() == 0) |
618
|
|
|
{ |
619
|
1 |
|
return $this->_behaviors; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
// Try to append |
623
|
6 |
|
return $this->set_append('behaviors', $behaviors); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Get / Set individual behaviors. |
629
|
|
|
* |
630
|
|
|
* @param string $name name of the association |
631
|
|
|
* @param mixed $association the association alias or object |
|
|
|
|
632
|
|
|
* @return Jam_Behavior|$this|null |
633
|
|
|
*/ |
634
|
|
|
public function behavior($name, $behavior = NULL, $prepend = FALSE) |
635
|
|
|
{ |
636
|
|
|
if ($behavior === NULL) |
637
|
|
|
{ |
638
|
|
|
// Get the behavior |
639
|
|
|
return Arr::get($this->_behaviors, $name); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
if ($this->_initialized) |
643
|
|
|
{ |
644
|
|
|
// Cannot set after initialization |
645
|
|
|
throw new Kohana_Exception(':class already initialized, cannot set :behavior', array( |
646
|
|
|
':class' => Jam::class_name($this->_model), |
647
|
|
|
':behavior' => $name, |
648
|
|
|
)); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
// Set the behavior |
652
|
|
|
if ($prepend) |
653
|
|
|
{ |
654
|
|
|
$this->_behaviors = array($name => $behavior) + $this->_behaviors; |
655
|
|
|
} |
656
|
|
|
else |
657
|
|
|
{ |
658
|
|
|
$this->_behaviors[$name] = $behavior; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
// Return Jam_Meta |
662
|
|
|
return $this; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* Gets the events attached to the object. |
667
|
|
|
* |
668
|
|
|
* @return Jam_Event |
669
|
|
|
*/ |
670
|
577 |
|
public function events() |
671
|
|
|
{ |
672
|
577 |
|
return $this->_events; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Gets or sets the model's primary key. |
677
|
|
|
* |
678
|
|
|
* @param string $value |
679
|
|
|
* @return mixed |
680
|
|
|
*/ |
681
|
216 |
|
public function primary_key($value = NULL) |
682
|
|
|
{ |
683
|
216 |
|
if (func_num_args() !== 0) |
684
|
|
|
{ |
685
|
1 |
|
return $this->set('primary_key', $value); |
686
|
|
|
} |
687
|
|
|
|
688
|
216 |
|
return $this->_primary_key; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
/** |
692
|
|
|
* Gets or sets the model's name key |
693
|
|
|
* |
694
|
|
|
* @param string $value |
695
|
|
|
* @return string|$this |
696
|
|
|
*/ |
697
|
34 |
|
public function name_key($value = NULL) |
698
|
|
|
{ |
699
|
34 |
|
if (func_num_args() !== 0) |
700
|
|
|
{ |
701
|
6 |
|
return $this->set('name_key', $value); |
702
|
|
|
} |
703
|
|
|
|
704
|
29 |
|
return $this->_name_key; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Gets or sets the model's foreign key |
709
|
|
|
* |
710
|
|
|
* @param string $value |
711
|
|
|
* @return string|$this |
712
|
|
|
*/ |
713
|
1 |
|
public function foreign_key($value = NULL) |
714
|
|
|
{ |
715
|
1 |
|
if (func_num_args() !== 0) |
716
|
|
|
{ |
717
|
1 |
|
return $this->set('foreign_key', $value); |
718
|
|
|
} |
719
|
|
|
|
720
|
1 |
|
return $this->_foreign_key; |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
/** |
724
|
|
|
* Gets the unique key basend on a model method |
725
|
|
|
* |
726
|
|
|
* @param string $value |
727
|
|
|
* @return string |
728
|
|
|
*/ |
729
|
30 |
|
public function unique_key($value = NULL) |
730
|
|
|
{ |
731
|
30 |
|
if (is_callable($value) AND ! $this->initialized()) |
732
|
|
|
{ |
733
|
|
|
return $this->set('unique_key', $value); |
734
|
|
|
} |
735
|
|
|
|
736
|
30 |
|
if ($this->_unique_key) |
737
|
|
|
{ |
738
|
|
|
return call_user_func($this->_unique_key, $value); |
739
|
|
|
} |
740
|
|
|
else |
741
|
|
|
{ |
742
|
30 |
|
return (is_numeric($value) OR $value === NULL) ? $this->primary_key() : $this->name_key(); |
743
|
|
|
} |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* Gets the model's polymorphic key. |
748
|
|
|
* |
749
|
|
|
* @param string $value |
750
|
|
|
* @return string |
751
|
|
|
*/ |
752
|
442 |
|
public function polymorphic_key($value = NULL) |
|
|
|
|
753
|
|
|
{ |
754
|
442 |
|
return $this->_polymorphic_key; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Gets the model's child models |
759
|
|
|
* |
760
|
|
|
* @param array $children |
761
|
|
|
* @return array |
762
|
|
|
*/ |
763
|
|
|
public function children(array $children = NULL) |
764
|
|
|
{ |
765
|
|
|
if (func_num_args() == 0) |
766
|
|
|
{ |
767
|
|
|
return $this->_children; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
// Try to append |
771
|
|
|
return $this->set_append('children', $children); |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* Gets or sets the object's sorting properties |
776
|
|
|
* |
777
|
|
|
* @param array|null $value |
778
|
|
|
* @return array |
779
|
|
|
*/ |
780
|
1 |
|
public function sorting($value = NULL) |
781
|
|
|
{ |
782
|
1 |
|
if (func_num_args() !== 0) |
783
|
|
|
{ |
784
|
1 |
|
return $this->set('sorting', $value); |
785
|
|
|
} |
786
|
|
|
|
787
|
1 |
|
return $this->_sorting; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
} // End Kohana_Jam_Meta |
791
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.