teamchallengeapps / distance
PHP 距离(值对象)助手
1.3.0
2023-04-03 14:33 UTC
Requires
- php: ^7.2|^8.0.2
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
## 关于
此距离助手包包含一个经过测试的PHP值对象,这使得处理、比较、转换和格式化距离(米、千米和步数)变得简单流畅。
该包的灵感来自PHP助手,如Carbon,以及重构虚拟办公场所步行挑战系统Big Team Challenge背后的代码的努力。
安装
您可以通过composer引入此包
composer require teamchallengeapps/distance
该包(尤其是配置)旨在与Laravel 5一起工作。在config/app.php
中包含我们的自定义服务提供者
'providers' => [ 'TeamChallengeApps\Distance\DistanceServiceProvider' ];
使用
要创建一个新的距离,只需实例化Distance类。
use TeamChallengeApps\Distance\Distance; $meters = new Distance(100, 'meters'); $km = new Distance(10.5, 'kilometers'); $miles = new Distance(10, 'miles'); $steps = new Distance(10000, 'footsteps');
默认距离是米,因此省略第二个(可选)构造函数参数将默认为米
$meters = new Distance(100);
API
转换
您可以使用to
方法将距离对象转换为新的单位。
$meters = new Distance(1000); $km = $meters->toKilometers(); echo $km->value; // 1
以下方法内置
toMeters()
toKilometers()
toMiles()
toFootsteps()
toSteps()
(别名)
如果您只想获取转换,而不更改对象,您可以使用asUnit
方法。
$meters = new Distance(1000); echo $meters->asUnit('kilometers'); // 1 echo $meters->value; // 1000
四舍五入
每个单位都有自己的小数精度,您可以使用round
方法获取四舍五入的格式。
$meters = new Distance(1000.995); echo $meters->value; // 1000.995 echo $meters->round(); // 1001.00
比较
空/零
$distance new Distance(0); if ($distance->isEmpty()) { // } if ($distance->isZero()) { }
值比较
$distance = new Distance(10); $total = new Distance(100); if ($distance->lt($total)) { // Less than } if ($distance->lte($total)) { // Less than or equal } if ($distance->gt($total)) { // Greater than } if ($distance->gte($total)) { // Greater than or equal }
百分比
$distance = new Distance(10); $total = new Distance(100); $percentage = $distance->percentageOf($total); // 10
默认情况下,百分比限制在100%,但通过将false
作为第二个参数传递,将始终返回真实百分比。
$distance = new Distance(150); $total = new Distance(100); $percentage = $distance->percentageOf($total); // 100 $percentage = $distance->percentageOf($total, false); // 150
修改
您可以添加或减去距离
$total = new Distance(1000); $logged = new Distance(10); $total->increment($logged); echo $total->value; // 1010
$total = new Distance(1010); $redeemed = new Distance(10); $total->decrement($logged); echo $total->value; // 1000
格式化
使用PHP的魔术__toString()
方法,echoing或cast对象本身将四舍五入并使用number_format
函数返回值的字符串表示形式。
$distance = new Distance(100500.591); echo $distance; // 10,500.59 $value = (string) $distance; echo $value; // 10,500.59
您可以将默认格式化选项更改为包括/省略逗号和单位后缀。使用以下命令发布配置文件
php artisan vendor:publish --provider="TeamChallengeApps\Distance\DistanceServiceProvider" --tag="config"
return [ 'format' => [ 'comma' => true, 'suffix' => false, ]; ];
您还可以使用toStringWithSuffix
方法强制在末尾添加后缀,例如
$meters = new Distance(100, 'meters'); echo $meters->toStringWithSuffix(); // 1000 m $km = new Distance(10.5, 'kilometers'); echo $km->toStringWithSuffix(); // 1000 km $miles = new Distance(10, 'miles'); echo $miles->toStringWithSuffix(); // 1000 mi. $steps = new Distance(10000, 'footsteps'); echo $steps->toStringWithSuffix(); // 1000 steps
贡献
请提交改进和修复 :)
变更日志
查看此包的CHANGELOG.md。