Completed
Pull Request — 2.x (#3949)
by Jory
06:10 queued 01:40
created

PodsField_Currency::options()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 118
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 84
nc 3
nop 0
dl 0
loc 118
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_Currency extends PodsField {
7
8
	/**
9
	 * Field Type Group
10
	 *
11
	 * @var string
12
	 * @since 2.0
13
	 */
14
	public static $group = 'Number';
15
16
	/**
17
	 * Field Type Identifier
18
	 *
19
	 * @var string
20
	 * @since 2.0
21
	 */
22
	public static $type = 'currency';
23
24
	/**
25
	 * Field Type Label
26
	 *
27
	 * @var string
28
	 * @since 2.0
29
	 */
30
	public static $label = 'Currency';
31
32
	/**
33
	 * Field Type Preparation
34
	 *
35
	 * @var string
36
	 * @since 2.0
37
	 */
38
	public static $prepare = '%d';
39
40
	/**
41
	 * Currency Formats
42
	 *
43
	 * @var array
44
	 * @since 2.0
45
	 */
46
	public static $currencies = array();
47
48
	/**
49
	 * Do things like register/enqueue scripts and stylesheets
50
	 *
51
	 * @since 2.0
52
	 */
53
	public function __construct() {
54
		self::$label = __( 'Currency', 'pods' );
55
		self::data_currencies();
56
	}
57
58
	/**
59
	 * Add options and set defaults to
60
	 *
61
	 * @return array
62
	 *
63
	 * @since 2.0
64
	 */
65
	public function options() {
66
67
		$currency_options = array();
68
		foreach ( self::$currencies as $key => $value ) {
69
			$currency = $value['label'];
70
			if ( $value['label'] != $value['name'] ) {
71
				$currency .= ': ' . $value['name'];
72
			}
73
			$currency .= ' (' . $value['sign'] . ')';
74
			$currency_options[ $key ] = $currency;
75
		}
76
77
		$options = array(
78
			self::$type . '_repeatable' => array(
79
				'label' => __( 'Repeatable Field', 'pods' ),
80
				'default' => 0,
81
				'type' => 'boolean',
82
				'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' ),
83
				'boolean_yes_label' => '',
84
				'dependency' => true,
85
				'developer_mode' => true
86
			),
87
			self::$type . '_format_type' => array(
88
				'label' => __( 'Input Type', 'pods' ),
89
				'default' => 'number',
90
				'type' => 'pick',
91
				'data' => array(
92
					'number' => __( 'Freeform Number', 'pods' ),
93
					'slider' => __( 'Slider', 'pods' )
94
				),
95
				'dependency' => true
96
			),
97
			self::$type . '_format_sign' => array(
98
				'label' => __( 'Currency Sign', 'pods' ),
99
				'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ),
100
				'type' => 'pick',
101
				'data' => apply_filters( 'pods_form_ui_field_number_currency_options', $currency_options )
102
			),
103
			self::$type . '_format_placement' => array(
104
				'label' => __( 'Currency Placement', 'pods' ),
105
				'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ),
106
				'type' => 'pick',
107
				'data' => array(
108
					'before' => __( 'Before (ex. $100)', 'pods' ),
109
					'after' => __( 'After (ex. 100$)', 'pods' ),
110
					'none' => __( 'None (ex. 100)', 'pods' ),
111
					'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' )
112
				)
113
			),
114
			self::$type . '_format' => array(
115
				'label' => __( 'Format', 'pods' ),
116
				'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ),
117
				'type' => 'pick',
118
				'data' => array(
119
					'i18n' => __( 'Localized Default', 'pods' ),
120
					'9,999.99' => '1,234.00',
121
					'9\'999.99' => '1\'234.00',
122
					'9.999,99' => '1.234,00',
123
					'9 999,99' => '1 234,00',
124
					'9999.99' => '1234.00',
125
					'9999,99' => '1234,00'
126
				)
127
			),
128
			self::$type . '_decimals' => array(
129
				'label' => __( 'Decimals', 'pods' ),
130
				'default' => 2,
131
				'type' => 'number'
132
			),
133
			self::$type . '_decimal_handling' => array(
134
				'label' => __( 'Decimal handling when zero', 'pods' ),
135
				'default' => 'none',
136
				'type' => 'pick',
137
				'data' => array(
138
					'none' => __( 'Default', 'pods' ),
139
					'remove' => __( 'Remove decimals', 'pods' ),
140
					'dash' => __( 'Convert to dash', 'pods' ) . ' (-)',
141
				)
142
			),
143
			self::$type . '_step' => array(
144
				'label' => __( 'Slider Increment (Step)', 'pods' ),
145
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
146
				'default' => 1,
147
				'type' => 'text'
148
			),
149
			self::$type . '_min' => array(
150
				'label' => __( 'Minimum Number', 'pods' ),
151
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
152
				'default' => 0,
153
				'type' => 'text'
154
			),
155
			self::$type . '_max' => array(
156
				'label' => __( 'Maximum Number', 'pods' ),
157
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
158
				'default' => 1000,
159
				'type' => 'text'
160
			),
161
			self::$type . '_max_length' => array(
162
				'label' => __( 'Maximum Length', 'pods' ),
163
				'default' => 12,
164
				'type' => 'number',
165
				'help' => __( 'Set to -1 for no limit', 'pods' )
166
			)
167
			/*,
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...
168
			self::$type . '_size' => array(
169
				'label' => __( 'Field Size', 'pods' ),
170
				'default' => 'medium',
171
				'type' => 'pick',
172
				'data' => array(
173
					'small' => __( 'Small', 'pods' ),
174
					'medium' => __( 'Medium', 'pods' ),
175
					'large' => __( 'Large', 'pods' )
176
				)
177
			)*/
178
		);
179
180
		return $options;
181
182
	}
183
184
	/**
185
	 * Define the current field's schema for DB table storage
186
	 *
187
	 * @param array $options
188
	 *
189
	 * @return array
190
	 * @since 2.0
191
	 */
192 View Code Duplication
	public function schema( $options = null ) {
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...
193
194
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
195
196
		if ( $length < 1 || 64 < $length ) {
197
			$length = 64;
198
		}
199
200
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
201
202
		if ( $decimals < 1 ) {
203
			$decimals = 0;
204
		}
205
		elseif ( 30 < $decimals ) {
206
			$decimals = 30;
207
		}
208
209
		if ( $length < $decimals ) {
210
			$decimals = $length;
211
		}
212
213
		$schema = 'DECIMAL(' . $length . ',' . $decimals . ')';
214
215
		return $schema;
216
217
	}
218
219
	/**
220
	 * Define the current field's preparation for sprintf
221
	 *
222
	 * @param array $options
223
	 *
224
	 * @return array
225
	 * @since 2.0
226
	 */
227 View Code Duplication
	public function prepare( $options = null ) {
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...
228
229
		$format = self::$prepare;
230
231
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
232
233
		if ( $length < 1 || 64 < $length ) {
234
			$length = 64;
235
		}
236
237
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
238
239
		if ( $decimals < 1 ) {
240
			$decimals = 0;
241
		}
242
		elseif ( 30 < $decimals ) {
243
			$decimals = 30;
244
		}
245
246
		if ( $length < $decimals ) {
247
			$decimals = $length;
248
		}
249
250
		if ( 0 < $decimals ) {
251
			$format = '%01.' . $decimals . 'F';
252
		}
253
		else {
254
			$format = '%d';
255
		}
256
257
		return $format;
258
259
	}
260
261
	/**
262
	 * Change the way the value of the field is displayed with Pods::get
263
	 *
264
	 * @param mixed $value
265
	 * @param string $name
266
	 * @param array $options
267
	 * @param array $pod
268
	 * @param int $id
269
	 *
270
	 * @return mixed|null|string
271
	 * @since 2.0
272
	 */
273
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
274
275
		$value = $this->format( $value, $name, $options, $pod, $id );
276
277
		$currency = 'usd';
278
279 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
280
			$currency = pods_v( self::$type . '_format_sign', $options );
281
		}
282
283
		$currency_sign = self::$currencies[ $currency ]['sign'];
284
		$currency_label = self::$currencies[ $currency ]['label'];
285
286
		$placement = pods_v( self::$type . '_format_placement', $options, 'before', true );
287
288
		// Currency placement policy
289
		// Single sign currencies: 100$, £100
290
		// Multiple sign currencies: 100 Fr, Kr 100
291
		$currency_gap = '';
292
293
		if ( strlen( $currency_sign ) > 1 && false === strpos( $currency_sign, '&' ) ) {
294
			$currency_gap = ' ';
295
		}
296
297
		if ( 'before' == $placement ) {
298
			$value = $currency_sign . $currency_gap . $value;
299
		}
300
		elseif ( 'after' == $placement ) {
301
			$value .= $currency_gap . $currency_sign;
302
		}
303
		elseif ( 'beforeaftercode' == $placement ) {
304
			$value = $currency_sign . $currency_gap . $value . ' ' . $currency_label;
305
		}
306
307
		return $value;
308
309
	}
310
311
	/**
312
	 * Customize output of the form field
313
	 *
314
	 * @param string $name
315
	 * @param mixed $value
316
	 * @param array $options
317
	 * @param array $pod
318
	 * @param int $id
319
	 *
320
	 * @since 2.0
321
	 */
322 View Code Duplication
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
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...
323
324
		$options = (array) $options;
325
		$form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
326
327
		if ( is_array( $value ) ) {
328
			$value = implode( '', $value );
329
		}
330
331
		if ( 'slider' == pods_v( self::$type . '_format_type', $options, 'number' ) ) {
332
			$field_type = 'slider';
333
		}
334
		else {
335
			$field_type = 'currency';
336
		}
337
338
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
339
			if ( pods_v( 'read_only', $options, false ) ) {
340
				$options[ 'readonly' ] = true;
341
342
				$field_type = 'text';
343
344
				$value = $this->format( $value, $name, $options, $pod, $id );
345
			}
346
			else {
347
				return;
348
			}
349
		}
350
		elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
351
			$options[ 'readonly' ] = true;
352
353
			$field_type = 'text';
354
355
			$value = $this->format( $value, $name, $options, $pod, $id );
356
		}
357
358
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
359
360
	}
361
362
	/**
363
	 * Build regex necessary for JS validation
364
	 *
365
	 * @param mixed $value
366
	 * @param string $name
367
	 * @param array $options
368
	 * @param string $pod
369
	 * @param int $id
370
	 *
371
	 * @return bool|string
372
	 * @since 2.0
373
	 */
374
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
375
376
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
377
378 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
379
			$thousands = '.';
380
			$dot = ',';
381
		}
382
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
383
			$thousands = ',';
384
			$dot = '.';
385
		}
386
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
387
			$thousands = '\'';
388
			$dot = '.';
389
		}
390
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
391
			$thousands = ' ';
392
			$dot = ',';
393
		}
394
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
395
			$thousands = '';
396
			$dot = '.';
397
		}
398
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
399
			$thousands = '';
400
			$dot = ',';
401
		}
402
		else {
403
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
404
			$dot = $wp_locale->number_format[ 'decimal_point' ];
405
		}
406
407
		$currency = 'usd';
408
409 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
410
			$currency = pods_v( self::$type . '_format_sign', $options );
411
		}
412
413
		$currency_sign = self::$currencies[ $currency ];
414
415
		return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+';
416
417
	}
418
419
	/**
420
	 * Validate a value before it's saved
421
	 *
422
	 * @param mixed $value
423
	 * @param string $name
424
	 * @param array $options
425
	 * @param array $fields
426
	 * @param array $pod
427
	 * @param int $id
428
	 * @param null $params
429
	 *
430
	 * @return bool|mixed|void
431
	 * @since 2.0
432
	 */
433
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
434
435
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
436
437 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
438
			$thousands = '.';
439
			$dot = ',';
440
		}
441
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
442
			$thousands = ',';
443
			$dot = '.';
444
		}
445
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
446
			$thousands = '\'';
447
			$dot = '.';
448
		}
449
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
450
			$thousands = ' ';
451
			$dot = ',';
452
		}
453
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
454
			$thousands = ',';
455
			$dot = '.';
456
		}
457
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
458
			$thousands = '.';
459
			$dot = ',';
460
		}
461
		else {
462
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
463
			$dot = $wp_locale->number_format[ 'decimal_point' ];
464
		}
465
466
		$currency = 'usd';
467
468 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
469
			$currency = pods_v( self::$type . '_format_sign', $options );
470
		}
471
472
		$currency_sign = self::$currencies[ $currency ];
473
474
		$check = str_replace( array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ) ), array( '', '.', '', '' ), $value );
475
		$check = trim( $check );
476
477
		$check = preg_replace( '/[0-9\.\-]/', '', $check );
478
479
		$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
480
481
		if ( 0 < strlen( $check ) ) {
482
			return sprintf( __( '%s is not numeric', 'pods' ), $label );
483
		}
484
485
		return true;
486
487
	}
488
489
	/**
490
	 * Change the value or perform actions after validation but before saving to the DB
491
	 *
492
	 * @param mixed $value
493
	 * @param int $id
494
	 * @param string $name
495
	 * @param array $options
496
	 * @param array $fields
497
	 * @param array $pod
498
	 * @param object $params
499
	 *
500
	 * @return mixed|string
501
	 * @since 2.0
502
	 */
503
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
504
505
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
506
507 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
508
			$thousands = '.';
509
			$dot = ',';
510
		}
511
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
512
			$thousands = ',';
513
			$dot = '.';
514
		}
515
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
516
			$thousands = '\'';
517
			$dot = '.';
518
		}
519
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
520
			$thousands = ' ';
521
			$dot = ',';
522
		}
523
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
524
			$thousands = ',';
525
			$dot = '.';
526
		}
527
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
528
			$thousands = '.';
529
			$dot = ',';
530
		}
531
		else {
532
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
533
			$dot = $wp_locale->number_format[ 'decimal_point' ];
534
		}
535
536
		$currency = 'usd';
537
538 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
539
			$currency = pods_v( self::$type . '_format_sign', $options );
540
		}
541
542
		$currency_sign = self::$currencies[ $currency ];
543
544
		$value = str_replace( array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ) ), array( '', '.', '', '' ), $value );
545
		$value = trim( $value );
546
547
		$value = preg_replace( '/[^0-9\.\-]/', '', $value );
548
549
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
550
551
		if ( $length < 1 || 64 < $length ) {
552
			$length = 64;
553
		}
554
555
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
556
557
		if ( $decimals < 1 ) {
558
			$decimals = 0;
559
		}
560
		elseif ( 30 < $decimals ) {
561
			$decimals = 30;
562
		}
563
564
		if ( $length < $decimals ) {
565
			$decimals = $length;
566
		}
567
568
		$value = number_format( (float) $value, $decimals, '.', '' );
569
570
		return $value;
571
572
	}
573
574
	/**
575
	 * Customize the Pods UI manage table column output
576
	 *
577
	 * @param int $id
578
	 * @param mixed $value
579
	 * @param string $name
580
	 * @param array $options
581
	 * @param array $fields
582
	 * @param array $pod
583
	 *
584
	 * @return mixed|null|string
585
	 * @since 2.0
586
	 */
587
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
588
589
		return $this->display( $value, $name, $options, $pod, $id );
590
591
	}
592
593
	/**
594
	 * Reformat a number to the way the value of the field is displayed
595
	 *
596
	 * @param mixed $value
597
	 * @param string $name
598
	 * @param array $options
599
	 * @param array $pod
600
	 * @param int $id
601
	 *
602
	 * @return string
603
	 * @since 2.0
604
	 */
605
	public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
606
607
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
608
609
		if ( null === $value ) {
610
			// Don't enforce a default value here
611
			return null;
612
		}
613
614 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
615
			$thousands = '.';
616
			$dot = ',';
617
		}
618
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
619
			$thousands = ',';
620
			$dot = '.';
621
		}
622
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
623
			$thousands = '\'';
624
			$dot = '.';
625
		}
626
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
627
			$thousands = ' ';
628
			$dot = ',';
629
		}
630
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
631
			$thousands = '';
632
			$dot = '.';
633
		}
634
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
635
			$thousands = '';
636
			$dot = ',';
637
		}
638
		else {
639
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
640
			$dot = $wp_locale->number_format[ 'decimal_point' ];
641
		}
642
643
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
644
645
		if ( $length < 1 || 64 < $length ) {
646
			$length = 64;
647
		}
648
649
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2 );
650
651
		if ( $decimals < 1 ) {
652
			$decimals = 0;
653
		}
654
		elseif ( 30 < $decimals ) {
655
			$decimals = 30;
656
		}
657
658
		if ( $length < $decimals ) {
659
			$decimals = $length;
660
		}
661
662 View Code Duplication
		if ( 'i18n' == pods_v( self::$type . '_format', $options ) ) {
663
			$value = number_format_i18n( (float) $value, $decimals );
664
		}
665
		else {
666
			$value = number_format( (float) $value, $decimals, $dot, $thousands );
667
		}
668
669
		// Additional output handling for decimals
670
		$decimal_handling = pods_v( self::$type . '_decimal_handling', $options, 'none' ) ;
671
		if ( 'none' !== $decimal_handling ) {
672
			$value_parts = explode( $dot, $value );
673
			if ( 'remove' === $decimal_handling ) {
674
				array_pop( $value_parts );
675
			} elseif ( 'dash' === $decimal_handling ) {
676
				array_pop( $value_parts );
677
				$value_parts[] = '-';
678
			}
679
			$value = implode( $dot, $value_parts );
680
		}
681
682
		return $value;
683
684
	}
685
686
	/**
687
	 * Get the currencies and place them in the local property
688
	 * @since  2.6.8
689
	 * @return array
690
	 */
691
	public static function data_currencies() {
692
693
		// If it's already done, do not redo the filter
694
		if ( ! empty( self::$currencies ) ) {
695
			return self::$currencies;
696
		}
697
698
		$default_currencies = array(
699
			'usd' => array(
700
				'label' => 'USD',
701
				'name'  => 'US Dollar',
702
				'sign'  => '$',
703
			),
704
			'euro' => array(
705
				'label' => 'EUR',
706
				'name'  => 'Euro',
707
				'sign'  => '&euro;',
708
			),
709
			'gbp' => array(
710
				'label' => 'GBP',
711
				'name'  => 'British Pound',
712
				'sign'  => '&pound;',
713
			),
714
			'cad' => array(
715
				'label' => 'CAD',
716
				'name'  => 'Canadian Dollar',
717
				'sign'  => '$',
718
			),
719
			'aud' => array(
720
				'label' => 'AUD',
721
				'name'  => 'Australian Dollar',
722
				'sign'  => '$',
723
			),
724
			'nzd' => array(
725
				'label' => 'NZD',
726
				'name'  => 'New Zealand Dollar',
727
				'sign'  => '$',
728
			),
729
			'rub' => array(
730
				'label' => 'RUB',
731
				'name'  => 'Russian Ruble',
732
				'sign'  => '&#8381;',
733
			),
734
			'chf' => array(
735
				'label' => 'CHF',
736
				'name'  => 'Swiss Franc',
737
				'sign'  => 'Fr.',
738
			),
739
			'dkk' => array(
740
				'label' => 'DKK',
741
				'name'  => 'Danish Krone',
742
				'sign'  => 'kr.',
743
			),
744
			'nok' => array(
745
				'label' => 'NOK',
746
				'name'  => 'Norwegian Krone',
747
				'sign'  => 'kr',
748
			),
749
			'sek' => array(
750
				'label' => 'SEK',
751
				'name'  => 'Swedish Krona',
752
				'sign'  => 'kr',
753
			),
754
			'zar' => array(
755
				'label' => 'ZAR',
756
				'name'  => 'South African Rand',
757
				'sign'  => 'R',
758
			),
759
			'inr' => array(
760
				'label' => 'INR',
761
				'name'  => 'Indian Rupee',
762
				'sign'  => '&#x20B9;',
763
			),
764
			'jpy' => array(
765
				'label' => 'JPY',
766
				'name'  => 'Japanese Yen',
767
				'sign'  => '&yen;',
768
			),
769
			'cny' => array(
770
				'label' => 'CNY',
771
				'name'  => 'Chinese Yuan',
772
				'sign'  => '&yen;',
773
			),
774
			'sgd' => array(
775
				'label' => 'SGD',
776
				'name'  => 'Singapore Dollar',
777
				'sign'  => '$',
778
			),
779
			'krw' => array(
780
				'label' => 'KRW',
781
				'name'  => 'Korean Won',
782
				'sign'  => '&#8361;',
783
			),
784
			'thb' => array(
785
				'label' => 'THB',
786
				'name'  => 'Thai Baht',
787
				'sign'  => '&#x0E3F;',
788
			),
789
			'trl' => array(
790
				'label' => 'TRL',
791
				'name'  => 'Turkish Lira',
792
				'sign'  => '&#8378;',
793
			),
794
			'vnd' => array(
795
				'label' => 'VND',
796
				'name'  => 'Vietnamese Dong',
797
				'sign'  => '&#8363;',
798
			),
799
			'myr' => array(
800
				'label' => 'MYR',
801
				'name'  => 'Malaysian Ringgit',
802
				'sign'  => 'MR',
803
			),
804
		);
805
806
		/**
807
		 * Add custom currencies
808
		 *
809
		 * @param  array  $options {
810
		 *     Required array of arrays.
811
		 *     @type  array {
812
		 *         @type  string  $label  The label (example: USD).
813
		 *         @type  string  $name   The full name (example: US Dollar).
814
		 *         @type  string  $sign   The sign (example: $).
815
		 *     }
816
		 * }
817
		 * @return array
818
		 */
819
		self::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', $default_currencies );
820
821
		// Sort the currencies
822
		ksort( self::$currencies );
823
824
		// Backwards compatibility
825
		foreach ( self::$currencies as $key => $value ) {
826
			if ( is_string( $value ) ) {
827
				self::$currencies[ $key ] = array(
828
					'label' => strtoupper( $key ),
829
					'name' => strtoupper( $key ),
830
					'sign' => $value,
831
				);
832
			} elseif ( is_array( $value ) ) {
833
				// Make sure all required values are set
834
				if ( empty( $value['label'] ) ) {
835
					$value['label'] = $key;
836
				}
837
				if ( empty( $value['name'] ) ) {
838
					$value['name'] = $key;
839
				}
840
				if ( empty( $value['sign'] ) ) {
841
					$value['sign'] = $key;
842
				}
843
			} else {
844
				// Invalid
845
				unset( self::$currencies[ $key ] );
846
			}
847
		}
848
849
		return self::$currencies;
850
	}
851
}
852