Completed
Pull Request — 2.x (#3950)
by Jory
04:34
created

PodsField_Currency   C

Complexity

Total Complexity 75

Size/Duplication

Total Lines 692
Duplicated Lines 32.95 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 228
loc 692
rs 5.0445
c 0
b 0
f 0
wmc 75
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B options() 0 119 1
B schema() 26 26 6
C prepare() 33 33 7
C display() 3 36 7
C input() 39 39 8
C regex() 31 44 8
C validate() 31 59 9
C pre_save() 31 70 13
A ui() 0 5 1
C format() 34 67 14

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PodsField_Currency often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PodsField_Currency, and based on these observations, apply Extract Interface, too.

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
		'usd' => '$',
48
		'euro' => '&euro;',
49
		'gbp' => '&pound;',
50
		'cad' => '$',
51
		'aud' => '$',
52
		'nzd' => '$',
53
		'rub' => '&#8381;',
54
		'chf' => 'CHF',
55
		'dkk' => 'kr',
56
		'nok' => 'kr',
57
		'sek' => 'kr',
58
		'zar' => 'R',
59
		'inr' => '&#x20B9;',
60
		'jpy' => '&yen;',
61
		'cny' => '&yen;',
62
		'sgd' => '$',
63
		'krw' => '&#8361;',
64
		'thb' => '&#x0E3F;',
65
		'trl' => '&#8378;',
66
		'vnd' => '&#8363;'
67
	);
68
69
	/**
70
	 * Do things like register/enqueue scripts and stylesheets
71
	 *
72
	 * @since 2.0
73
	 */
74
	public function __construct() {
75
		self::$label = __( 'Currency', 'pods' );
76
		self::$currencies = apply_filters( 'pods_form_ui_field_currency_currencies', self::$currencies );
77
	}
78
79
	/**
80
	 * Add options and set defaults to
81
	 *
82
	 * @return array
83
	 *
84
	 * @since 2.0
85
	 */
86
	public function options() {
87
88
		$options = array(
89
			self::$type . '_repeatable' => array(
90
				'label' => __( 'Repeatable Field', 'pods' ),
91
				'default' => 0,
92
				'type' => 'boolean',
93
				'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' ),
94
				'boolean_yes_label' => '',
95
				'dependency' => true,
96
				'developer_mode' => true
97
			),
98
			self::$type . '_format_type' => array(
99
				'label' => __( 'Input Type', 'pods' ),
100
				'default' => 'number',
101
				'type' => 'pick',
102
				'data' => array(
103
					'number' => __( 'Freeform Number', 'pods' ),
104
					'slider' => __( 'Slider', 'pods' )
105
				),
106
				'dependency' => true
107
			),
108
			self::$type . '_format_sign' => array(
109
				'label' => __( 'Currency Sign', 'pods' ),
110
				'default' => apply_filters( 'pods_form_ui_field_number_currency_default', 'usd' ),
111
				'type' => 'pick',
112
				'data' => apply_filters( 'pods_form_ui_field_number_currency_options', array(
113
						'usd' => '$ (USD)',
114
						'euro' => '&euro; (EUR)',
115
						'gbp' => '&pound; (GBP)',
116
						'aud' => 'Australian Dollar (AUD)',
117
						'cad' => 'Canadian Dollar (CAD)',
118
						'cny' => 'Chinese Yuan (CNY)',
119
						'dkk' => 'Danish Krone (DKK)',
120
						'inr' => 'Indian Rupee (INR)',
121
						'jpy' => 'Japanese Yen (JPY)',
122
						'krw' => 'Korean Won (KRW)',
123
						'nzd' => 'New Zealand Dollar (NZD)',
124
						'nok' => 'Norwegian Krone (NOK)',
125
						'rub' => 'Russian Ruble (RUB)',
126
						'sgd' => 'Singapore Dollar (SGD)',
127
						'zar' => 'South African Rand (ZAR)',
128
						'sek' => 'Swedish Krona (SEK)',
129
						'chf' => 'Swiss Franc (CHF)',
130
						'thb' => 'Thai Baht (THB)',
131
						'trl' => 'Turkish Lira (TRL)',
132
						'vnd' => 'Vietnamese Dong (VND)'
133
					) )
134
			),
135
			self::$type . '_format_placement' => array(
136
				'label' => __( 'Currency Placement', 'pods' ),
137
				'default' => apply_filters( 'pods_form_ui_field_number_currency_placement_default', 'before' ),
138
				'type' => 'pick',
139
				'data' => array(
140
					'before' => __( 'Before (ex. $100)', 'pods' ),
141
					'after' => __( 'After (ex. 100$)', 'pods' ),
142
					'none' => __( 'None (ex. 100)', 'pods' ),
143
					'beforeaftercode' => __( 'Before with Currency Code after (ex. $100 USD)', 'pods' )
144
				)
145
			),
146
			self::$type . '_format' => array(
147
				'label' => __( 'Format', 'pods' ),
148
				'default' => apply_filters( 'pods_form_ui_field_number_currency_format_default', 'i18n' ),
149
				'type' => 'pick',
150
				'data' => array(
151
					'i18n' => __( 'Localized Default', 'pods' ),
152
					'9,999.99' => '1,234.00',
153
					'9\'999.99' => '1\'234.00',
154
					'9.999,99' => '1.234,00',
155
					'9 999,99' => '1 234,00',
156
					'9999.99' => '1234.00',
157
					'9999,99' => '1234,00'
158
				)
159
			),
160
			self::$type . '_decimals' => array(
161
				'label' => __( 'Decimals', 'pods' ),
162
				'default' => 2,
163
				'type' => 'number'
164
			),
165
			self::$type . '_step' => array(
166
				'label' => __( 'Slider Increment (Step)', 'pods' ),
167
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
168
				'default' => 1,
169
				'type' => 'text'
170
			),
171
			self::$type . '_min' => array(
172
				'label' => __( 'Minimum Number', 'pods' ),
173
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
174
				'default' => 0,
175
				'type' => 'text'
176
			),
177
			self::$type . '_max' => array(
178
				'label' => __( 'Maximum Number', 'pods' ),
179
				'depends-on' => array( self::$type . '_format_type' => 'slider' ),
180
				'default' => 1000,
181
				'type' => 'text'
182
			),
183
			self::$type . '_max_length' => array(
184
				'label' => __( 'Maximum Length', 'pods' ),
185
				'default' => 12,
186
				'type' => 'number',
187
				'help' => __( 'Set to -1 for no limit', 'pods' )
188
			)
189
			/*,
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...
190
			self::$type . '_size' => array(
191
				'label' => __( 'Field Size', 'pods' ),
192
				'default' => 'medium',
193
				'type' => 'pick',
194
				'data' => array(
195
					'small' => __( 'Small', 'pods' ),
196
					'medium' => __( 'Medium', 'pods' ),
197
					'large' => __( 'Large', 'pods' )
198
				)
199
			)*/
200
		);
201
202
		return $options;
203
204
	}
205
206
	/**
207
	 * Define the current field's schema for DB table storage
208
	 *
209
	 * @param array $options
210
	 *
211
	 * @return array
212
	 * @since 2.0
213
	 */
214 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...
215
216
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
217
218
		if ( $length < 1 || 64 < $length ) {
219
			$length = 64;
220
		}
221
222
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
223
224
		if ( $decimals < 1 ) {
225
			$decimals = 0;
226
		}
227
		elseif ( 30 < $decimals ) {
228
			$decimals = 30;
229
		}
230
231
		if ( $length < $decimals ) {
232
			$decimals = $length;
233
		}
234
235
		$schema = 'DECIMAL(' . $length . ',' . $decimals . ')';
236
237
		return $schema;
238
239
	}
240
241
	/**
242
	 * Define the current field's preparation for sprintf
243
	 *
244
	 * @param array $options
245
	 *
246
	 * @return array
247
	 * @since 2.0
248
	 */
249 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...
250
251
		$format = self::$prepare;
252
253
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
254
255
		if ( $length < 1 || 64 < $length ) {
256
			$length = 64;
257
		}
258
259
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
260
261
		if ( $decimals < 1 ) {
262
			$decimals = 0;
263
		}
264
		elseif ( 30 < $decimals ) {
265
			$decimals = 30;
266
		}
267
268
		if ( $length < $decimals ) {
269
			$decimals = $length;
270
		}
271
272
		if ( 0 < $decimals ) {
273
			$format = '%01.' . $decimals . 'F';
274
		}
275
		else {
276
			$format = '%d';
277
		}
278
279
		return $format;
280
281
	}
282
283
	/**
284
	 * Change the way the value of the field is displayed with Pods::get
285
	 *
286
	 * @param mixed $value
287
	 * @param string $name
288
	 * @param array $options
289
	 * @param array $pod
290
	 * @param int $id
291
	 *
292
	 * @return mixed|null|string
293
	 * @since 2.0
294
	 */
295
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
296
297
		$value = $this->format( $value, $name, $options, $pod, $id );
298
299
		$currency = 'usd';
300
301 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
302
			$currency = pods_v( self::$type . '_format_sign', $options );
303
		}
304
305
		$currency_sign = self::$currencies[ $currency ];
306
307
		$placement = pods_v( self::$type . '_format_placement', $options, 'before', true );
308
309
		// Currency placement policy
310
		// Single sign currencies: 100$, £100
311
		// Multiple sign currencies: 100 Fr, Kr 100
312
		$currency_gap = '';
313
314
		if ( strlen( $currency_sign ) > 1 && false === strpos( $currency_sign, '&' ) ) {
315
			$currency_gap = ' ';
316
		}
317
318
		if ( 'before' == $placement ) {
319
			$value = $currency_sign . $currency_gap . $value;
320
		}
321
		elseif ( 'after' == $placement ) {
322
			$value .= $currency_gap . $currency_sign;
323
		}
324
		elseif ( 'beforeaftercode' == $placement ) {
325
			$value = $currency_sign . $currency_gap . $value . ' ' . strtoupper( $currency );
326
		}
327
328
		return $value;
329
330
	}
331
332
	/**
333
	 * Customize output of the form field
334
	 *
335
	 * @param string $name
336
	 * @param mixed $value
337
	 * @param array $options
338
	 * @param array $pod
339
	 * @param int $id
340
	 *
341
	 * @since 2.0
342
	 */
343 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...
344
345
		$options = (array) $options;
346
		$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...
347
348
		if ( is_array( $value ) ) {
349
			$value = implode( '', $value );
350
		}
351
352
		if ( 'slider' == pods_v( self::$type . '_format_type', $options, 'number' ) ) {
353
			$field_type = 'slider';
354
		}
355
		else {
356
			$field_type = 'currency';
357
		}
358
359
		if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) {
360
			if ( pods_v( 'read_only', $options, false ) ) {
361
				$options[ 'readonly' ] = true;
362
363
				$field_type = 'text';
364
365
				$value = $this->format( $value, $name, $options, $pod, $id );
366
			}
367
			else {
368
				return;
369
			}
370
		}
371
		elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
372
			$options[ 'readonly' ] = true;
373
374
			$field_type = 'text';
375
376
			$value = $this->format( $value, $name, $options, $pod, $id );
377
		}
378
379
		pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
380
381
	}
382
383
	/**
384
	 * Build regex necessary for JS validation
385
	 *
386
	 * @param mixed $value
387
	 * @param string $name
388
	 * @param array $options
389
	 * @param string $pod
390
	 * @param int $id
391
	 *
392
	 * @return bool|string
393
	 * @since 2.0
394
	 */
395
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
396
397
		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...
398
399 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
400
			$thousands = '.';
401
			$dot = ',';
402
		}
403
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
404
			$thousands = ',';
405
			$dot = '.';
406
		}
407
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
408
			$thousands = '\'';
409
			$dot = '.';
410
		}
411
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
412
			$thousands = ' ';
413
			$dot = ',';
414
		}
415
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
416
			$thousands = '';
417
			$dot = '.';
418
		}
419
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
420
			$thousands = '';
421
			$dot = ',';
422
		}
423
		else {
424
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
425
			$dot = $wp_locale->number_format[ 'decimal_point' ];
426
		}
427
428
		$currency = 'usd';
429
430 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
431
			$currency = pods_v( self::$type . '_format_sign', $options );
432
		}
433
434
		$currency_sign = self::$currencies[ $currency ];
435
436
		return '\-*\\' . $currency_sign . '*[0-9\\' . implode( '\\', array_filter( array( $dot, $thousands ) ) ) . ']+';
437
438
	}
439
440
	/**
441
	 * Validate a value before it's saved
442
	 *
443
	 * @param mixed $value
444
	 * @param string $name
445
	 * @param array $options
446
	 * @param array $fields
447
	 * @param array $pod
448
	 * @param int $id
449
	 * @param null $params
450
	 *
451
	 * @return bool|mixed
452
	 * @since 2.0
453
	 */
454
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
455
456
		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...
457
458 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
459
			$thousands = '.';
460
			$dot = ',';
461
		}
462
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
463
			$thousands = ',';
464
			$dot = '.';
465
		}
466
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
467
			$thousands = '\'';
468
			$dot = '.';
469
		}
470
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
471
			$thousands = ' ';
472
			$dot = ',';
473
		}
474
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
475
			$thousands = ',';
476
			$dot = '.';
477
		}
478
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
479
			$thousands = '.';
480
			$dot = ',';
481
		}
482
		else {
483
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
484
			$dot = $wp_locale->number_format[ 'decimal_point' ];
485
		}
486
487
		$currency = 'usd';
488
489 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
490
			$currency = pods_v( self::$type . '_format_sign', $options );
491
		}
492
493
		$currency_sign = self::$currencies[ $currency ];
494
495
		$check = str_replace(
496
			array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ), html_entity_decode( $thousands ) ),
497
			array( '', '.', '', '', '' ),
498
			$value
499
		);
500
		$check = trim( $check );
501
502
		$check = preg_replace( '/[0-9\.\-\s]/', '', $check );
503
504
		$label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) );
505
506
		if ( 0 < strlen( $check ) ) {
507
			return sprintf( __( '%s is not numeric', 'pods' ), $label );
508
		}
509
510
		return true;
511
512
	}
513
514
	/**
515
	 * Change the value or perform actions after validation but before saving to the DB
516
	 *
517
	 * @param mixed $value
518
	 * @param int $id
519
	 * @param string $name
520
	 * @param array $options
521
	 * @param array $fields
522
	 * @param array $pod
523
	 * @param object $params
524
	 *
525
	 * @return mixed|string
526
	 * @since 2.0
527
	 */
528
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
529
530
		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...
531
532 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
533
			$thousands = '.';
534
			$dot = ',';
535
		}
536
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
537
			$thousands = ',';
538
			$dot = '.';
539
		}
540
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
541
			$thousands = '\'';
542
			$dot = '.';
543
		}
544
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
545
			$thousands = ' ';
546
			$dot = ',';
547
		}
548
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
549
			$thousands = ',';
550
			$dot = '.';
551
		}
552
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
553
			$thousands = '.';
554
			$dot = ',';
555
		}
556
		else {
557
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
558
			$dot = $wp_locale->number_format[ 'decimal_point' ];
559
		}
560
561
		$currency = 'usd';
562
563 View Code Duplication
		if ( isset( self::$currencies[ pods_v( self::$type . '_format_sign', $options, -1 ) ] ) ) {
564
			$currency = pods_v( self::$type . '_format_sign', $options );
565
		}
566
567
		$currency_sign = self::$currencies[ $currency ];
568
569
		$value = str_replace( array( $thousands, $dot, $currency_sign, html_entity_decode( $currency_sign ) ), array( '', '.', '', '' ), $value );
570
		$value = trim( $value );
571
572
		$value = preg_replace( '/[^0-9\.\-]/', '', $value );
573
574
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
575
576
		if ( $length < 1 || 64 < $length ) {
577
			$length = 64;
578
		}
579
580
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2, true );
581
582
		if ( $decimals < 1 ) {
583
			$decimals = 0;
584
		}
585
		elseif ( 30 < $decimals ) {
586
			$decimals = 30;
587
		}
588
589
		if ( $length < $decimals ) {
590
			$decimals = $length;
591
		}
592
593
		$value = number_format( (float) $value, $decimals, '.', '' );
594
595
		return $value;
596
597
	}
598
599
	/**
600
	 * Customize the Pods UI manage table column output
601
	 *
602
	 * @param int $id
603
	 * @param mixed $value
604
	 * @param string $name
605
	 * @param array $options
606
	 * @param array $fields
607
	 * @param array $pod
608
	 *
609
	 * @return mixed|null|string
610
	 * @since 2.0
611
	 */
612
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
613
614
		return $this->display( $value, $name, $options, $pod, $id );
615
616
	}
617
618
	/**
619
	 * Reformat a number to the way the value of the field is displayed
620
	 *
621
	 * @param mixed $value
622
	 * @param string $name
623
	 * @param array $options
624
	 * @param array $pod
625
	 * @param int $id
626
	 *
627
	 * @return string
628
	 * @since 2.0
629
	 */
630
	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...
631
632
		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...
633
634
		if ( null === $value ) {
635
			// Don't enforce a default value here
636
			return null;
637
		}
638
639 View Code Duplication
		if ( '9.999,99' == pods_v( self::$type . '_format', $options ) ) {
640
			$thousands = '.';
641
			$dot = ',';
642
		}
643
		elseif ( '9,999.99' == pods_v( self::$type . '_format', $options ) ) {
644
			$thousands = ',';
645
			$dot = '.';
646
		}
647
		elseif ( '9\'999.99' == pods_v( self::$type . '_format', $options ) ) {
648
			$thousands = '\'';
649
			$dot = '.';
650
		}
651
		elseif ( '9 999,99' == pods_v( self::$type . '_format', $options ) ) {
652
			$thousands = ' ';
653
			$dot = ',';
654
		}
655
		elseif ( '9999.99' == pods_v( self::$type . '_format', $options ) ) {
656
			$thousands = '';
657
			$dot = '.';
658
		}
659
		elseif ( '9999,99' == pods_v( self::$type . '_format', $options ) ) {
660
			$thousands = '';
661
			$dot = ',';
662
		}
663
		else {
664
			$thousands = $wp_locale->number_format[ 'thousands_sep' ];
665
			$dot = $wp_locale->number_format[ 'decimal_point' ];
666
		}
667
668
		$length = (int) pods_v( self::$type . '_max_length', $options, 12, true );
669
670
		if ( $length < 1 || 64 < $length ) {
671
			$length = 64;
672
		}
673
674
		$decimals = (int) pods_v( self::$type . '_decimals', $options, 2 );
675
676
		if ( $decimals < 1 ) {
677
			$decimals = 0;
678
		}
679
		elseif ( 30 < $decimals ) {
680
			$decimals = 30;
681
		}
682
683
		if ( $length < $decimals ) {
684
			$decimals = $length;
685
		}
686
687 View Code Duplication
		if ( 'i18n' == pods_v( self::$type . '_format', $options ) ) {
688
			$value = number_format_i18n( (float) $value, $decimals );
689
		}
690
		else {
691
			$value = number_format( (float) $value, $decimals, $dot, $thousands );
692
		}
693
694
		return $value;
695
696
	}
697
}
698