PHP code example of matmar10 / money-bundle

1. Go to this page and download the library: Download matmar10/money-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

matmar10 / money-bundle example snippets



$eur = new Currency('EUR', 2, 2);

$euros = new Money($eur);
$euros->setAmountFloat(1.99);



// must use a valid iso4217 currency code 
// with the exception of BTC for Bitcoin, as specified in the override configuration
$usd = new Currency('USD', 5, 2);

$usdAmount1 = new Money($usd);
$usdAmount1->setAmountFloat(1.2345);

$usdAmount2 = new Money($usd);
$usdAmount2->setAmountFloat(1.2345);

$usdAmount1->isEqual($usdAmount2); // true

$resultAmount1 = $usdAmount1->add($usdAmount2);
echo $resultAmount1->getAmountDisplay(); // 2.47

$resultAmount2 = $usdAmount1->subtract($usdAmount2);
echo $resultAmount2->getAmountFloat(); // 0

$resultAmount3 = $usdAmount1->multiply(3);
echo $resultAmount3->getAmountFloat(); // 3.7035
echo $resultAmount3->getAmountDisplay(); // 3.70

$resultAmount4 = $usdAmount1->divide(2);
echo $resultAmount3->getAmountFloat(); // 0.61725
echo $resultAmount3->getAmountDisplay(); // 0.62



$eurAmount = new Money(new Currency('EUR', 2, 2));
$eurAmount->setAmountFloat(10);

// split the 10 euros into three equal parts using euro cents as the smallest unit
$shares = $eurAmount->allocate(array(1, 1, 1), 2);

$shares[0]->getAmountFloat(); // 3.34
$shares[1]->getAmountFloat(); // 3.33
$shares[2]->getAmountFloat(); // 3.33



$gbp = new Currency('GBP', 2, 2);
$usd = new Currency('USD', 2, 2);

$gbpAmount = new Money($gbp);
$gbpAmount->setAmountFloat(10);


// 1 GBP = 1.5 USD
$gbpToUsd = new CurrencyPair($gbp, $usd, 1.5);

$usdAmount = $gbpToUsd->convert($gbpAmount);
echo $usdAmount->getDisplay(); // 15.00

$gbpAmount2 = $gbpToUsd->convert($usdAmount);
echo $gbpAmount2->getDisplay(); // 10.00



// inside a Symfony controller, for example

$manager = $this->getContainer()->get('lmh_money.currency_manager');

$amount = $manager->getMoney('ES');
echo $amount->getCurrency(); // EUR
$amount->setAmountFloat(100);

// 1 USD = 0.75 EUR
$pair = $manager->getPair('US', 'ES', 0.75);

$converted = $pair->convert($amount);
echo $converted->getAmountDisplay(); // 75.00




// inside an entity file, such as src/Bundle/AcmeBundle/Entity/Purchase.php

namespace Acme\Bundle\AcmeBundle\Entity;

use Lmh\Bundle\MoneyBundle\Validator\Constraints as Assert;

class Purchase
{
    /**
     * @Assert\CurrencyCode()
     */
    public $currency;

}




use Acme\Bundle\AcmeBundle;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()
    ->enableAnnotationMapping()
    ->getValidator();

$purchase = new Purchase()
$purchase->currency = 'invalid-this-is-not-a-code';
$violations = $validator->validate($purchase);




namespace foo\bar;

use Doctrine\ORM\Mapping as ORM;

class Invoice {

    /**
	 * @var \Matmar10\Money\Entity\Money
	 * @ORM\Embedded(columnPrefix="price_", class="Matmar10\Money\Entity\Money")
	 */
    private $price;

}