MIT License

项目介绍

akaunting/laravel-money 是一个托管在 GitHub 上的开源项目,主要使用 PHP 编写,采用 MIT License 许可证,目前获得 789 个 Star、108 次 Fork。

仓库全名
akaunting/laravel-money
开源许可证
MIT License
Star 数
789
Fork 数
108

README 摘要

Currency formatting and conversion package for Laravel This package intends to provide tools for formatting and conversion of monetary values in an easy, yet powerful way for Laravel projects. Why not use the moneyphp package? Because it uses the intl extension for number formatting. intl extension isn't present by default on PHP installs and can give different results in different servers. Getting Started 1…

完整内容见 GitHub 原始 README 或本页 。

常见问题

laravel-money 用什么语言开发?

主要使用 PHP 编写,采用 MIT License 许可证,目前有 789 个 Star。

laravel-money 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 laravel-money 的源码?

可以直接克隆仓库:git clone https://github.com/akaunting/laravel-money.git,默认分支为 master;安装与运行步骤见仓库 README。

laravel-money 最近还在维护吗?

仓库最近一次更新为 3月24日,当前没有未关闭的 Issue;Star 数为 789,可作为社区活跃度的参考。

关于

Currency formatting and conversion package for Laravel

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
1020
价值分
507
活跃状态
活跃

语言占比

Blade
PHP

Release

README

Currency formatting and conversion package for Laravel

Downloads Tests StyleCI License

This package intends to provide tools for formatting and conversion of monetary values in an easy, yet powerful way for Laravel projects.

Why not use the moneyphp package?

Because it uses the intl extension for number formatting. intl extension isn’t present by default on PHP installs and can give different results in different servers.

Getting Started

1. Install

Run the following command:

composer require akaunting/laravel-money

2. Publish

Publish config file.

php artisan vendor:publish --tag=money

3. Configure

You can change the currencies information of your app from config/money.php file

Usage

use Akaunting\Money\Currency;
use Akaunting\Money\Money;

echo Money::USD(500); // '$5.00' unconverted
echo new Money(500, new Currency('USD')); // '$5.00' unconverted
echo Money::USD(500, true); // '$500.00' converted
echo new Money(500, new Currency('USD'), true); // '$500.00' converted

Advanced

$m1 = Money::USD(500);
$m2 = Money::EUR(500);

$m1->getCurrency();
$m1->isSameCurrency($m2);
$m1->compare($m2);
$m1->equals($m2);
$m1->greaterThan($m2);
$m1->greaterThanOrEqual($m2);
$m1->lessThan($m2);
$m1->lessThanOrEqual($m2);
$m1->convert(Currency::GBP(), 3.5);
$m1->add($m2);
$m1->subtract($m2);
$m1->multiply(2);
$m1->divide(2);
$m1->allocate([1, 1, 1]);
$m1->isZero();
$m1->isPositive();
$m1->isNegative();
$m1->format();

Helpers

money(500)
money(500, 'USD')
currency('USD')

Blade Directives

@money(500)
@money(500, 'USD')
@currency('USD')

Blade Component

Same as the directive, there is also a blade component for you to create money and currency in your views:

<x-money amount="500" />
or
<x-money amount="500" currency="USD" />
or
<x-money amount="500" currency="USD" convert />

<x-currency currency="USD" />

Macros

This package implements the Laravel Macroable trait, allowing macros and mixins on both Money and Currency.

Example use case:

use Akaunting\Money\Currency;
use Akaunting\Money\Money;

Money::macro(
    'absolute',
    fn () => $this->isPositive() ? $this : $this->multiply(-1)
);

$money = Money::USD(1000)->multiply(-1);

$absolute = $money->absolute();

Macros can be called statically too:

use Akaunting\Money\Currency;
use Akaunting\Money\Money;

Money::macro('zero', fn (?string $currency = null) => new Money(0, new Currency($currency ?? 'GBP')));

$money = Money::zero();

Mixins

Along with Macros, Mixins are also supported. This allows merging another classes methods into the Money or Currency class.

Define the mixin class:

use Akaunting\Money\Money;

class CustomMoney
{
    public function absolute(): Money
    {
        return $this->isPositive() ? $this : $this->multiply(-1);
    }

    public static function zero(?string $currency = null): Money
    {
        return new Money(0, new Currency($currency ?? 'GBP'));
    }
}

Register the mixin, by passing an instance of the class:

Money::mixin(new CustomMoney);

The methods from the custom class will be available:

$money = Money::USD(1000)->multiply(-1);
$absolute = $money->absolute();

// Static methods via mixins are supported too:
$money = Money::zero();

Changelog

Please see Releases for more information on what has changed recently.

Contributing

Pull requests are more than welcome. You must follow the PSR coding standards.

Security

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.

评论

暂无评论