Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

PodsField_Currency::input()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 39
Code Lines 21

Duplication

Lines 39
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 16
nop 5
dl 39
loc 39
rs 5.3846
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once( PODS_DIR . 'classes/fields/number.php' );
3
4
/**
5
 * @package Pods\Fields
6
 */
7
class PodsField_Currency extends PodsField_Number {
8
9
	/**
10
	 * Field Type Group
11
	 *
12
	 * @var string
13
	 * @since 2.0
14
	 */
15
	public static $group = 'Number';
16
17
	/**
18
	 * Field Type Identifier
19
	 *
20
	 * @var string
21
	 * @since 2.0
22
	 */
23
	public static $type = 'currency';
24
25
	/**
26
	 * Field Type Label
27
	 *
28
	 * @var string
29
	 * @since 2.0
30
	 */
31
	public static $label = 'Currency';
32
33
	/**
34
	 * Field Type Preparation
35
	 *
36
	 * @var string
37
	 * @since 2.0
38
	 */
39
	public static $prepare = '%d';
40
41
	/**
42
	 * Currency Formats
43
	 *
44
	 * @var array
45
	 * @since 2.0
46
	 */
47
	public static $currencies = array();
48
49
	/**
50
	 * Do things like register/enqueue scripts and stylesheets
51
	 *
52
	 * @since 2.0
53
	 */
54
	public function __construct() {
55
		self::$label = __( 'Currency', 'pods' );
56
		static::data_currencies();
57
	}
58
59
	/**
60
	 * Add options and set defaults to
61
	 *
62
	 * @return array
63
	 *
64
	 * @since 2.0
65
	 */
66
	public function options() {
67
68
		$currency_options = array();
69
		foreach ( static::$currencies as $key => $value ) {
70
			$currency = $value['label'];
71
			if ( $value['label'] != $value['name'] ) {
72
				$currency .= ': ' . $value['name'];
73
			}
74
			$currency .= ' (' . $value['sign'] . ')';
75
			$currency_options[ $key ] = $currency;
76
		}
77
78
		$options = array(
79
			static::$type . '_repeatable' => array(
80
				'label' => __( 'Repeatable Field', 'pods' ),
81
				'default' => 0,
82
				'type' => 'boolean',
83
				'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),
84
				'boolean_yes_label' => '',
85
				'dependency' => true,
86
				'developer_mode' => true
87
			),
88
			static::$type . '_format_type' => array(
89
				'label' => __( 'Input Type', 'pods' ),
90
				'default' => 'number',
91
				'type' => 'pick',
92
				'data' => array(
93
					'number' => __( 'Freeform Number', 'pods' ),
94
					'slider' => __( 'Slider', 'pods' )
95
				),
96
				'dependency' => true
97
			),
98
			static::$type . '_format_sign' => array(
99
				'label' => __( 'Currency Sign', 'pods' ),
100
				'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ),
101
				'type' => 'pick',
102
				//'pick_format_single' => 'autocomplete',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
				'data' => apply_filters( 'pods_form_ui_field_number_currency_options', $currency_options )
104
			),
105
			static::$type . '_format_placement' => array(
106
				'label' => __( 'Currency Placement', 'pods' ),
107
				'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ),
108
				'type' => 'pick',
109
				'data' => array(
110
					'before' => __( 'Before (ex. $100)', 'pods' ),
111
					'after' => __( 'After (ex. 100$)', 'pods' ),
112
					'none' => __( 'None (ex. 100)', 'pods' ),
113
					'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' )
114
				)
115
			),
116
			static::$type . '_format' => array(
117
				'label' => __( 'Format', 'pods' ),
118
				'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ),
119
				'type' => 'pick',
120
				'data' => array(
121
					'i18n' => __( 'Localized Default', 'pods' ),
122
					'9,999.99' => '1,234.00',
123
					'9\'999.99' => '1\'234.00',
124
					'9.999,99' => '1.234,00',
125
					'9 999,99' => '1 234,00',
126
					'9999.99' => '1234.00',
127
					'9999,99' => '1234,00'
128
				)
129
			),
130
			static::$type . '_decimals' => array(
131
				'label' => __( 'Decimals', 'pods' ),
132
				'default' => 2,
133
				'type' => 'number'
134
			),
135
			static::$type . '_decimal_handling' => array(
136
				'label' => __( 'Decimal handling when zero', 'pods' ),
137
				'default' => 'none',
138
				'type' => 'pick',
139
				'data' => array(
140
					'none' => __( 'Default', 'pods' ),
141
					'remove' => __( 'Remove decimals', 'pods' ),
142
					'dash' => __( 'Convert to dash', 'pods' ) . ' (-)',
143
				)
144
			),
145
			static::$type . '_step' => array(
146
				'label' => __( 'Slider Increment (Step)', 'pods' ),
147
				'depends-on' => array( static::$type . '_format_type' => 'slider' ),
148
				'default' => 1,
149
				'type' => 'text'
150
			),
151
			static::$type . '_min' => array(
152
				'label' => __( 'Minimum Number', 'pods' ),
153
				'depends-on' => array( static::$type . '_format_type' => 'slider' ),
154
				'default' => 0,
155
				'type' => 'text'
156
			),
157
			static::$type . '_max' => array(
158
				'label' => __( 'Maximum Number', 'pods' ),
159
				'depends-on' => array( static::$type . '_format_type' => 'slider' ),
160
				'default' => 1000,
161
				'type' => 'text'
162
			),
163
			static::$type . '_max_length' => array(
164
				'label' => __( 'Maximum Length', 'pods' ),
165
				'default' => 12,
166
				'type' => 'number',
167
				'help' => __( 'Set to -1 for no limit', 'pods' )
168
			),
169
			static::$type . '_placeholder' => array(
170
				'label' => __( 'HTML Placeholder', 'pods' ),
171
				'default' => '',
172
				'type' => 'text',
173
				'help' => array(
174
					__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
175
					'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
176
				),
177
			),
178
			/*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179
			static::$type . '_size' => array(
180
				'label' => __( 'Field Size', 'pods' ),
181
				'default' => 'medium',
182
				'type' => 'pick',
183
				'data' => array(
184
					'small' => __( 'Small', 'pods' ),
185
					'medium' => __( 'Medium', 'pods' ),
186
					'large' => __( 'Large', 'pods' )
187
				)
188
			)*/
189
		);
190
191
		return $options;
192
193
	}
194
195
	/**
196
	 * Change the way the value of the field is displayed with Pods::get
197
	 *
198
	 * @param mixed $value
199
	 * @param string $name
200
	 * @param array $options
201
	 * @param array $pod
202
	 * @param int $id
203
	 *
204
	 * @return mixed|null|string
205
	 * @since 2.0
206
	 */
207
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
208
209
		$value = $this->format( $value, $name, $options, $pod, $id );
210
211
		$currency = 'usd';
212
213 View Code Duplication
		if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, -1 ) ] ) ) {
214
			$currency = pods_v( static::$type . '_format_sign', $options );
215
		}
216
217
		$currency_sign = static::$currencies[ $currency ]['sign'];
218
		$currency_label = static::$currencies[ $currency ]['label'];
219
220
		$placement = pods_v( static::$type . '_format_placement', $options, 'before', true );
221
222
		// Currency placement policy
223
		// Single sign currencies: 100$, £100
224
		// Multiple sign currencies: 100 Fr, Kr 100
225
		$currency_gap = '';
226
227
		if ( strlen( $currency_sign ) > 1 && false === strpos( $currency_sign, '&' ) ) {
228
			$currency_gap = ' ';
229
		}
230
231
		if ( 'before' == $placement ) {
232
			$value = $currency_sign . $currency_gap . $value;
233
		}
234
		elseif ( 'after' == $placement ) {
235
			$value .= $currency_gap . $currency_sign;
236
		}
237
		elseif ( 'beforeaftercode' == $placement ) {
238
			$value = $currency_sign . $currency_gap . $value . ' ' . $currency_label;
239
		}
240
241
		return $value;
242
243
	}
244
245
	/**
246
	 * Build regex necessary for JS validation
247
	 *
248
	 * @param mixed $value
249
	 * @param string $name
250
	 * @param array $options
251
	 * @param string $pod
252
	 * @param int $id
253
	 *
254
	 * @return bool|string
255
	 * @since 2.0
256
	 */
257
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
258
259
		$format_args = $this->get_number_format_args( $options );
260
		$thousands   = $format_args['thousands'];
261
		$dot         = $format_args['dot'];
262
263
		$currency = 'usd';
264
265 View Code Duplication
		if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, -1 ) ] ) ) {
266
			$currency = pods_v( static::$type . '_format_sign', $options );
267
		}
268
269
		$currency_sign = static::$currencies[ $currency ]['sign'];
270
271
		return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+';
272
273
	}
274
275
	/**
276
	 * Validate a value before it's saved
277
	 *
278
	 * @param mixed $value
279
	 * @param string $name
280
	 * @param array $options
281
	 * @param array $fields
282
	 * @param array $pod
283
	 * @param int $id
284
	 * @param null $params
285
	 *
286
	 * @return bool|mixed
287
	 * @since 2.0
288
	 */
289
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
290
291
		$format_args = $this->get_number_format_args( $options );
292
		$thousands   = $format_args['thousands'];
293
		$dot         = $format_args['dot'];
294
295
		$currency = 'usd';
296
297 View Code Duplication
		if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, -1 ) ] ) ) {
298
			$currency = pods_v( static::$type . '_format_sign', $options );
299
		}
300
301
		$currency_sign = static::$currencies[ $currency ]['sign'];
302
		$currency_entity = static::$currencies[ $currency ]['entity'];
303
304
		// Remove currency and thousands symbols
305
		$check = str_replace(
306
			array(
307
				$thousands,
308
				$currency_sign,
309
				$currency_entity,
310
				html_entity_decode( $thousands ),
311
				html_entity_decode( $currency_sign ),
312
				html_entity_decode( $currency_entity ),
313
			),
314
			'',
315
			$value
316
		);
317
		// Convert decimal type for numeric type
318
		$check = str_replace( $dot, '.', $check );
319
		$check = trim( $check );
320
321
		$check = preg_replace( '/[0-9\.\-\s]/', '', $check );
322
323
		$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
324
325
		if ( 0 < strlen( $check ) ) {
326
			return sprintf( __( '%s is not numeric', 'pods' ), $label );
327
		}
328
329
		return true;
330
331
	}
332
333
	/**
334
	 * Change the value or perform actions after validation but before saving to the DB
335
	 *
336
	 * @param mixed $value
337
	 * @param int $id
338
	 * @param string $name
339
	 * @param array $options
340
	 * @param array $fields
341
	 * @param array $pod
342
	 * @param object $params
343
	 *
344
	 * @return mixed|string
345
	 * @since 2.0
346
	 */
347
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
348
349
		$format_args = $this->get_number_format_args( $options );
350
		$thousands   = $format_args['thousands'];
351
		$dot         = $format_args['dot'];
352
		$decimals    = $format_args['decimals'];
353
354
		$currency = 'usd';
355
356 View Code Duplication
		if ( isset( static::$currencies[ pods_v( static::$type . '_format_sign', $options, -1 ) ] ) ) {
357
			$currency = pods_v( static::$type . '_format_sign', $options );
358
		}
359
360
		$currency_sign = static::$currencies[ $currency ]['sign'];
361
		$currency_entity = static::$currencies[ $currency ]['entity'];
362
363
		// Convert decimal type for numeric type
364
		$value = str_replace(
365
			array(
366
				$thousands,
367
				$currency_sign,
368
				$currency_entity,
369
				html_entity_decode( $thousands ),
370
				html_entity_decode( $currency_sign ),
371
				html_entity_decode( $currency_entity ),
372
			),
373
			'',
374
			$value
375
		);
376
		// Convert decimal type for numeric type
377
		$value = str_replace( $dot, '.', $value );
378
		$value = trim( $value );
379
380
		$value = preg_replace( '/[^0-9\.\-]/', '', $value );
381
382
		$value = number_format( (float) $value, $decimals, '.', '' );
383
384
		return $value;
385
386
	}
387
388
	/**
389
	 * Reformat a number to the way the value of the field is displayed
390
	 *
391
	 * @param mixed $value
392
	 * @param string $name
393
	 * @param array $options
394
	 * @param array $pod
395
	 * @param int $id
396
	 *
397
	 * @return string
398
	 * @since 2.0
399
	 */
400
	public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
401
402
		if ( null === $value ) {
403
			// Don't enforce a default value here
404
			return null;
405
		}
406
407
		$format_args = $this->get_number_format_args( $options );
408
		$thousands   = $format_args['thousands'];
409
		$dot         = $format_args['dot'];
410
		$decimals    = $format_args['decimals'];
411
412 View Code Duplication
		if ( 'i18n' == pods_v( static::$type . '_format', $options ) ) {
413
			$value = number_format_i18n( (float) $value, $decimals );
414
		}
415
		else {
416
			$value = number_format( (float) $value, $decimals, $dot, $thousands );
417
		}
418
419
		// Additional output handling for decimals
420
		$decimal_handling = pods_v( static::$type . '_decimal_handling', $options, 'none' ) ;
421
		if ( 'none' !== $decimal_handling ) {
422
			$value_parts = explode( $dot, $value );
423
			if ( 'remove' === $decimal_handling ) {
424
				array_pop( $value_parts );
425
			} elseif ( 'dash' === $decimal_handling ) {
426
				array_pop( $value_parts );
427
				$value_parts[] = '-';
428
			}
429
			$value = implode( $dot, $value_parts );
430
		}
431
432
		return $value;
433
434
	}
435
436
	/**
437
	 * Get the currencies and place them in the local property
438
	 * @since  2.6.8
439
	 * @return array
440
	 */
441
	public static function data_currencies() {
442
443
		// If it's already done, do not redo the filter
444
		if ( ! empty( static::$currencies ) ) {
445
			return static::$currencies;
446
		}
447
448
		$default_currencies = array(
449
			'aud' => array(
450
				'label'  => 'AUD',
451
				'name'   => __( 'Australian Dollar', 'pods' ),
452
				'sign'   => '$',
453
				'entity' => '&#36;',
454
			),
455
			'brl' => array(
456
				'label'  => 'BRL',
457
				'name'   => __( 'Brazilian Real', 'pods' ),
458
				'sign'   => 'R$',
459
				'entity' => 'R&#36;',
460
			),
461
			'cad' => array(
462
				'label'  => 'CAD',
463
				'name'   => __( 'Canadian Dollar', 'pods' ),
464
				'sign'   => '$',
465
				'entity' => '&#36;',
466
			),
467
			'chf' => array(
468
				'label'  => 'CHF',
469
				'name'   => __( 'Swiss Franc', 'pods' ),
470
				'sign'   => 'Fr',
471
				'entity' => 'Fr',
472
			),
473
			'cny' => array(
474
				'label'  => 'CNY',
475
				'name'   => __( 'Chinese Yuan', 'pods' ),
476
				'sign'   => '¥',
477
				'entity' => '&yen;',
478
			),
479
			'cny2' => array(
480
				'label'  => 'CNY',
481
				'name'   => __( 'Chinese Yuan', 'pods' ),
482
				'sign'   => '元',
483
				'entity' => '&#20803;',
484
			),
485
			'czk' => array(
486
				'label'  => 'CZK',
487
				'name'   => __( 'Czech Koruna', 'pods' ),
488
				'sign'   => 'Kč',
489
				'entity' => 'K&#x10D;',
490
			),
491
			'dkk' => array(
492
				'label'  => 'DKK',
493
				'name'   => __( 'Danish Krone', 'pods' ),
494
				'sign'   => 'kr.',
495
				'entity' => 'kr.',
496
			),
497
			'euro' => array(
498
				'label'  => 'EUR',
499
				'name'   => __( 'Euro', 'pods' ),
500
				'sign'   => '€',
501
				'entity' => '&euro;',
502
			),
503
			'gbp' => array(
504
				'label'  => 'GBP',
505
				'name'   => __( 'British Pound', 'pods' ),
506
				'sign'   => '£',
507
				'entity' => '&pound;',
508
			),
509
			'hkd' => array(
510
				'label'  => 'HKD',
511
				'name'   => __( 'Hong Kong Dollar', 'pods' ),
512
				'sign'   => '$',
513
				'entity' => '&#36;',
514
			),
515
			'huf' => array(
516
				'label'  => 'HUF',
517
				'name'   => __( 'Hungarian Forint', 'pods' ),
518
				'sign'   => 'Ft',
519
				'entity' => 'Ft',
520
			),
521
			'ils' => array(
522
				'label'  => 'ILS',
523
				'name'   => __( 'Israeli New Sheqel', 'pods' ),
524
				'sign'   => '₪',
525
				'entity' => '&#x20AA;',
526
			),
527
			'jpy' => array(
528
				'label'  => 'JPY',
529
				'name'   => __( 'Japanese Yen', 'pods' ),
530
				'sign'   => '¥',
531
				'entity' => '&yen;',
532
			),
533
			'krw' => array(
534
				'label'  => 'KRW',
535
				'name'   => __( 'Korean Won', 'pods' ),
536
				'sign'   => '₩',
537
				'entity' => '&#8361;',
538
			),
539
			'myr' => array(
540
				'label'  => 'MYR',
541
				'name'   => __( 'Malaysian Ringgit', 'pods' ),
542
				'sign'   => 'MR',
543
				'entity' => 'MR',
544
			),
545
			'mxn' => array(
546
				'label'  => 'MXN',
547
				'name'   => __( 'Mexican Peso', 'pods' ),
548
				'sign'   => '$',
549
				'entity' => '&#36;',
550
			),
551
			'nok' => array(
552
				'label'  => 'NOK',
553
				'name'   => __( 'Norwegian Krone', 'pods' ),
554
				'sign'   => 'kr',
555
				'entity' => 'kr',
556
			),
557
			'nzd' => array(
558
				'label'  => 'NZD',
559
				'name'   => __( 'New Zealand Dollar', 'pods' ),
560
				'sign'   => '$',
561
				'entity' => '&#36;',
562
			),
563
			'php' => array(
564
				'label'  => 'PHP',
565
				'name'   => __( 'Philippine Peso', 'pods' ),
566
				'sign'   => '₱',
567
				'entity' => '&#x20B1;',
568
			),
569
			'pln' => array(
570
				'label'  => 'PLN',
571
				'name'   => __( 'Polish Złoty', 'pods' ),
572
				'sign'   => 'zł',
573
				'entity' => 'z&#x142;',
574
			),
575
			'rub' => array(
576
				'label'  => 'RUB',
577
				'name'   => __( 'Russian Ruble', 'pods' ),
578
				'sign'   => '₽',
579
				'entity' => '&#8381;',
580
			),
581
			'sek' => array(
582
				'label'  => 'SEK',
583
				'name'   => __( 'Swedish Krona', 'pods' ),
584
				'sign'   => 'kr',
585
				'entity' => 'kr',
586
			),
587
			'sgd' => array(
588
				'label'  => 'SGD',
589
				'name'   => __( 'Singapore Dollar', 'pods' ),
590
				'sign'   => '$',
591
				'entity' => '&#36;',
592
			),
593
			'thb' => array(
594
				'label'  => 'THB',
595
				'name'   => __( 'Thai Baht', 'pods' ),
596
				'sign'   => '฿',
597
				'entity' => '&#x0E3F;',
598
			),
599
			'trl' => array(
600
				'label'  => 'TRL',
601
				'name'   => __( 'Turkish Lira', 'pods' ),
602
				'sign'   => '₺',
603
				'entity' => '&#8378;',
604
			),
605
			'twd' => array(
606
				'label'  => 'TWD',
607
				'name'   => __( 'Taiwan New Dollar', 'pods' ),
608
				'sign'   => '$',
609
				'entity' => '&#36;',
610
			),
611
			'usd' => array(
612
				'label'  => 'USD',
613
				'name'   => __( 'US Dollar', 'pods' ),
614
				'sign'   => '$',
615
				'entity' => '&#36;',
616
			),
617
			'vnd' => array(
618
				'label'  => 'VND',
619
				'name'   => __( 'Vietnamese Dong', 'pods' ),
620
				'sign'   => '₫',
621
				'entity' => '&#8363;',
622
			),
623
			'zar' => array(
624
				'label'  => 'ZAR',
625
				'name'   => __( 'South African Rand', 'pods' ),
626
				'sign'   => 'R',
627
				'entity' => 'R',
628
			),
629
			'inr' => array(
630
				'label'  => 'INR',
631
				'name'   => __( 'Indian Rupee', 'pods' ),
632
				'sign'   => '₹',
633
				'entity' => '&#x20B9;',
634
			),
635
		);
636
637
		/**
638
		 * Add custom currencies
639
		 *
640
		 * @param  array  $options {
641
		 *     Required array of arrays.
642
		 *     @type  array {
643
		 *         @type  string  $label   The label (example: USD).
644
		 *         @type  string  $name    The full name (example: US Dollar).
645
		 *         @type  string  $sign    The sign (example: $).
646
		 *         @type  string  $entity  The HTML entity (example: &#36;).
647
		 *     }
648
		 * }
649
		 * @return array
650
		 */
651
		static::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', $default_currencies );
652
653
		// Sort the currencies
654
		ksort( static::$currencies );
655
656
		// Backwards compatibility
657
		foreach ( static::$currencies as $key => $value ) {
658
			if ( is_string( $value ) ) {
659
				static::$currencies[ $key ] = array(
660
					'label'  => strtoupper( $key ),
661
					'name'   => strtoupper( $key ),
662
					'sign'   => $value,
663
					'entity' => $value,
664
				);
665
			} elseif ( is_array( $value ) ) {
666
				// Make sure all required values are set
667
				if ( empty( $value['label'] ) ) {
668
					$value['label'] = $key;
669
				}
670
				if ( empty( $value['name'] ) ) {
671
					$value['name'] = $key;
672
				}
673
				if ( empty( $value['sign'] ) ) {
674
					$value['sign'] = $key;
675
				}
676
				if ( empty( $value['entity'] ) ) {
677
					$value['entity'] = $key;
678
				}
679
			} else {
680
				// Invalid
681
				unset( static::$currencies[ $key ] );
682
			}
683
		}
684
685
		return static::$currencies;
686
	}
687
688
	/**
689
	 * Get the max allowed decimals.
690
	 * Overwrites the default value of Number field. 2 decimals instead of 0.
691
	 *
692
	 * @since 2.7
693
	 * @param array $options
694
	 * @return int
695
	 */
696 View Code Duplication
	public function get_max_decimals( $options ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
697
698
		$length = (int) pods_v( static::$type . '_max_length', $options, 12, true );
699
700
		if ( $length < 1 || 64 < $length ) {
701
			$length = 64;
702
		}
703
704
		$decimals = (int) pods_v( static::$type . '_decimals', $options, 2 );
705
706
		if ( $decimals < 1 ) {
707
			$decimals = 0;
708
		}
709
		elseif ( 30 < $decimals ) {
710
			$decimals = 30;
711
		}
712
713
		if ( $length < $decimals ) {
714
			$decimals = $length;
715
		}
716
717
		return $decimals;
718
	}
719
}
720