1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.services |
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Intl\Intl; |
10
|
|
|
use OpenPsa\Ranger\Ranger; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Symfony Intl integration |
14
|
|
|
* |
15
|
|
|
* @package midcom.services |
16
|
|
|
*/ |
17
|
|
|
class midcom_services_i18n_formatter |
18
|
|
|
{ |
19
|
|
|
private string $language; |
20
|
|
|
|
21
|
61 |
|
public function __construct(string $language) |
22
|
|
|
{ |
23
|
61 |
|
$this->language = $language; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function number(int|float $value, int $precision = 2) |
27
|
|
|
{ |
28
|
|
|
// The fallback implementation in Intl only supports DECIMAL, so we hardcode the style here.. |
29
|
|
|
$formatter = new NumberFormatter($this->get_locale(), NumberFormatter::DECIMAL); |
30
|
|
|
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); |
31
|
|
|
return $formatter->format($value); |
32
|
|
|
} |
33
|
|
|
|
34
|
10 |
|
public function amount(int|float $value) |
35
|
|
|
{ |
36
|
|
|
// The fallback implementation in Intl only supports DECIMAL, so we hardcode the style here.. |
37
|
10 |
|
$formatter = new NumberFormatter($this->get_locale(), NumberFormatter::DECIMAL); |
38
|
10 |
|
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); |
39
|
10 |
|
$formatter->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_HALFUP); |
40
|
10 |
|
return $formatter->format($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
23 |
|
public function date(int|string|null|DateTimeInterface $value = null, int|string $dateformat = 'medium') |
44
|
|
|
{ |
45
|
23 |
|
return $this->datetime($value, $dateformat, IntlDateFormatter::NONE); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function time(int|string|null|DateTimeInterface $value = null, int|string $timeformat = 'short') |
49
|
|
|
{ |
50
|
1 |
|
return $this->datetime($value, IntlDateFormatter::NONE, $timeformat); |
51
|
|
|
} |
52
|
|
|
|
53
|
51 |
|
public function datetime(int|string|null|DateTimeInterface $value = null, int|string $dateformat = 'medium', int|string $timeformat = 'short') |
54
|
|
|
{ |
55
|
51 |
|
$value ??= time(); |
56
|
51 |
|
$formatter = new IntlDateFormatter($this->get_locale(), $this->constant($dateformat), $this->constant($timeformat)); |
57
|
51 |
|
return $formatter->format($value); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function customdate(int|string|DateTimeInterface $value, string $pattern) |
61
|
|
|
{ |
62
|
3 |
|
$formatter = new IntlDateFormatter($this->get_locale(), IntlDateFormatter::FULL, IntlDateFormatter::FULL); |
63
|
3 |
|
$formatter->setPattern($pattern); |
64
|
3 |
|
return $formatter->format($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
public function timeframe($start, $end, string $mode = 'both', ?string $range_separator = null, bool $fulldate = false) : string |
68
|
|
|
{ |
69
|
4 |
|
$ranger = new Ranger($this->get_locale()); |
70
|
4 |
|
if ($mode !== 'date') { |
71
|
|
|
$ranger->setTimeType(IntlDateFormatter::SHORT); |
72
|
|
|
} |
73
|
4 |
|
if ($fulldate) { |
74
|
|
|
$ranger->setDateType(IntlDateFormatter::FULL); |
75
|
|
|
} |
76
|
4 |
|
if ($range_separator !== null) { |
77
|
|
|
$ranger->setRangeSeparator($range_separator); |
78
|
|
|
} |
79
|
4 |
|
return $ranger->format($start, $end); |
80
|
|
|
} |
81
|
|
|
|
82
|
51 |
|
private function constant(int|string $input) : int |
83
|
|
|
{ |
84
|
51 |
|
if (is_int($input)) { |
85
|
28 |
|
return $input; |
86
|
|
|
} |
87
|
48 |
|
return constant('IntlDateFormatter::' . strtoupper($input)); |
88
|
|
|
} |
89
|
|
|
|
90
|
54 |
|
private function get_locale() : string |
91
|
|
|
{ |
92
|
54 |
|
return Intl::isExtensionLoaded() ? $this->language : 'en'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|