|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Propaganistas\LaravelIntl; |
|
4
|
|
|
|
|
5
|
|
|
use CommerceGuys\Intl\Formatter\NumberFormatter; |
|
6
|
|
|
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Propaganistas\LaravelIntl\Contracts\Intl; |
|
9
|
|
|
|
|
10
|
|
|
class Number extends Intl |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Array of localized number formatters. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $formatters; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The current locale. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string $locale |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $locale; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The current locale. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string $locale |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $fallbackLocale; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Format a number. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|int|float $number |
|
37
|
|
|
* @param array $options |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
15 |
|
public function format($number, $options = []) |
|
41
|
|
|
{ |
|
42
|
15 |
|
return $this->formatter()->format($number, |
|
43
|
15 |
|
$this->mergeOptions($options) |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Format as percentage. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string|int|float $number |
|
51
|
|
|
* @param array $options |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function percent($number, $options = []) |
|
55
|
|
|
{ |
|
56
|
3 |
|
return $this->formatter()->format($number, |
|
57
|
3 |
|
$this->mergeOptions($options, ['style' => 'percent']) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Parse a localized number into native PHP format. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string|int|float $number |
|
65
|
|
|
* @param array $options |
|
66
|
|
|
* @return string|false |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function parse($number, $options = []) |
|
69
|
|
|
{ |
|
70
|
3 |
|
return $this->formatter()->parse($number, |
|
71
|
3 |
|
$this->mergeOptions($options) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the current locale. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
24 |
|
public function getLocale() |
|
81
|
|
|
{ |
|
82
|
24 |
|
return $this->locale; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set the current locale. |
|
87
|
|
|
* |
|
88
|
|
|
* @param $locale |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
24 |
|
public function setLocale($locale) |
|
92
|
|
|
{ |
|
93
|
24 |
|
$this->locale = $locale; |
|
94
|
|
|
|
|
95
|
24 |
|
$this->load($locale, $this->getFallbackLocale()); |
|
96
|
|
|
|
|
97
|
24 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the fallback locale. |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
24 |
|
public function getFallbackLocale() |
|
106
|
|
|
{ |
|
107
|
24 |
|
return $this->fallbackLocale; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the fallback locale. |
|
112
|
|
|
* |
|
113
|
|
|
* @param $locale |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
24 |
|
public function setFallbackLocale($locale) |
|
117
|
|
|
{ |
|
118
|
24 |
|
$this->fallbackLocale = $locale; |
|
119
|
|
|
|
|
120
|
24 |
|
$this->load($this->getLocale(), $locale); |
|
121
|
|
|
|
|
122
|
24 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Load the format repository for the given locale. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $locale |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
24 |
|
protected function load($locale, $fallbackLocale) |
|
132
|
|
|
{ |
|
133
|
24 |
|
$key = $this->getLocalesKey($locale, $fallbackLocale); |
|
134
|
|
|
|
|
135
|
24 |
View Code Duplication |
if (! isset($this->repositories[$key])) { |
|
|
|
|
|
|
136
|
24 |
|
$this->formatters[$key] = new NumberFormatter(new NumberFormatRepository($fallbackLocale), ['locale' => $locale]); |
|
137
|
|
|
} |
|
138
|
24 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the formatter's key. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string|null $locale |
|
144
|
|
|
* @param string|null $fallbackLocale |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
24 |
View Code Duplication |
protected function getLocalesKey($locale = null, $fallbackLocale = null) |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
24 |
|
return implode('|', [ |
|
150
|
24 |
|
$locale ?: $this->getLocale(), |
|
151
|
24 |
|
$fallbackLocale ?: $this->getFallbackLocale(), |
|
152
|
|
|
]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* The current number formatter. |
|
157
|
|
|
* |
|
158
|
|
|
* @return \CommerceGuys\Intl\Formatter\NumberFormatter |
|
159
|
|
|
*/ |
|
160
|
21 |
|
protected function formatter() |
|
161
|
|
|
{ |
|
162
|
21 |
|
return $this->formatters[$this->getLocalesKey()]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Merges the options array. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $options |
|
169
|
|
|
* @param array $defaults |
|
170
|
|
|
* @return array |
|
171
|
|
|
*/ |
|
172
|
21 |
|
protected function mergeOptions(array $options, array $defaults = []) |
|
173
|
|
|
{ |
|
174
|
21 |
|
Arr::forget($options, 'locale'); |
|
175
|
|
|
|
|
176
|
21 |
|
return $defaults + $options; |
|
177
|
|
|
} |
|
178
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: