-
Notifications
You must be signed in to change notification settings - Fork 11
/
NumeralConverter.php
53 lines (45 loc) · 1.09 KB
/
NumeralConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace mdm\converter;
/**
* NumeralConverter
* Change number format
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class NumeralConverter extends BaseConverter
{
/**
* @var string Thousands separator
*/
public $thousands_sep = ',';
/**
* @var string Decimal point
*/
public $decimal_point = '.';
/**
* @var integer Decimal precision
*/
public $decimals = 0;
/**
* @inheritdoc
*/
protected function convertToLogical($value, $attribute)
{
if ($value === null || $value === '') {
return $value;
}
return number_format($value, $this->decimals, $this->decimal_point, $this->thousands_sep);
}
/**
* @inheritdoc
*/
protected function convertToPhysical($value, $attribute)
{
if ($value === null || $value === '') {
return $value;
}
$number = explode($this->decimal_point, $value, 2);
return str_replace($this->thousands_sep, '', $number[0]) . (isset($number[1]) ? '.' . $number[1] : '');
}
}