pointybeard/laravel-unit-converter
Composer 安装命令:
composer require pointybeard/laravel-unit-converter
包简介
A Laravel package for conversion between simple unit types.
README 文档
README
A Laravel package for conversion between simple unit types e.g. G to KG, IN to CM
Installation
This library is installed via Composer. To install, use composer require pointybeard/laravel-unit-converter.
Requirements
This library requires PHP 8.0 or greater.
Usage
The UnitConverter facade allows you to perform conversions with a simple and intuitive fluent interface.
<?php use pointybeard\LaravelUnitConverter\Facades\UnitConverter; // 25 Celsius to Fahrenheit $result = UnitConverter::convert(25)->from('c')->to('f'); echo $result; // 77 // 2.5 Kilograms to Pounds $result = UnitConverter::convert(2.5)->from('kg')->to('lb'); echo $result; // 5.51156 // 1,000 Millimeters to Meters $result = UnitConverter::convert(1000)->from('mm')->to('m'); echo $result; // 1
If you attempt to convert between unsupported units, an exception will be thrown:
<?php use pointybeard\LaravelUnitConverter\Facades\UnitConverter; try { UnitConverter::convert(10)->from('abc')->to('xyz'); } catch (\InvalidArgumentException $e) { echo $e->getMessage(); // Unsupported unit: abc }
Adding Calculators
You can extend the package by registering custom calculators to handle additional unit conversions.
<?php use Pointybeard\LaravelUnitConverter\UnitConverter; use Pointybeard\LaravelUnitConverter\Calculators\CalculatorInterface; class CustomCalculator implements CalculatorInterface { protected function getConversions(): array return [ 'custom1' => [ 'custom2' => 2.0, // Conversion factor: 1 custom1 = 2 custom2 ], ]; } } // Register the custom calculator UnitConverter::registerCalculator(new CustomCalculator); // Usage echo UnitConverter::convert(10)->from('custom1')->to('custom2'); // 20
If your unit conversions require more complex logic (e.g., temperature conversions), you can create a method-based calculator by extending the AbstractCalculator. The conversion logic is defined in specific methods, and getConversions() maps unit pairs to these methods.
<?php use Pointybeard\LaravelUnitConverter\Calculators\AbstractCalculator; use Pointybeard\LaravelUnitConverter\UnitConverter; class CustomCalculator extends AbstractCalculator { protected function getConversions(): array { return [ 'custom1' => [ 'custom2' => 'customMethod', ], ]; } private function customMethod(float $value): float { return $value * 45; } } // Register the CustomCalculator UnitConverter::registerCalculator(CustomCalculator::class); // Usage echo UnitConverter::convert(10)->from('custom1')->to('custom2'); // 450
Support
If you believe you have found a bug, please report it using the GitHub issue tracker.
License
"Laravel Unit Converter" is released under the MIT License.
pointybeard/laravel-unit-converter 适用场景与选型建议
pointybeard/laravel-unit-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 910 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pointybeard/laravel-unit-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pointybeard/laravel-unit-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 910
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-11