Completed
Branch BUG/11288/fix-datepicker (d15367)
by
unknown
108:07 queued 94:31
created

EE_Money_Field::ensureNotMoney()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\domain\values\currency\Money;
4
use EventEspresso\core\exceptions\InvalidDataTypeException;
5
use EventEspresso\core\exceptions\InvalidIdentifierException;
6
use EventEspresso\core\exceptions\InvalidInterfaceException;
7
use EventEspresso\core\services\currency\MoneyFactory;
8
use EventEspresso\core\services\loaders\LoaderFactory;
9
10
defined('EVENT_ESPRESSO_VERSION') || exit;
11
12
/**
13
 * EE_Money_Field
14
 * Model field for dealing with money amounts. Originally this accepted and returns float
15
 * values, but now it also deals in Money entities.
16
 */
17
class EE_Money_Field extends EE_Float_Field
18
{
19
20
    /**
21
     * @var $money_factory MoneyFactory
22
     */
23
    protected $money_factory;
24
25
26
    /**
27
     * @param string       $table_column
28
     * @param string       $nicename
29
     * @param bool         $nullable
30
     * @param null         $default_value
31
     * @param MoneyFactory $factory
32
     * @throws InvalidArgumentException
33
     * @throws InvalidInterfaceException
34
     * @throws InvalidDataTypeException
35
     */
36 View Code Duplication
    public function __construct(
37
        $table_column,
38
        $nicename,
39
        $nullable,
40
        $default_value = null,
41
        MoneyFactory $factory = null
42
    ) {
43
        if (! $factory instanceof MoneyFactory) {
44
            $factory = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\currency\MoneyFactory');
45
        }
46
        $this->money_factory = $factory;
47
        parent::__construct($table_column, $nicename, $nullable, $default_value);
48
        $this->setSchemaType('object');
49
    }
50
51
52
    /**
53
     * Formats the value for pretty output, according to $schema.
54
     * If legacy filters are being used, uses EEH_Money::format_currency() to format it and currency data from the database
55
     * (which admins can change), otherwise uses MoneyFormatter which takes currency information from a JSON file
56
     * (which admins CANNOT change).
57
     * Legacy Schemas (use the admin-editable currency data from the database):
58
     *    'localized_float': "3,023.00"
59
     *    'no_currency_code': "$3,023.00"
60
     *    null: "$3,023.00<span>USD</span>"
61
     * New Schemas (use the currency data from a JSON file that we control):
62
     *    MoneyFormatter::RAW: "3023.0000"
63
     *    MoneyFormatter::DECIMAL_ONLY: "3023.00"
64
     *    MoneyFormatter::ADD THOUSANDS/: "3,023.00"
65
     *    MoneyFormatter::ADD_CURRENCY_SIGN: "$3,023.00"
66
     *    MoneyFormatter::ADD_CURRENCY_CODE: "$3,023.00<span>USD</span>"
67
     *
68
     * @param string|Money $value_on_field_to_be_outputted
69
     * @param string       $schema
70
     * @return string
71
     * @throws InvalidIdentifierException
72
     * @throws InvalidArgumentException
73
     * @throws InvalidInterfaceException
74
     * @throws InvalidDataTypeException
75
     * @throws EE_Error
76
     * @throws ReflectionException
77
     */
78
    public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null)
79
    {
80
            $value_on_field_to_be_outputted = $this->ensureNotMoney($value_on_field_to_be_outputted);
81
            $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted);
82
83
            if ($schema === 'localized_float') {
84
                return $pretty_float;
85
            }
86
            $display_code = true;
87
            if ($schema === 'no_currency_code') {
88
                //          echo "schema no currency!";
89
                $display_code = false;
90
            }
91
92
            //we don't use the $pretty_float because format_currency will take care of it.
93
            return EEH_Money::format_currency($value_on_field_to_be_outputted, false, $display_code);
94
95
    }
96
97
98
    /**
99
     * Make sure this value is a money object
100
     *
101
     * @param string|float|int|Money $value
102
     * @return Money
103
     * @throws InvalidIdentifierException
104
     * @throws InvalidArgumentException
105
     * @throws InvalidInterfaceException
106
     * @throws InvalidDataTypeException
107
     * @throws EE_Error
108
     */
109
    private function ensureMoney($value)
110
    {
111
        if (! $value instanceof Money) {
112
            return $this->money_factory->createForSite($value);
113
        }
114
        return $value;
115
    }
116
117
118
119
    /**
120
     * Ensures we're dealing with something that isn't Money
121
     * (for passing off to legacy systems or the parent field)
122
     * @param  string|float|int|Money $value
123
     * @return string|float|int
124
     */
125
    private function ensureNotMoney($value)
126
    {
127
        if( $value instanceof Money) {
128
            return $value->amount();
129
        }
130
        return $value;
131
    }
132
133
134
    /**
135
     * If provided with a string, strips out money-related formatting to turn it into a proper float.
136
     * Rounds the float to the correct number of decimal places for this country's currency.
137
     * Also, interprets periods and commas according to the country's currency settings.
138
     * So if you want to pass in a string that NEEDS to interpret periods as decimal marks,
139
     * type cast it to a float first.
140
     *
141
     * @param string|float|int|Money $value_inputted_for_field_on_model_object
142
     * @return Money
143
     * @throws InvalidInterfaceException
144
     * @throws InvalidIdentifierException
145
     * @throws InvalidDataTypeException
146
     * @throws EE_Error
147
     * @throws InvalidArgumentException
148
     */
149
    public function prepare_for_set($value_inputted_for_field_on_model_object)
150
    {
151
        if ($value_inputted_for_field_on_model_object instanceof Money) {
152
            return $value_inputted_for_field_on_model_object;
153
        }
154
        //now it's a float-style string or number
155
        return $this->ensureMoney(
156
            parent::prepare_for_set($value_inputted_for_field_on_model_object)
157
        );
158
    }
159
160
161
162
    /**
163
     * @param string|float|int|Money $value_of_field_on_model_object
164
     * @return float
165
     * @throws InvalidArgumentException
166
     * @throws InvalidInterfaceException
167
     * @throws InvalidDataTypeException
168
     */
169
    public function prepare_for_get($value_of_field_on_model_object)
170
    {
171
        $value_of_field_on_model_object = $this->ensureNotMoney($value_of_field_on_model_object);
172
        $c = EE_Registry::instance()->CFG->currency;
173
        return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc);
174
    }
175
176
177
    /**
178
     * Takes the incoming float and create a money entity for the model object
179
     *
180
     * @param string|float|int $value_found_in_db_for_model_object
181
     * @return Money
182
     * @throws InvalidIdentifierException
183
     * @throws InvalidArgumentException
184
     * @throws InvalidInterfaceException
185
     * @throws InvalidDataTypeException
186
     * @throws EE_Error
187
     */
188
    public function prepare_for_set_from_db($value_found_in_db_for_model_object)
189
    {
190
        return $this->money_factory->createForSite($value_found_in_db_for_model_object);
191
    }
192
193
194
195
    /**
196
     * Prepares a value for use in the DB
197
     * @param string|float|int|Money $value_of_field_on_model_object
198
     * @return float
199
     */
200
    public function prepare_for_use_in_db($value_of_field_on_model_object)
201
    {
202
        $value_of_field_on_model_object = $this->ensureNotMoney($value_of_field_on_model_object);
203
        return parent::prepare_for_use_in_db($value_of_field_on_model_object);
204
    }
205
206
207
208 View Code Duplication
    public function getSchemaProperties()
209
    {
210
        return array(
211
            'raw' => array(
212
                'description' =>  sprintf(
213
                    __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'),
214
                    $this->get_nicename()
215
                ),
216
                'type' => 'number'
217
            ),
218
            'pretty' => array(
219
                'description' =>  sprintf(
220
                    __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'),
221
                    $this->get_nicename()
222
                ),
223
                'type' => 'string'
224
            )
225
        );
226
    }
227
}
228