Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage
  • Money
  • Currency
  • Formatters
  • Twig Bridge
  • Installation
  • Usage
  • Need Help?

Was this helpful?

Edit on GitHub
  1. Components

Money

PreviousTransportsNextCurrency Providers

Last updated 9 months ago

Was this helpful?

The Money component was inspired by JSR 354 along with a few other ideas. It is mainly used for services and sites dealing with money.

Installation

composer require sonsofphp/money

Usage

Money

<?php
use SonsOfPHP\Component\Money\Money;
use SonsOfPHP\Component\Money\Currency;

// Can use the Money Class like this
$money = new Money(100, new Currency('USD'));
$amount = $money->getAmount(); // AmountInterface
$value = $amount->toString(); // `toInt` and `toFloat` are also supported

// Or like this
$money = Money::USD(100);

You can preform different to create new money.

<?php
use SonsOfPHP\Component\Money\Money;

$money = Money::USD(100);

$newMoney1 = $money->add(Money::USD(100)); // Amount is now 200
$newMoney2 = $money->subtract(Money::USD(100)); // Amount is now 0

// The amount of the orginal Money does not change
$amount = $money->getAmount(); // Amount is 100

You can multiple and divide too.

<?php
use SonsOfPHP\Component\Money\Money;

$money = Money::USD(100);

$newMoney1 = $money->multiply(5); // value of Amount is now 500
$newMoney2 = $money->divide(5); // value of Amount is now 20

// The amount of the orginal Money does not change
$amount = $money->getAmount(); // value of Amount is 100

Currency

<?php
use SonsOfPHP\Component\Money\Currency;

$currency = new Currency('USD');
// OR
$currency = Currency::USD();

Formatters

<?php

use SonsOfPHP\Component\Money\Formatter\IntlMoneyFormatter;
use SonsOfPHP\Component\Money\Money;

$formatter = new IntlMoneyFormatter(new \NumberFormatter('en_US', \NumberFormatter::CURRENCY));
$output = $formatter->format(Money::USD(4.20));
echo $output; // $4.20

Twig Bridge

Installation

composer require sonsofphp/money-twig

Usage

Add Extension to Twig Environment

<?php

use SonsOfPHP\Bridge\Twig\Money\MoneyExtension;
use SonsOfPHP\Component\Money\Formatter\IntlMoneyFormatter;

$formatter = new IntlMoneyFormatter(new \NumberFormatter('en_US', \NumberFormatter::CURRENCY));
$extension = new MoneyExtension($formatter);

$twig = new \Twig\Environment($loader);
$twig->addExtension($extension);

Usage in Twig Templates

Your total is {{ money|format_money }}.

Need Help?

Check out .

📦
operations
Sons of PHP's Organization Discussions