hao-li/laravel-amount
Composer 安装命令:
composer require hao-li/laravel-amount
包简介
关键字:
README 文档
README
背景
系统中涉及到金额的字段,View 层表现的时候一般都是以元为单位使用小数形式展示,不过 Domain 层存储时从空间、性能、容错角度出发,经常以分为单位,用整型来存储。
在 Lavarel 中,可以在 Model 中添加属性方法进行转换
public function getAmountAttribute($value) { return $value / 100; } public function setAmountAttribute($value) { $this->attributes['amount'] = (int)($value * 100); }
不过涉及金额的字段比较多时就需要定义很多相同逻辑的函数,本项目即将该逻辑抽出为 Trait,简化金额字段相关的处理。
除了金额外,小数位数固定的面积、长度等场景使用起来也很方便。
原理
将转换逻辑封装在 AmountTrait 中,覆写 Model 类的 getMutatedAttributes, mutateAttributeForArray, getAttributeValue 及 setAttribute 方法,当访问相关字段时自动进行转换处理。
public function getMutatedAttributes() { $attributes = parent::getMutatedAttributes(); return array_merge($attributes, $this->getAmountFields()); } protected function mutateAttributeForArray($key, $value) { return (in_array($key, $this->getAmountFields())) ? $value / $this->getAmountTimes($key) : parent::mutateAttributeForArray($key, $value); } public function getAttributeValue($key) { $value = parent::getAttributeValue($key); if (in_array($key, $this->getAmountFields())) { $value = $value / $this->getAmountTimes($key); } return $value; } public function setAttribute($key, $value) { if (in_array($key, $this->getAmountFields())) { $value = (int) round($value * $this->getAmountTimes($key)); } parent::setAttribute($key, $value); } public function getAmountFields() { return (property_exists($this, 'amountFields')) ? $this->amountFields : []; } public function getAmountTimes($key) { $ret = 100; if (property_exists($this, 'amountTimes')) { if (is_array($this->amountTimes) && array_key_exists($key, $this->amountTimes)) { $ret = $this->amountTimes[$key]; } elseif (is_numeric($this->amountTimes)) { $ret = $this->amountTimes; } } return $ret; }
依赖
Laravel >= 5.2
安装
composer require "hao-li/laravel-amount:dev-master"
使用
- 在 Model 中引用 AmountTrait
use HaoLi\LaravelAmount\Traits\AmountTrait;
- 使用 AmountTrait
use AmountTrait;
- 定义金额字段(本例中为 amount)
protected $amountFields = ['amount'];
- 通过
$amountTimes指定金额字段的倍数(可选,默认 100)
- 各金额字段使用相同的倍数
protected $amountTimes = 100;
- 不同金额字段设置不同倍数
protected $amountTimes = [ 'amount' => 100, ]
- 完成
之后读取 amount 字段时,该字段的内容会自动从数据库的分转换为元,向其赋值时反之从元转换为分。
FAQ
和别的 trait 中方法冲突
以 setRawAttributes 为例(此为之前方案,目前并未覆写此方法,仅为举例,其他方法原理相同)
- 将冲突的方法分别重命名
use AmountTrait, BTrait { AmountTrait::setRawAttributes as amountTraitSetRawAttributes; BTrait::setRawAttributes as BTraitSetRawAttributes; }
- 在 Model 中定义该冲突的方法,根据情况分别调用别名方法
public function setRawAttributes(array $attributes, $sync = false) { $this->BTraitSetRawAttributes($attributes, $sync); $attributes = $this->getAttributes(); $this->amountTraitSetRawAttributes($attributes, $sync); }
注意这里 $attributes 可能已被改变,所以再次使用时要重新取得最新值
hao-li/laravel-amount 适用场景与选型建议
hao-li/laravel-amount 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.09k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2016 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hao-li/laravel-amount 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hao-li/laravel-amount 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hao-li/laravel-amount 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 3.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-09-17