|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ardenexal\FHIRTools\Component\Metadata\Ucum; |
| 6 | + |
| 7 | +/** |
| 8 | + * Minimal UCUM unit conversion shared across components. |
| 9 | + * |
| 10 | + * Converts a recognised UCUM code to a canonical base unit so two quantities of the same |
| 11 | + * physical dimension can be compared. This is intentionally a small, hand-maintained table — |
| 12 | + * NOT a full UCUM engine — covering the units the test corpus and FHIRPath comparisons exercise |
| 13 | + * (mass, length, volume, and the day/week duration pair). Unknown codes return null, which callers |
| 14 | + * treat as "not commensurable / cannot compare". |
| 15 | + * |
| 16 | + * Calendar-duration keyword semantics (year/month approximations, the FHIRPath month↔year |
| 17 | + * incomparability rule) deliberately live in the FHIRPath ComparisonService, not here: those are |
| 18 | + * FHIRPath temporal rules, not UCUM unit conversion. |
| 19 | + */ |
| 20 | +final class UcumConverter |
| 21 | +{ |
| 22 | + public const string SYSTEM_URL = 'http://unitsofmeasure.org'; |
| 23 | + |
| 24 | + /** |
| 25 | + * UCUM code => canonical base unit + multiplicative factor to that base. |
| 26 | + */ |
| 27 | + private const array CONVERSIONS = [ |
| 28 | + '1' => ['base' => '1', 'factor' => 1.0], |
| 29 | + 'kg' => ['base' => 'kg', 'factor' => 1.0], |
| 30 | + 'g' => ['base' => 'kg', 'factor' => 0.001], |
| 31 | + 'mg' => ['base' => 'kg', 'factor' => 0.000001], |
| 32 | + '[lb_av]' => ['base' => 'kg', 'factor' => 0.45359237], |
| 33 | + 'm' => ['base' => 'm', 'factor' => 1.0], |
| 34 | + 'cm' => ['base' => 'm', 'factor' => 0.01], |
| 35 | + 'mm' => ['base' => 'm', 'factor' => 0.001], |
| 36 | + 'km' => ['base' => 'm', 'factor' => 1000.0], |
| 37 | + '[in_i]' => ['base' => 'm', 'factor' => 0.0254], |
| 38 | + '[ft_i]' => ['base' => 'm', 'factor' => 0.3048], |
| 39 | + '[mi_i]' => ['base' => 'm', 'factor' => 1609.344], |
| 40 | + 'L' => ['base' => 'L', 'factor' => 1.0], |
| 41 | + 'mL' => ['base' => 'L', 'factor' => 0.001], |
| 42 | + 'wk' => ['base' => 'd', 'factor' => 7.0], |
| 43 | + 'd' => ['base' => 'd', 'factor' => 1.0], |
| 44 | + ]; |
| 45 | + |
| 46 | + /** |
| 47 | + * Convert a value in the given UCUM code to its canonical base unit. |
| 48 | + * |
| 49 | + * Returns null when the code is not in the conversion table. |
| 50 | + * |
| 51 | + * @return array{base: string, value: float}|null |
| 52 | + */ |
| 53 | + public function toBase(string $code, float $value): ?array |
| 54 | + { |
| 55 | + $definition = self::CONVERSIONS[$code] ?? null; |
| 56 | + if ($definition === null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + return [ |
| 61 | + 'base' => $definition['base'], |
| 62 | + 'value' => $value * $definition['factor'], |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Whether the given UCUM code is one this converter can convert (i.e. present in the table). |
| 68 | + * |
| 69 | + * Lets callers distinguish "unit not recognised by this minimal converter" from "units are of |
| 70 | + * different physical dimensions" when {@see compare()} returns null — the two are not the same |
| 71 | + * diagnosis, even though both block a comparison. |
| 72 | + */ |
| 73 | + public function knows(string $code): bool |
| 74 | + { |
| 75 | + return isset(self::CONVERSIONS[$code]); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Whether two UCUM codes belong to the same physical dimension (both recognised and sharing a |
| 80 | + * canonical base unit), i.e. their quantities can be ordered against one another. |
| 81 | + */ |
| 82 | + public function areCommensurable(string $codeA, string $codeB): bool |
| 83 | + { |
| 84 | + $a = self::CONVERSIONS[$codeA] ?? null; |
| 85 | + $b = self::CONVERSIONS[$codeB] ?? null; |
| 86 | + |
| 87 | + return $a !== null && $b !== null && $a['base'] === $b['base']; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Compare two quantities by their UCUM codes after converting to a common base unit. |
| 92 | + * |
| 93 | + * Returns the sign of (a - b) as -1, 0, or 1, or null when the units are not commensurable |
| 94 | + * (different dimension, or either code unrecognised) and therefore cannot be ordered. |
| 95 | + */ |
| 96 | + public function compare(float $valueA, string $codeA, float $valueB, string $codeB): ?int |
| 97 | + { |
| 98 | + $a = $this->toBase($codeA, $valueA); |
| 99 | + $b = $this->toBase($codeB, $valueB); |
| 100 | + |
| 101 | + if ($a === null || $b === null || $a['base'] !== $b['base']) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + return $a['value'] <=> $b['value']; |
| 106 | + } |
| 107 | +} |
0 commit comments