1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use OpenBuildings\Monetary\Monetary; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package Openbuildings\Jam |
7
|
|
|
* @author Ivan Kerin <[email protected]> |
8
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
9
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
10
|
|
|
*/ |
11
|
|
|
class Kohana_Jam_Field_Price extends Jam_Field_String { |
12
|
|
|
|
13
|
|
|
public $default = 0; |
14
|
|
|
|
15
|
|
|
public $allow_null = TRUE; |
16
|
|
|
|
17
|
|
|
public $convert_empty = TRUE; |
18
|
|
|
|
19
|
|
|
public $default_currency = 'GBP'; |
20
|
|
|
|
21
|
|
|
public $default_display_currency = 'GBP'; |
22
|
|
|
|
23
|
|
|
public $default_ceil_on_convert = FALSE; |
24
|
|
|
|
25
|
|
|
public static $autoload_from_model = array('currency', 'display_currency', 'monetary', 'ceil_on_convert'); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Casts to a string, preserving NULLs along the way. |
29
|
|
|
* |
30
|
|
|
* @param mixed $value |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
2 |
|
public function set(Jam_Validated $model, $value, $is_changed) |
34
|
|
|
{ |
35
|
2 |
|
list($value, $return) = $this->_default($model, $value); |
|
|
|
|
36
|
|
|
|
37
|
2 |
|
return $value; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Preserve nulls and 0 / 0.0 values |
42
|
|
|
* @param Jam_Validated $model |
43
|
|
|
* @param mixed $value |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
2 |
|
protected function _default(Jam_Validated $model, $value) |
47
|
|
|
{ |
48
|
2 |
|
$return = FALSE; |
49
|
|
|
|
50
|
2 |
|
$value = $this->run_filters($model, $value); |
51
|
2 |
|
if (is_string($value)) { |
52
|
|
|
$value = (float) $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Convert empty values to NULL, if needed |
56
|
2 |
|
if ($this->convert_empty AND empty($value) AND $value !== 0 AND $value !== 0.0) |
57
|
2 |
|
{ |
58
|
1 |
|
$value = $this->empty_value; |
59
|
1 |
|
$return = TRUE; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
// Allow NULL values to pass through untouched by the field |
63
|
2 |
|
if ($this->allow_null AND $value === NULL) |
64
|
2 |
|
{ |
65
|
1 |
|
$value = NULL; |
66
|
1 |
|
$return = TRUE; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
2 |
|
return array($value, $return); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function monetary() |
73
|
|
|
{ |
74
|
2 |
|
return Monetary::instance(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* convert to Jam_Price if not NULL |
79
|
|
|
* @param Jam_Validated $model |
80
|
|
|
* @param mixed $value |
81
|
|
|
* @param boolean $is_loaded |
82
|
|
|
* @return Jam_Price |
83
|
|
|
*/ |
84
|
2 |
|
public function get(Jam_Validated $model, $value, $is_loaded) |
85
|
|
|
{ |
86
|
2 |
|
if ( ! ($value instanceof Jam_Price) AND $value !== NULL) |
87
|
2 |
|
{ |
88
|
2 |
|
$value = new Jam_Price($value, $this->default_currency, $this->monetary(), $this->default_display_currency, $this->default_ceil_on_convert); |
89
|
|
|
|
90
|
2 |
|
foreach (static::$autoload_from_model as $autoload_method) |
91
|
|
|
{ |
92
|
2 |
|
if (method_exists($model, $autoload_method)) |
93
|
2 |
|
{ |
94
|
1 |
|
$value->$autoload_method($model->$autoload_method()); |
95
|
1 |
|
} |
96
|
2 |
|
} |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
return $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.