-
Notifications
You must be signed in to change notification settings - Fork 2
Implement formatters for common units #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6 +/- ##
============================================
+ Coverage 99.31% 99.42% +0.10%
- Complexity 141 162 +21
============================================
Files 14 21 +7
Lines 294 349 +55
============================================
+ Hits 292 347 +55
Misses 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements unit formatters that perform unit promotion within measurement systems (metric and imperial). The formatters automatically select the most appropriate unit representation based on magnitude, converting values like 0.1m to 10cm or 60s to 1min.
Changes:
- Added
UnitPromotertrait providing shared promotion logic for all unit formatters - Implemented 6 new formatters:
TimeFormatter,MetricFormatter(length),MassFormatter(metric),ImperialLengthFormatter,ImperialMassFormatter, andImperialAreaFormatter - Updated
BuilderandChaininterfaces to support the new formatters
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Internal/UnitPromoter.php | Core trait implementing unit promotion logic with ratio-based conversion |
| src/TimeFormatter.php | Time unit formatter supporting ns to millennia |
| src/MetricFormatter.php | Metric length formatter (mm, cm, m, km) |
| src/MassFormatter.php | Metric mass formatter (mg, g, kg, t) |
| src/ImperialLengthFormatter.php | Imperial length formatter (in, ft, yd, mi) |
| src/ImperialMassFormatter.php | Imperial mass formatter (oz, lb, st, ton) |
| src/ImperialAreaFormatter.php | Imperial area formatter (in², ft², yd², ac, mi²) |
| src/Mixin/Builder.php | Added builder methods for new formatters |
| src/Mixin/Chain.php | Added chain methods for new formatters |
| tests/Unit/*FormatterTest.php | Comprehensive unit tests for all formatters |
| docs/*Formatter.md | Documentation for each new formatter |
| README.md | Updated formatter listing table |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Includes formatters that perform unit promotion. We DO NOT do
unit conversion (for example, inches to centimeters), we only
choose within the appropriate system (metric or imperial) the
best representation for a given value.
For example, saying `10cm` is much better than saying `0.1m`.
Both are still within the metric system.
Considerations made during this change:
- We only use unit symbols, not the full names. This simplifies
aspects such as translations, since most units are universal
but their localized full names are not. There is some space
for later accepting full names (new MetricFormatter('meter'))
and deciding the output representation automatically. This is
also the reason why PHP enums were not used as the source of
truth for supported units.
- Time unit symbols are *not universal*, but they were considered
for inclusion for their value in technical systems (simplifying
a log entry or measure).
- The SI (colloquially known as "metric system") is the widest
adopted standard, so only "Imperial" was prefixed. We used the
"Metric" name to denote SI Length units.
Includes formatters that perform unit promotion. We DO NOT do unit conversion (for example, inches to centimeters), we only choose within the appropriate system (metric or imperial) the best representation for a given value.
For example, saying
10cmis much better than saying0.1m. Both are still within the metric system.Considerations made during this change: