Skip to content

Commit 7018ba9

Browse files
committed
README wip
1 parent 283742c commit 7018ba9

File tree

8 files changed

+365
-82
lines changed

8 files changed

+365
-82
lines changed

README.md

Lines changed: 259 additions & 23 deletions
Large diffs are not rendered by default.

src/AbstractBasicConverter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ function (self $remainder, array $sortedUnits) use ($options, $formats, $divider
7878
$formatted = trim(implode($divider, array_filter($text)));
7979

8080
if (!$formatted && !($options & static::OPTION_NO_FALLBACK)) {
81-
$minSuffix = $unitFormatters[$this->baseUnit];
82-
$formatted = $this->with(0, $this->baseUnit)->format($minSuffix);
81+
$minSuffix = $unitFormatters[$this->unit];
82+
$formatted = $this->with(0, $this->unit)->format($minSuffix);
8383
}
8484

8585
return $formatted;
@@ -99,7 +99,7 @@ public function nearest(
9999
}
100100

101101
$closestValue = $this->value;
102-
$closestUnit = $this->baseUnit;
102+
$closestUnit = $this->unit;
103103
$minDistance = null;
104104

105105
foreach ($sortedUnits as $unit => $rate) {
@@ -126,7 +126,7 @@ public function nearest(
126126

127127
$new = clone $this;
128128
$new->value = $closestValue;
129-
$new->baseUnit = $closestUnit;
129+
$new->unit = $closestUnit;
130130

131131
return $new;
132132
}

src/AbstractConverter.php

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ abstract class AbstractConverter implements \Stringable
2424
set(mixed $value) => $this->value = BigDecimal::of($value);
2525
}
2626

27-
public string $baseUnit;
27+
public protected(set) string $unit;
2828

29-
abstract public string $atomUnit {
29+
abstract public protected(set) string $atomUnit {
3030
get;
3131
}
3232

33-
abstract public string $defaultUnit {
33+
abstract public protected(set) string $defaultUnit {
3434
get;
3535
}
3636

@@ -51,6 +51,18 @@ abstract class AbstractConverter implements \Stringable
5151
}
5252
}
5353

54+
public string $baseUnit {
55+
get {
56+
foreach ($this->availableUnitExchanges as $unit => $rate) {
57+
if (BigDecimal::of($rate)->isEqualTo(1)) {
58+
return $unit;
59+
}
60+
}
61+
62+
throw new \RuntimeException('No base unit found in available unit exchanges.');
63+
}
64+
}
65+
5466
protected ?array $availableUnits = null;
5567

5668
public ?\Closure $unitNormalizer = null;
@@ -114,20 +126,20 @@ public function withParse(
114126

115127
$new = $new->withValue($atomValue);
116128

117-
$asUnit ??= $this->baseUnit;
129+
$asUnit ??= $this->unit;
118130

119-
if ($asUnit && $asUnit !== $new->baseUnit) {
131+
if ($asUnit && $asUnit !== $new->unit) {
120132
$asUnit = $this->normalizeUnit($asUnit);
121133
$new = $new->convertTo($asUnit, $scale, $roundingMode);
122134
}
123135

124136
return $new;
125137
}
126138

127-
public function __construct(mixed $value = 0, ?string $baseUnit = null)
139+
public function __construct(mixed $value = 0, ?string $unit = null)
128140
{
129141
$this->value = $value;
130-
$this->baseUnit = $baseUnit ?? $this->defaultUnit;
142+
$this->unit = $unit ?? $this->defaultUnit;
131143
}
132144

133145
public function withAvailableUnits(?array $units): static
@@ -176,17 +188,17 @@ public function convertTo(
176188
): static {
177189
$toUnit = $this->normalizeUnit($toUnit);
178190

179-
if ($toUnit === $this->baseUnit) {
191+
if ($toUnit === $this->unit) {
180192
return $this;
181193
}
182194

183195
$new = clone $this;
184196

185197
if (!$new->value->isZero()) {
186-
$new->value = $this->convertValue($new->value, $new->baseUnit, $toUnit, $scale, $roundingMode);
198+
$new->value = $this->convertValue($new->value, $new->unit, $toUnit, $scale, $roundingMode);
187199
}
188200

189-
$new->baseUnit = $toUnit;
201+
$new->unit = $toUnit;
190202

191203
return $new;
192204
}
@@ -228,28 +240,28 @@ public function withValue(
228240
): static {
229241
$new = clone $this;
230242
$new->value = $value;
231-
$new->baseUnit = $fromUnit ? $new->normalizeUnit($fromUnit) : $this->baseUnit;
243+
$new->unit = $fromUnit ? $new->normalizeUnit($fromUnit) : $this->unit;
232244

233-
if ($new->baseUnit !== $this->baseUnit) {
234-
$new = $new->convertTo($this->baseUnit, $scale, $roundingMode);
245+
if ($new->unit !== $this->unit) {
246+
$new = $new->convertTo($this->unit, $scale, $roundingMode);
235247
}
236248

237249
return $new;
238250
}
239251

240-
public function withBaseUnit(string $unit): static
252+
public function withUnit(string $unit): static
241253
{
242254
$new = clone $this;
243-
$new->baseUnit = $this->normalizeUnit($unit);
255+
$new->unit = $this->normalizeUnit($unit);
244256

245257
return $new;
246258
}
247259

248-
public function with(mixed $value, ?string $baseUnit = null): static
260+
public function with(mixed $value, ?string $unit = null): static
249261
{
250262
$new = clone $this;
251263
$new->value = $value;
252-
$new->baseUnit = $baseUnit ? $this->normalizeUnit($baseUnit) : $this->baseUnit;
264+
$new->unit = $unit ? $this->normalizeUnit($unit) : $this->unit;
253265

254266
return $new;
255267
}
@@ -285,7 +297,7 @@ public function format(
285297
$value = $value->stripTrailingZeros();
286298
}
287299

288-
$unit ??= $this->baseUnit;
300+
$unit ??= $this->unit;
289301
$suffix ??= $unit;
290302

291303
if ($suffix instanceof \Closure) {
@@ -314,7 +326,7 @@ public function isNegative(): bool
314326
/**
315327
* @param string $unit
316328
*
317-
* @return array{ static, static }
329+
* @return array{ static, static } A tuple with [extracted, remainder]
318330
*/
319331
public function withExtract(string $unit): array
320332
{
@@ -325,7 +337,7 @@ public function withExtract(string $unit): array
325337

326338
protected function extract(string $unit): static
327339
{
328-
$rate = $this->with(1, $unit)->convertTo($this->baseUnit)->value;
340+
$rate = $this->with(1, $unit)->convertTo($this->unit)->value;
329341

330342
/** @var BigDecimal $part */
331343
$part = $this->value->dividedBy($rate, 0, RoundingMode::DOWN);
@@ -416,14 +428,16 @@ public function getUnitExchangeRate(string $unit): ?BigNumber
416428

417429
/**
418430
* @param array<BigNumber|float|int> $units
431+
* @param string $atomUnit
419432
* @param string $defaultUnit
420433
*
421434
* @return $this
422435
*/
423-
public function withUnitExchanges(array $units, string $defaultUnit): static
436+
public function withUnitExchanges(array $units, string $atomUnit, string $defaultUnit): static
424437
{
425438
$new = clone $this;
426439
$new->unitExchanges = $units;
440+
$new->atomUnit = $atomUnit;
427441
$new->defaultUnit = $defaultUnit;
428442

429443
return $new;
@@ -573,6 +587,22 @@ public static function unitConstants(): array
573587
return $returnConstants;
574588
}
575589

590+
public function withDefaultUnit(string $defaultUnit): static
591+
{
592+
$new = clone $this;
593+
$new->defaultUnit = $defaultUnit;
594+
595+
return $new;
596+
}
597+
598+
public function withAtomUnit(string $atomUnit): static
599+
{
600+
$new = clone $this;
601+
$new->atomUnit = $atomUnit;
602+
603+
return $new;
604+
}
605+
576606
public function __toString(): string
577607
{
578608
return (string) $this->value->toBigDecimal()->stripTrailingZeros();

src/Compound/AbstractCompoundConverter.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ abstract class AbstractCompoundConverter extends AbstractConverter
2525
get;
2626
}
2727

28-
public string $baseUnit {
28+
public string $unit {
2929
set {
3030
[$measureUnit, $denoUnit] = $this->normalizeAndSplitUnit($value);
3131

32-
$this->baseUnit = $this->normalizeCompoundUnit($value);
33-
$this->measure = $this->measure->withBaseUnit($measureUnit);
32+
$this->unit = $this->normalizeCompoundUnit($value);
33+
$this->measure = $this->measure->withUnit($measureUnit);
3434

3535
if ($denoUnit) {
36-
$this->deno = $this->deno->withBaseUnit($denoUnit);
36+
$this->deno = $this->deno->withUnit($denoUnit);
3737
}
3838
}
3939
}
@@ -89,9 +89,9 @@ public function withParse(
8989

9090
$new = $new->with($atomValue, $atomUnit);
9191

92-
$asUnit ??= $this->baseUnit;
92+
$asUnit ??= $this->unit;
9393

94-
if ($asUnit && $asUnit !== $new->baseUnit) {
94+
if ($asUnit && $asUnit !== $new->unit) {
9595
$asUnit = $this->normalizeUnit($asUnit);
9696
$new = $new->convertTo($asUnit, $scale, $roundingMode);
9797
}
@@ -122,7 +122,7 @@ public function convertTo(
122122
return $new->with($newValue, $toUnit);
123123
}
124124

125-
$fromUnit = $this->normalizeCompoundUnit($this->baseUnit);
125+
$fromUnit = $this->normalizeCompoundUnit($this->unit);
126126

127127
// Direct exchange. For example, mph to km/s.
128128
// Directly use the exchange rate if available.
@@ -168,17 +168,17 @@ public function convertUnitPairTo(
168168
->convertTo($measureUnit, $scale, $roundingMode);
169169

170170
$new = $this->withValue($this->measure->value);
171-
$new->baseUnit = $measureUnit;
171+
$new->unit = $measureUnit;
172172
}
173173

174174
// If we have a denominator unit, we need to convert the value accordingly.
175-
if ($denoUnit && $denoUnit !== $this->deno->baseUnit) {
175+
if ($denoUnit && $denoUnit !== $this->deno->unit) {
176176
// Make the deno as target unit.
177177
$new->deno = $new->deno->with(1, $denoUnit);
178178

179179
// Convert the value to the base unit of the deno.
180180
$new->value = $new->deno->withValue($new->value)
181-
->convertTo($this->deno->baseUnit, $scale, $roundingMode)
181+
->convertTo($this->deno->unit, $scale, $roundingMode)
182182
->value;
183183
}
184184

@@ -228,17 +228,17 @@ public function format(
228228
$addDenoSuffix = $suffix === null;
229229

230230
if (!$suffix) {
231-
$suffix = $unit ?? $this->baseUnit;
231+
$suffix = $unit ?? $this->unit;
232232

233233
if ($this->measure->getUnitExchangeRate($suffix) !== null) {
234-
$suffix .= '/' . $this->deno->formatSuffix($this->deno->baseUnit, $this->value, $this->deno->baseUnit);
234+
$suffix .= '/' . $this->deno->formatSuffix($this->deno->unit, $this->value, $this->deno->unit);
235235
}
236236
}
237237

238238
$text = parent::format($suffix, $unit, $scale, $roundingMode);
239239

240240
if ($addDenoSuffix && $this->measure->getUnitExchangeRate($suffix) !== null) {
241-
$text .= '/' . $this->deno->formatSuffix($this->deno->baseUnit, $this->value, $this->deno->baseUnit);
241+
$text .= '/' . $this->deno->formatSuffix($this->deno->unit, $this->value, $this->deno->unit);
242242
}
243243

244244
return $text;

src/Duration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ public function withFromDateInterval(
152152

153153
$instance = $this->with($microseconds, static::UNIT_MICROSECONDS);
154154

155-
$asUnit ??= $this->baseUnit;
155+
$asUnit ??= $this->unit;
156156

157-
if ($asUnit && $asUnit !== $instance->baseUnit) {
157+
if ($asUnit && $asUnit !== $instance->unit) {
158158
$asUnit = $this->normalizeUnit($asUnit);
159159
$instance = $instance->convertTo($asUnit, $scale, $roundingMode);
160160
}

tests/AbstractUnitConverterTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public function withValue(): void
105105
}
106106

107107
#[Test]
108-
public function withBaseUnit(): void
108+
public function withUnit(): void
109109
{
110110
$d = Duration::from(500, Duration::UNIT_SECONDS)
111-
->withBaseUnit(Duration::UNIT_DAYS);
111+
->withUnit(Duration::UNIT_DAYS);
112112

113113
self::assertEquals(
114114
'500days',
@@ -368,7 +368,7 @@ public function nearest(): void
368368

369369
assertEquals(
370370
FileSize::UNIT_MEBIBYTES,
371-
$f->baseUnit
371+
$f->unit
372372
);
373373

374374
$f = FileSize::from('4360000KiB');
@@ -381,7 +381,7 @@ public function nearest(): void
381381

382382
assertEquals(
383383
FileSize::UNIT_GIBIBYTES,
384-
$f->baseUnit
384+
$f->unit
385385
);
386386

387387
$f = FileSize::from('0.000001245TiB');
@@ -394,7 +394,7 @@ public function nearest(): void
394394

395395
assertEquals(
396396
FileSize::UNIT_MEBIBYTES,
397-
$f->baseUnit
397+
$f->unit
398398
);
399399
}
400400

@@ -415,7 +415,7 @@ public function nearestWithPresetUnits(): void
415415

416416
assertEquals(
417417
FileSize::UNIT_MEBIBYTES,
418-
$f->baseUnit
418+
$f->unit
419419
);
420420
}
421421

tests/FileSizeTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,31 @@ public static function bytesBinaryProvider(): array
8383
];
8484
}
8585

86-
public static function createBytesBinary(mixed $value = 0, ?string $baseUnit = null): FileSize
86+
public static function createBytesBinary(mixed $value = 0, ?string $unit = null): FileSize
8787
{
8888
return new FileSize()
8989
->withOnlyBytesBinary()
90-
->withFrom($value, $baseUnit);
90+
->withFrom($value, $unit);
9191
}
9292

93-
public static function createBitsBinary(mixed $value = 0, ?string $baseUnit = null): FileSize
93+
public static function createBitsBinary(mixed $value = 0, ?string $unit = null): FileSize
9494
{
9595
return new FileSize()
9696
->withOnlyBitsBinary()
97-
->withFrom($value, $baseUnit);
97+
->withFrom($value, $unit);
9898
}
9999

100-
public static function createBytesBase10(mixed $value = 0, ?string $baseUnit = null): FileSize
100+
public static function createBytesBase10(mixed $value = 0, ?string $unit = null): FileSize
101101
{
102102
return new FileSize()
103103
->withOnlyBytesBase10()
104-
->withFrom($value, $baseUnit);
104+
->withFrom($value, $unit);
105105
}
106106

107-
public static function createBitsBase10(mixed $value = 0, ?string $baseUnit = null): FileSize
107+
public static function createBitsBase10(mixed $value = 0, ?string $unit = null): FileSize
108108
{
109109
return new FileSize()
110110
->withOnlyBitsBase10()
111-
->withFrom($value, $baseUnit);
111+
->withFrom($value, $unit);
112112
}
113113
}

0 commit comments

Comments
 (0)