jtn/shopify-currency-format

用于格式化字符串以适应Shopify货币格式的辅助工具

v1.0.0 2019-11-06 07:17 UTC

This package is auto-updated.

Last update: 2024-09-06 18:36:30 UTC


README

Build Status

这是一个由Shopify API提供的货币格式化的小型辅助工具。所有在Shopify货币格式文档中涵盖的格式都得到了支持

安装

使用composer安装。

composer require jtn/shopify-currency-format

用法

只需创建一个类的实例,并传入你想要使用的格式,然后将以分表示的整数传递给format方法。

如果你传递一个字符串,库将期望这是来自Shopify API的,因此将以美元和分表示,所以它将被除以100。

你可以通过调用setFormat来更改格式。

use Jtn\ShopifyCurrencyFormat\ShopifyCurrencyFormat;

$formatter = new ShopifyCurrencyFormat('{{ amount_no_decimals }}');
echo $formatter->format(3.65); // 4

$formatter->setFormat('{{ amount_no_decimals }}');
echo $formatter->format(300); // 3.00

$formatter->setFormat('<span class=money>£{{ amount }} GBP</span>');
echo $formatter->format('5.99'); // <span class=money>£5.99 GBP</span>

如果你指定了一个不受支持的格式,将抛出异常。

use Jtn\ShopifyCurrencyFormat\ShopifyCurrencyFormat;
use Jtn\ShopifyCurrencyFormat\UnsupportedFormatException;

$formatter = new ShopifyCurrencyFormat('{{ some_unsupported_format }}');

try {
	echo $formatter->format(1.00);
} catch(UnsupportedFormatException $e) {
	echo 'That format is not supported :(';
}

// That format is not supported :(