PHP code example of red-jasmine / money

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

    

red-jasmine / money example snippets


return [
    // 默认货币
    'default_currency' => env('MONEY_DEFAULT_CURRENCY', 'CNY'),

    // 货币配置
    'currencies' => [
        // ISO 标准货币
        'iso' => [
            'CNY', 'USD', 'EUR', 'GBP', 'JPY', 'HKD', 'TWD', 'KRW'
        ],
        // 或使用 'all' 支持所有 ISO 货币
        // 'iso' => 'all',

        // 比特币货币
        'bitcoin' => false,

        // 自定义货币
        'custom' => [
            // 积分
            'POINTS' => [
                'name' => '积分',
                'code' => 'POINTS',
                'subunit' => 0,  // 没有小数
                'numeric_code' => 999,
            ],
            
            // 游戏金币
            'GOLD' => [
                'name' => '金币',
                'code' => 'GOLD',
                'subunit' => 0,
                'numeric_code' => 998,
            ],
            
            // 平台代金券
            'VOUCHER' => [
                'name' => '代金券',
                'code' => 'VOUCHER',
                'subunit' => 2,  // 两位小数
                'numeric_code' => 997,
            ],
        ],
    ],
];

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->decimal('price_amount', 10, 2);  // 金额
    $table->string('price_currency', 3);      // 货币代码
    $table->timestamps();
});

use Illuminate\Database\Eloquent\Model;
use Money\Money;
use RedJasmine\Money\Casts\MoneyCast;

class Product extends Model
{
    protected $casts = [
        'price' => MoneyCast::class,
    ];
}

// 创建产品
$product = new Product();
$product->price = new Money(10050, new Currency('CNY')); // 100.50 元
$product->save();

// 或使用字符串
$product->price = '100.50';
$product->save();

// 或使用数组
$product->price = [
    'amount' => '100.50',
    'currency' => 'USD'
];
$product->save();

// 读取
$price = $product->price; // Money 对象
echo $price->getAmount(); // "10050" (最小单位:分)
echo $price->getCurrency()->getCode(); // "CNY"

use Money\Money;
use RedJasmine\Money\Casts\MoneyCast;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Data;

class ProductData extends Data
{
    public function __construct(
        public string $name,
        
        #[WithCast(MoneyCast::class)]
        public ?Money $price = null,
    ) {
    }
}

protected $casts = [
    'price' => MoneyCast::class . ':currency_code,money_value',
];

protected $casts = [
    'price' => MoneyCast::class . ':null,null,null,bigint',
];

$table->bigInteger('price_amount');      // 使用 bigint 存储最小单位
$table->string('price_currency', 3);

protected $casts = [
    'price' => MoneyCast::class . ':currency,null,true',
    'cost' => MoneyCast::class . ':currency,null,true',
];

$table->decimal('price_amount', 10, 2);
$table->decimal('cost_amount', 10, 2);
$table->string('currency', 3);  // 共享货币字段

use Money\Currency;
use Money\Money;

// 使用积分
$points = new Money(1000, new Currency('POINTS'));

// 使用游戏金币
$gold = new Money(500, new Currency('GOLD'));

// 使用代金券
$voucher = new Money(5000, new Currency('VOUCHER')); // 50.00

use RedJasmine\Money\Currencies\AggregateCurrencies;

// 获取货币管理器
$currencies = app(AggregateCurrencies::class);

// 检查货币是否支持
$isSupported = $currencies->contains(new Currency('POINTS'));

// 获取货币小数位数
$subunit = $currencies->subunitFor(new Currency('CNY')); // 2

// 检查货币类型
$isISO = $currencies->isISO(new Currency('USD'));        // true
$isBitcoin = $currencies->isBitcoin(new Currency('XBT')); // true
$isCustom = $currencies->isCustom(new Currency('POINTS')); // true

MoneyCast::class . ':currencyKey,valueKey,isShareCurrencyField,valueType'
bash
php artisan vendor:publish --tag=money-config