sfolador / measures-for-laravel
Laravel的单元转换工具集合
0.1.8
2024-03-29 15:25 UTC
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0|^7.0|^8.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^1.21|^2.0
- pestphp/pest-plugin-laravel: ^1.1|^2.0|^3.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5|^10.0
README
Laravel的单元转换工具集合
在Laravel中转换度量单位。
安装
您可以通过composer安装此包
composer require sfolador/measures-for-laravel
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="measures-for-laravel-config"
使用方法
use \Sfolador\Measures\Measures; use \Sfolador\Measures\Unit\Length\Length; \Sfolador\Measures\Unit\Weight\Weight; $measure = Measures::length("2.0m"); echo $measures->toCm(); // 200.0 cm //or you can use the Length class directly $length = Length::from("2.0m"); echo $length->toCm(); // 200.0 cm $measure = Measures::weight("2.0Kg"); echo $measures->toG(); // 2000.0 g //or you can use the Weight class directly $length = Weight::from("2.0Kg"); echo $length->toG(); // 2000.0 g
也可以使用扩展的流畅方法
$measure = Length::from("2.0m"); echo $measures->toCentimeters(); // 200 cm //you can chain the methods: echo Length::from("2.0m")->toCentimeters(); // 200 cm
如果您不知道正在处理哪种度量,可以使用Measures
类来自动检测度量类型
$measure = Measures::from("2.0m"); // $measure is an instance of Length echo $measures->toCm(); // 200 cm $measure = Measures::from("2.0Kg"); // $measure is an instance of Weight echo $measures->toG(); // 2000 g
Eloquent类型转换
可以将模型属性转换为度量
use \Sfolador\Measures\Unit\Weight\Weight; use Sfolador\Measures\Cast\Measure; class Product extends Model { protected $casts = [ 'weight' => Measure::class, 'length' => Measure::class, ]; } $product = Product::first(); echo $product->weight->toKg(); // 2 Kg echo $product->length->toCm(); // 200 cm $product->weight = Weight::from("3.0Kg"); $product->length = Length::from("1.0m"); $product->save(); echo $product->weight->toKg(); // 3 Kg echo $product->length->toCm(); // 100 cm
可用单位
长度
- 毫米
- 厘米
- 米
- 千米
- 英寸
- 英尺
- 码
- 英里
- 海里
重量
- 毫克
- 克
- 千克
- 吨
- 盎司
- 磅
- 英石
- 长吨
- 短吨
体积
- 毫升
- 升
- 立方米
- 立方英寸
- 立方英尺
- 加仑
- 品脱
- 杯
温度
- 摄氏度
- 华氏度
- 开尔文
面积
- 平方米
- 平方千米
- 平方厘米
- 平方毫米
- 平方英寸
- 平方英尺
- 平方码
- 平方英里
- 英亩
- 公顷
数据
- 比特
- 字节
- 千比特
- 千字节
- 兆比特
- 兆字节
- 吉比特
- 吉字节
- 太比特
- 太字节
- 拍比特
- 拍字节
- 基比特
- 基字节
- 梅比特
- 梅字节
- 吉比特
- 吉字节
- 泰比特
- 泰字节
- 佩比特
- 佩字节
速度
- 每秒米
- 每小时千米
- 每小时英里
- 节
- 每秒英尺
- 马赫数
时间
- 纳秒
- 微秒
- 毫秒
- 秒
- 分钟
- 小时
- 天
- 周
- 月
- 年
压力
- 帕斯卡
- 千帕斯卡
- 巴
- 毫巴
- 大气压
- 托
- 每平方英寸磅
- 水银柱毫米
能量
- 焦耳
- 千焦耳
- 兆焦耳
- 吉焦耳
- 瓦时
- 千瓦时
- 兆瓦时
- 吉瓦时
- 卡路里
- 千卡路里
- 兆卡路里
- 吉卡路里
- 电子伏特
- 千电子伏特
- 兆电子伏特
- 吉电子伏特
角度
- 度
- 弧度
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。