-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormatter.php
230 lines (191 loc) · 7.17 KB
/
Formatter.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Formatter;
use DateInterval;
use DateTimeInterface;
use DateTimeZone;
use Exception;
use Illuminate\Support\Traits\Macroable;
use IntlTimeZone;
use LastDragon_ru\LaraASP\Core\Application\ApplicationResolver;
use LastDragon_ru\LaraASP\Core\Application\ConfigResolver;
use LastDragon_ru\LaraASP\Formatter\Contracts\Format;
use LastDragon_ru\LaraASP\Formatter\Exceptions\FormatterFailedToFormatValue;
use OutOfBoundsException;
use Stringable;
use function sprintf;
class Formatter {
use Macroable;
public const string String = 'string';
public const string Integer = 'integer';
public const string Scientific = 'scientific';
public const string Spellout = 'spellout';
public const string Ordinal = 'ordinal';
public const string Decimal = 'decimal';
public const string Percent = 'percent';
public const string Time = 'time';
public const string Date = 'date';
public const string DateTime = 'datetime';
public const string Filesize = 'filesize';
public const string Disksize = 'disksize';
public const string Secret = 'secret';
public const string Currency = 'currency';
public const string Duration = 'duration';
/**
* @var array<string, Format<*, mixed>>
*/
private array $cache = [];
private ?string $locale = null;
private IntlTimeZone|DateTimeZone|string|null $timezone = null;
public function __construct(
protected readonly ApplicationResolver $application,
protected readonly ConfigResolver $config,
protected readonly PackageConfig $package,
) {
// empty
}
// <editor-fold desc="Factory">
// =========================================================================
/**
* Create a new formatter for the specified locale.
*/
public function forLocale(?string $locale): static {
$formatter = $this;
if ($this->locale !== $locale) {
$formatter = clone $this;
$formatter->locale = $locale;
}
return $formatter;
}
/**
* Create a new formatter for the specified timezone.
*/
public function forTimezone(IntlTimeZone|DateTimeZone|string|null $timezone): static {
$formatter = $this;
if ($this->timezone !== $timezone) {
$formatter = clone $this;
$formatter->timezone = $timezone;
}
return $formatter;
}
// </editor-fold>
// <editor-fold desc="Getters & Setters">
// =========================================================================
public function getLocale(): string {
return $this->locale ?? $this->getDefaultLocale();
}
public function getTimezone(): IntlTimeZone|DateTimeZone|string|null {
return $this->timezone ?? $this->getDefaultTimezone();
}
// </editor-fold>
// <editor-fold desc="Format">
// =========================================================================
public function format(string $format, mixed $value): string {
try {
return ($this->getFormat($format))($value);
} catch (Exception $exception) {
throw new FormatterFailedToFormatValue($format, $value, $exception);
}
}
/**
* @return Format<*, mixed>
*/
protected function getFormat(string $format): Format {
// Cached?
if (isset($this->cache[$format])) {
return $this->cache[$format];
}
// Known?
$config = $this->package->getInstance();
$settings = $config->formats[$format] ?? null;
if ($settings === null) {
throw new OutOfBoundsException(sprintf('The `%s` format is unknown.', $format));
}
// Create
$locale = $this->getLocale();
$this->cache[$format] = $this->application->getInstance()->make($settings->class, [
'formatter' => $this,
'options' => [
$settings->locales[$locale] ?? null,
$settings->default,
],
]);
return $this->cache[$format];
}
// </editor-fold>
// <editor-fold desc="Formats">
// =========================================================================
public function string(Stringable|string|null $value): string {
return $this->format(self::String, $value);
}
public function integer(float|int|null $value): string {
return $this->format(self::Integer, $value);
}
public function decimal(float|int|null $value): string {
return $this->format(self::Decimal, $value);
}
/**
* @param non-empty-string|null $currency
*/
public function currency(float|int|null $value, ?string $currency = null): string {
return $this->format(self::Currency, [$value, $currency]);
}
/**
* @param float|int|null $value must be between 0-100
*/
public function percent(float|int|null $value): string {
return $this->format(self::Percent, $value !== null ? $value / 100 : $value);
}
public function scientific(float|int|null $value): string {
return $this->format(self::Scientific, $value);
}
public function spellout(float|int|null $value): string {
return $this->format(self::Spellout, $value);
}
public function ordinal(?int $value): string {
return $this->format(self::Ordinal, $value);
}
public function duration(DateInterval|float|int|null $value): string {
return $this->format(self::Duration, $value);
}
public function time(?DateTimeInterface $value): string {
return $this->format(self::Time, $value);
}
public function date(?DateTimeInterface $value): string {
return $this->format(self::Date, $value);
}
public function datetime(?DateTimeInterface $value): string {
return $this->format(self::DateTime, $value);
}
/**
* Formats number of bytes into units based on powers of 2 (kibibyte, mebibyte, etc).
*
* @param numeric-string|float|int|null $bytes
*/
public function filesize(string|float|int|null $bytes): string {
return $this->format(self::Filesize, $bytes);
}
/**
* Formats number of bytes into units based on powers of 10 (kilobyte, megabyte, etc).
*
* @param numeric-string|float|int|null $bytes
*/
public function disksize(string|float|int|null $bytes): string {
return $this->format(self::Disksize, $bytes);
}
public function secret(?string $value): string {
return $this->format(self::Secret, $value);
}
// </editor-fold>
// <editor-fold desc="Functions">
// =========================================================================
protected function getDefaultLocale(): string {
return $this->application->getInstance()->getLocale();
}
protected function getDefaultTimezone(): IntlTimeZone|DateTimeZone|string|null {
return $this->config->getInstance()->get('app.timezone') ?? null;
}
public function __clone(): void {
$this->cache = [];
}
// </editor-fold>
}