1
|
|
|
<?php namespace Propaganistas\LaravelIntl; |
2
|
|
|
|
3
|
|
|
use CommerceGuys\Intl\Formatter\NumberFormatter; |
4
|
|
|
use CommerceGuys\Intl\NumberFormat\NumberFormat; |
5
|
|
|
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository; |
6
|
|
|
use Propaganistas\LaravelIntl\Base\Intl; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
class Number extends Intl |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \CommerceGuys\Intl\NumberFormat\NumberFormatRepository |
13
|
|
|
*/ |
14
|
|
|
protected $data; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Number constructor. |
18
|
|
|
* |
19
|
|
|
* @param \CommerceGuys\Intl\NumberFormat\NumberFormatRepository $data |
20
|
|
|
*/ |
21
|
27 |
|
public function __construct(NumberFormatRepository $data) |
22
|
|
|
{ |
23
|
27 |
|
$this->data = $data; |
24
|
27 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Format an value in the given locale (or app locale if not specified). |
28
|
|
|
* |
29
|
|
|
* @param int|float $value |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
12 |
|
public function format($value) |
33
|
|
|
{ |
34
|
12 |
|
$format = $this->get(); |
35
|
12 |
|
$formatter = new NumberFormatter($format); |
36
|
|
|
|
37
|
12 |
|
return $formatter->format($value); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Format an value as percents in the given locale (or app locale if not specified). |
42
|
|
|
* |
43
|
|
|
* @param int|float $value |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
3 |
|
public function percent($value) |
47
|
|
|
{ |
48
|
3 |
|
$format = $this->get(); |
49
|
3 |
|
$formatter = new NumberFormatter($format, NumberFormatter::PERCENT); |
50
|
|
|
|
51
|
3 |
|
return $formatter->format($value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Parse a localized number into native PHP format. |
56
|
|
|
* |
57
|
|
|
* @param string|int|float $value |
58
|
|
|
* @return int|float |
59
|
|
|
*/ |
60
|
3 |
View Code Duplication |
public function parse($value) |
|
|
|
|
61
|
|
|
{ |
62
|
3 |
|
$format = $this->get(null); |
63
|
3 |
|
$formatter = new NumberFormatter($format); |
64
|
|
|
|
65
|
|
|
// At time of writing, commerceguys/intl has number parsing still coupled to a currency. Parsing does |
66
|
|
|
// succeed however,even though a value is provided without any currency. So let's just pass in |
67
|
|
|
// a very rare currency to avoid unwanted formatting behavior. Sorry Cape Verdean Escudo! |
68
|
3 |
|
return $formatter->parseCurrency($value, $currency = currency()->get('CVE')); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get a localized entry. |
73
|
|
|
* |
74
|
|
|
* @param string|null $code |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
21 |
|
public function get($code = null) |
78
|
|
|
{ |
79
|
21 |
|
return $this->data->get(null); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get a localized list of entries, keyed by their code. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
3 |
|
public function all() |
88
|
|
|
{ |
89
|
|
|
// Unsupported. |
90
|
3 |
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the 'digits' property of a given NumberFormat. |
95
|
|
|
* |
96
|
|
|
* @param \CommerceGuys\Intl\NumberFormat\NumberFormat $format |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function getDigitsOfFormat(NumberFormat $format) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$formatter = new NumberFormatter($format); |
102
|
|
|
|
103
|
|
|
$reflection = new ReflectionClass($formatter); |
104
|
|
|
$property = $reflection->getProperty('digits'); |
105
|
|
|
$property->setAccessible(true); |
106
|
|
|
|
107
|
|
|
return $property->getValue($formatter); |
108
|
|
|
} |
109
|
|
|
} |
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.