PHP code example of sirix / money

1. Go to this page and download the library: Download sirix/money 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/ */

    

sirix / money example snippets


use Brick\Money\Context\AutoContext;
use Brick\Money\Context\CustomContext;
use Sirix\Money\FiatCurrencyCode;
use Sirix\Money\CryptoCurrencyCode;
use Sirix\Money\SirixMoney;
use Brick\Math\RoundingMode;

// Basic usage with default context and rounding
$amount = SirixMoney::of(10.99, FiatCurrencyCode::Usd);

// Advanced usage with custom context and rounding mode
$amount = SirixMoney::of(
    amount: 10.99,
    currencyCode: FiatCurrencyCode::Usd,
    context: new CustomContext(scale: 5), // Custom context
    rounding: RoundingMode::UP
);

// Create from minor units (cents) with default settings
$cents = SirixMoney::ofMinor(1099, FiatCurrencyCode::Usd);

// Create from minor units with custom context and rounding
$cents = SirixMoney::ofMinor(
    amount: 1099,
    currencyCode: 'USD',
    context: new AutoContext(),
    roundingMode: RoundingMode::DOWN
);

// Check if currency is crypto
$isCrypto = SirixMoney::isCrypto('BTC'); // returns true

// Create a money object with trailing zeros
$money = SirixMoney::of('10.90', CryptoCurrencyCode::Bch);

// Get amount with trailing zeros removed (default behavior)
$withoutZeros = SirixMoney::getAmount($money); // returns "10.9"

// Get amount keeping trailing zeros
$withZeros = SirixMoney::getAmount($money, withoutTrailingZeros: false); // returns "10.90000000"

// Example with more decimal places
$precise = SirixMoney::of('10.50000', CryptoCurrencyCode::Bch);
echo SirixMoney::getAmount($precise); // displays: "10.5"
echo SirixMoney::getAmount($precise, false); // displays: "10.50000000"

// Minor amounts are always returned as full integers
$minorAmount = SirixMoney::getMinorAmount($money); // returns "1050000000"


use Sirix\Money\CurrencyRegistry;
use Brick\Money\Currency;

// Get the registry instance
$registry = CurrencyRegistry::getInstance();

// Configure caching
$registry->setCache($psr6CacheImplementation);
$registry->setCachePrefix('custom_prefix_'); //change cache prefix (if necessary)
$registry->setCacheTtl(7200); //set cache ttl (default: 86400 sec)
// Add custom currency
$customCurrency = new Currency('XYZ', 999, 'Custom Currency', 2);
$registry->addCustomCurrency($customCurrency, true); // true for crypto, false for fiat

$registry = CurrencyRegistry::getInstance();
$registry->setCache($cachePool);
$registry->setCachePrefix('my_app_');

use Sirix\Money\CurrencyCode;

// Create a FiatCurrencyCode from a numeric code
$usd = CurrencyCode::fromNumericCode(840); // Returns FiatCurrencyCode::Usd

// Create a CryptoCurrencyCode from a numeric code
$btc = CurrencyCode::fromNumericCode(1004); // Returns CryptoCurrencyCode::Btc