2017 © Pedro Peláez
 

library money

Currency formatting and conversion package for Laravel.

image

akaunting/money

Currency formatting and conversion package for Laravel.

  • Sunday, July 15, 2018
  • by denisdulici
  • Repository
  • 6 Watchers
  • 129 Stars
  • 12,589 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 13 Forks
  • 3 Open issues
  • 7 Versions
  • 66 % Grown

The README.md

Currency formatting and conversion package for Laravel

Downloads Tests StyleCI Check Imports License, (*1)

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

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., (*3)

Getting Started

1. Install

Run the following command:, (*4)

composer require akaunting/laravel-money

2. Publish

Publish config file., (*5)

php artisan vendor:publish --tag=money

3. Configure

You can change the currencies information of your app from config/money.php file, (*6)

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:, (*7)

<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., (*8)

Example use case:, (*9)

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:, (*10)

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., (*11)

Define the mixin class:, (*12)

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:, (*13)

Money::mixin(new CustomMoney);

The methods from the custom class will be available:, (*14)

$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., (*15)

Contributing

Pull requests are more than welcome. You must follow the PSR coding standards., (*16)

Security

Please review our security policy on how to report security vulnerabilities., (*17)

Credits

License

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

The Versions

15/07 2018

dev-master

9999999-dev

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format

15/07 2018

1.0.5

1.0.5.0

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format

06/02 2018

1.0.4

1.0.4.0

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format

20/12 2017

1.0.3

1.0.3.0

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format

09/12 2017

1.0.2

1.0.2.0

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format

09/12 2017

1.0.1

1.0.1.0

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format

26/11 2017

1.0.0

1.0.0.0

Currency formatting and conversion package for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel currency money convert format