khaledalam/unit
Composer 安装命令:
composer require khaledalam/unit
包简介
A lightweight, type-safe PHP library for units, quantities, and dimensional analysis with automatic conversion.
关键字:
README 文档
README
Type-safe units, quantities & dimensional analysis for PHP
A lightweight, immutable, type-safe PHP library for working with quantities, units, and dimensional analysis. Define units, convert across compatible ones, and do arithmetic that refuses to add meters to seconds.
use KhaledAlam\Unit\Quantity; echo Quantity::of(2, 'm')->add(Quantity::of(100, 'cm'))->to('m'); // "3 m" echo Quantity::of(100, '°C')->to('°F')->format(1); // "212.0 °F" echo Quantity::of(4, 'm')->multiply(Quantity::of(3, 'm')); // "12 m²"
No setup, no registration — common units are ready the moment you install.
▶️ Try the live demo — an interactive converter running in your browser.
Why Unit?
| Unit | Hand-rolled * factor |
Raw floats | |
|---|---|---|---|
Blocks nonsensical math (m + s) |
✅ | ❌ | ❌ |
| Auto-converts compatible units | ✅ | ❌ | ❌ |
| Affine temperature scales (°C/°F/K) | ✅ | ⚠️ easy to get wrong | ❌ |
Derived units (m/s, m²) |
✅ | ❌ | ❌ |
| Immutable value objects | ✅ | — | ❌ |
| Zero dependencies | ✅ | ✅ | ✅ |
If you've ever shipped a bug because someone stored centimeters in a "meters" column, this library is for you.
Installation
composer require khaledalam/unit
Requires PHP 8.2+.
Quick start
<?php require __DIR__ . '/vendor/autoload.php'; use KhaledAlam\Unit\Quantity; // Common units are auto-registered — just use them. $length1 = Quantity::of(2.0, 'm'); $length2 = Quantity::of(100.0, 'cm'); $sum = $length1->add($length2); // auto-converts cm -> m echo $sum->to('m'); // "3 m"
Features
- ✅ Immutable value objects
- ✅ Dimensionally-aware arithmetic (
add,subtract,multiply,divide) - ✅ Automatic conversion between compatible units (e.g. cm → m)
- ✅ Correct affine temperature conversion (°C ↔ °F ↔ K)
- ✅ Derived/compound units rendered as
m/s,m²,m·s - ✅ Scalar math (
times,dividedBy,pow,sqrt,abs,negate) - ✅ Named dimensions (
dimensionName()→"velocity") - ✅ Comparisons (
equals,isGreaterThan,isLessThan) - ✅
JsonSerializableoutput, Laravel cast & Doctrine type - ✅ Enum-powered unit naming and a custom unit registry
- ✅ Zero runtime dependencies
Usage
Conversion
echo Quantity::of(2, 'm')->to('cm'); // "200 cm" echo Quantity::of(5, 'km')->to('m'); // "5000 m" echo Quantity::of(1, 'h')->to('s'); // "3600 s"
Arithmetic
$speed = Quantity::of(10, 'm')->divide(Quantity::of(2, 's')); // "5 m/s" $area = Quantity::of(2, 'm')->multiply(Quantity::of(3, 'm')); // "6 m²"
All operations return new Quantity objects with the proper dimension and unit.
Scalar math
echo Quantity::of(5, 'm')->times(3); // "15 m" echo Quantity::of(10, 'm')->dividedBy(4); // "2.5 m" echo Quantity::of(2, 'm')->pow(2); // "4 m²" echo Quantity::of(4, 'm²')->sqrt(); // "2 m" echo Quantity::of(-3, 'm')->abs(); // "3 m"
Dimension names
Quantity::of(1, 'm/s')->dimensionName(); // "velocity" Quantity::of(1, 'N')->dimensionName(); // "force" Quantity::of(1, 'Pa')->dimensionName(); // "pressure"
Temperature (affine scales)
echo Quantity::of(100, '°C')->to('°F')->format(1); // "212.0 °F" echo Quantity::of(32, '°F')->to('°C')->format(1); // "0.0 °C" echo Quantity::of(25, '°C')->to('K')->format(2); // "298.15 K"
Parsing strings
echo Quantity::parse('100 km/h')->to('mph')->format(2); // "62.14 mph" echo Quantity::parse('-40 °C')->to('°F')->format(1); // "-40.0 °F" echo Quantity::parse('5 ft 3 in')->to('cm')->format(2); // "160.02 cm" (segments summed)
Humanize (auto-pick the best unit)
echo Quantity::of(1500, 'm')->humanize(); // "1.5 km" echo Quantity::of(2500000, 'B')->humanize(); // "2.5 MB" echo Quantity::of(5400, 's')->humanize(); // "1.5 h"
Comparison
Quantity::of(1, 'm')->isGreaterThan(Quantity::of(90, 'cm')); // true Quantity::of(1, 'm')->equals(Quantity::of(100, 'cm')); // true
Precision & formatting
echo Quantity::of(1, 'm')->divide(Quantity::of(3, 's'))->format(2); // "0.33 m/s"
JSON
echo json_encode(Quantity::of(2, 'm')); // {"value":2,"unit":"m"}
Custom units
Register your own units against a dimension:
use KhaledAlam\Unit\{Unit, Name, Dimension, UnitRegistry}; UnitRegistry::register(new Unit('yard', 'yd', 0.9144, new Dimension(['L' => 1]))); echo Quantity::of(1, 'yd')->to('m'); // "0.9144 m"
Incompatible operations throw
Quantity::of(5, 'kg')->add(Quantity::of(3, 's')); // InvalidArgumentException
Supported units
| Dimension | Units |
|---|---|
| Length | mm, cm, m, km, in, ft, yd, mi |
| Mass | mg, g, kg, t, lb, oz |
| Time | ms, s, min, h, d, wk |
| Area | cm², m², km², ft², ha, acre |
| Volume | mL, L, m³, gal |
| Temperature | °C, °F, K |
| Speed | m/s, km/h, mph, kn |
| Data | bit, B, KB, MB, GB, TB, KiB, MiB, GiB, TiB |
| Force | N, kN |
| Energy | J, kJ, cal, kcal, Wh, kWh |
| Power | W, kW, hp |
| Pressure | Pa, kPa, bar, atm, psi |
| Frequency | Hz, kHz, MHz, GHz |
| Angle | rad, deg, grad, turn |
Need more? Open an issue or register your own.
Laravel integration
The package ships a service provider (auto-discovered) and an Eloquent cast. Store a
quantity in a single column and get a Quantity back on access:
use Illuminate\Database\Eloquent\Model; use KhaledAlam\Unit\Laravel\AsQuantity; class Parcel extends Model { protected function casts(): array { return ['weight' => AsQuantity::class]; } } $parcel->weight = Quantity::of(2.5, 'kg'); // stored as "2.5 kg" $parcel->weight->to('g'); // 2500 g $parcel->weight = '5 ft 3 in'; // strings are parsed on the way in
Register app-specific units via config/unit.php:
return [ 'units' => [ new \KhaledAlam\Unit\Unit('furlong', 'fur', 201.168, new \KhaledAlam\Unit\Dimension(['L' => 1])), ], ];
Requires
illuminate/database(already present in any Laravel app).
Symfony / Doctrine integration
A Doctrine DBAL type persists a Quantity as a string column. Register it once, then
map any column with it:
use Doctrine\DBAL\Types\Type; use KhaledAlam\Unit\Doctrine\QuantityType; Type::addType(QuantityType::NAME, QuantityType::class); // e.g. in a bundle boot / bootstrap
use Doctrine\ORM\Mapping as ORM; #[ORM\Column(type: 'quantity', nullable: true)] public ?Quantity $weight = null;
Strings assigned to the property are parsed on write, and the column hydrates back into a
Quantity on read.
Requires
doctrine/dbal^4.
Examples
Runnable scripts live in examples/:
php examples/basic.php php examples/temperature.php php examples/parse-and-humanize.php php examples/scalar-math.php php examples/shipping.php php examples/physics.php
Testing & quality
composer test # PHPUnit composer analyse # PHPStan (level max) composer cs # PHP-CS-Fixer (dry run)
The suite combines PHPUnit test classes with .phpt scenario tests and holds 100%
line coverage, verified on PHP 8.2, 8.3, and 8.4 in CI (PHPStan level max, PSR-12).
Contributing
Contributions are welcome! Please read CONTRIBUTING.md and the Code of Conduct. Adding a unit is usually a three-line change.
License
MIT © Khaled Alam
khaledalam/unit 适用场景与选型建议
khaledalam/unit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 07 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「metric」 「units」 「measurement」 「physics」 「quantity」 「imperial」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 khaledalam/unit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 khaledalam/unit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 khaledalam/unit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The "View Counter" bundle
Additional Checks for Laravel Health (Spatie)
A small client implementing the Measurement Protocol for Google Analytics 4.
Physical quantities and formulas
Measurement unit management for Laravel Enso
Packaging unit management for Laravel Enso
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-05