diversified-design/mesuraphp
Composer 安装命令:
composer require diversified-design/mesuraphp
包简介
Strongly-typed measurement units and conversions for PHP.
关键字:
README 文档
README
A PHP library for representing, converting, and formatting physical measurements — from temperature and pressure to length, weight, and volume — with full SI metric prefix support and a fluent, type-safe API.
Installation
composer require diversified-design/mesura-php
Available Units
| Domain | Available unit |
|---|---|
| Angle | Degree, Radian |
| Area | Acre, Hectare, SquareFoot, SquareKilometer, SquareMeter, SquareMile + all metric prefixes (see below) |
| ArealDensity | GramPerSquareMeter, KilogramPerSquareMeter, OuncePerSquareYard, PoundPerSquareFoot |
| Energy | BritishThermalUnit, Calorie, FootPound, Joule, Kilocalorie, KilowattHour, WattHour + all metric prefixes (see below) |
| Irradiance | BtuPerHourPerSquareFoot, KilowattPerSquareMeter, WattPerSquareMeter |
| Length | Centimeter, Fathom, Foot, Furlong, HorseLength, Inch, Kilometer, Meter, Millimeter, NauticalMile, StatuteMile, SurveyMile, Thou, Yard + all metric prefixes (see below) |
| MassConcentration | GrainPerCubicFoot, GrainPerCubicMeter, GramPerCubicMeter, KilogramPerCubicMeter, MicrogramPerCubicMeter, MilligramPerCubicMeter |
| Percentage | Percent, PartsPerMillion, PartsPerBillion |
| Power | BtuPerHour, CaloriePerSecond, FootPoundPerSecond, Horsepower, Watt + all metric prefixes (see below) |
| Pressure | Bar, Hectopascal, Kilopascal, Millibar, MillimetreOfMercury, Pascal, PoundPerSquareInch, StandardAtmosphere, Torr |
| SpecificEnergy | BtuPerPound, CaloriePerGram, JoulePerKilogram, KilojoulePerKilogram, MegajoulePerKilogram |
| Speed | KilometerPerHour, Knot, MeterPerSecond, MilesPerHour |
| Temperature | Celsius, Fahrenheit, Kelvin, Rankine |
| Time | Day, Hour, Minute, Second |
| Torque | NewtonMeter |
| Volume | CubicInch, CubicMeter, CubicYard, FluidDram, FluidOunce, ImperialFluidDram, ImperialFluidOunce, ImperialPint, ImperialQuart, Liter, Pint, Quart, TableSpoon + all metric prefixes (see below) |
| Weight | Gram, Kilogram, MetricTon, Pound + all metric prefixes (see below) |
All the units of a domain can be converted to each other with corresponding methods.
Full SI Metric Prefix Support
Length, Weight, Volume (liters), Area (square meters), Energy (joules), and Power (watts) support all 24 SI metric prefixes — from Quetta (10^30) down to Quecto (10^-30). For example, Length includes Quettameter, Megameter, Kilometer, Centimeter, Nanometer, Quectometer, and everything in between. The same applies to the other metric dimensions (e.g. Milligram, Kiloliter, SquareCentimeter, Megajoule, Gigawatt).
Common prefixed units have dedicated convenience methods (e.g. ->toNanometer(), ->toMilligram()). All others are accessible via ->toUnit(Yottameter::class).
Unit System Classification
Every unit exposes its measurement system via unitSystem():
use Mesura\Length\Meter; use Mesura\Temperature\Celsius; use Mesura\Volume\Pint; Meter::unitSystem(); // UnitSystem::SI Celsius::unitSystem(); // UnitSystem::Metric Pint::unitSystem(); // UnitSystem::USCustomary
Available systems: SI, Metric, Imperial, USCustomary, Nautical, Dimensionless, Other.
See ./examples/unit_system.php for filtering and grouping examples.
Basic Usage
Set the value of the measurement unit directly
$celsius = new Celsius(28); # $celsius = (Mesura\Temperature\Celsius) 28
Convert to another unit in the same domain:
$fahrenheit = $celsius->toFahrenheit(); # $fahrenheit = (Mesura\Temperature\Fahrenheit) 82.4
See many more examples in ./examples/fluent_conversions.php & ./examples/complete_example.php
Fluent Usage
The same example as above, but fluently
$fahrenheit = (new Celsius(28))->toFahrenheit();
See more examples in ./examples/fluent_conversions.php
Formatting output
Formatting methods use sprintf()
See the examples in ./examples/formatting.php
Setting Custom Symbols
All units have default symbols already assigned, but you can set custom ones per instance or per lifecycle.
See the examples in ./examples/symbol_manipulation.php
You can also customize all units up-front in one go using the Customize class' ::unitSymbols() static method.
See the examples in ./examples/customize_units.php
Credit Due
This codebase is a much expanded and heavily modified "hard" fork of the excellent PrinsFrank/measurement-unit.
It adds several utility methods, many more units across more domains, and relies on Brick\Math for all conversions.
LLMs, Coding Agents, etc…
Yes, this work relies heavily on such tools. It is however not "vibe coded" nor "one-shotted."
As an artisan, I use the tools available as tools: to help me make better things, and learn faster.
Did I write every line of code? No. Do I understand every single part of it. No, but most of it.
I did (re)architect and (re)design it with my own knowledge, experience and needs, discussing with and instructing agents as I would have with a team of engineers.
Issues and Pull Requests from fellow humans are always welcome.
I hope you find this library as useful and pleasurable to use as I do.
diversified-design/mesuraphp 适用场景与选型建议
diversified-design/mesuraphp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「conversion」 「speed」 「metric」 「temperature」 「units」 「area」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 diversified-design/mesuraphp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 diversified-design/mesuraphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 diversified-design/mesuraphp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Additional Checks for Laravel Health (Spatie)
Physical quantities and formulas
Integrate New Relic into Symfony2
Fixer.io data provider for Peso
Speedtest.net for PHP | Library and command line interface
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-24