rikless/laravel-currency 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rikless/laravel-currency

Composer 安装命令:

composer require rikless/laravel-currency

包简介

Currency component for Laravel 5+

README 文档

README

Build Status

Versions

As of version v1.3.0, package requires PHP 7.1. For earlier versions of PHP, please use v1.2.0

Installation

Install the package using composer

composer require sebastiansulinski/laravel-currency

Service Provider and Facade

To use the package with the IOC Container, add its SSD\Currency\CurrencyServiceProvider to the list of providers inside of the config/app.php under the providers:

'providers' => [
    ...

    SSD\Currency\CurrencyServiceProvider::class

]

To use it as a Facade, add it under the aliases:

'aliases' => [
    ...

    'Currency'  => SSD\Currency\CurrencyFacade::class
]

now run:

php artisan vendor:publish

This will add a new file config/currency.php with the following structure:

<?php

return [
    "key" => "currency",
    "default" => "gbp",
    "currencies" => [
        "gbp" => \SSD\Currency\Currencies\GBP::class,
        "usd" => \SSD\Currency\Currencies\USD::class,
        "eur" => \SSD\Currency\Currencies\EUR::class
    ],
    "value_as_integer" => false
];
  • The key is used as the session key, which stores the currently selected currency.
  • The default states the default currency.
  • The currencies contains a list of available currencies.
  • The value_as_integer indicates whether your prices are stored as integers or float / decimal.

Provider

Package comes with two implementations / providers:

  • SSD\Currency\Providers\CookieProvider, which stores selected currency in the encrypted cookie
  • SSD\Currency\Providers\SessionProvider, which will store the selected currency using default session driver

You can create additional providers by:

  • extending SSD\Currency\Providers\BaseProvider

There are 3 methods that the new Provider needs to implement - these are: get, set and is. Please see SSD\Currency\Providers\CookieProvider to get a better idea.

Once you have a new implementation ready, create the new ServiceProvider and replace it within config/app.php.

Adding more currencies

Package comes with 3 currencies out of the box: GBP, USD, EUR.

If you'd like to add more, first create a new currency class, which:

  • extends SSD\Currency\Currencies\BaseCurrency

For instance, implementation for Japanese Yen would be (assuming you keep your currencies under App\Components\Currencies namespace):

<?php 

namespace App\Components\Currencies;

use SSD\Currency\Currencies\BaseCurrency;

class JPY extends BaseCurrency
{
    /**
     * @var string
     */
    protected $prefix = '¥';

    /**
     * @var string
     */
    protected $postfix = 'JPY';
}

All you need to do now to be able to use it is to add it to the config/currency.php file:

<?php

return [
    "key" => "currency",
    "default" => "gbp",
    "currencies" => [
        "gbp" => \SSD\Currency\Currencies\GBP::class,
        "usd" => \SSD\Currency\Currencies\USD::class,
        "eur" => \SSD\Currency\Currencies\EUR::class,
        "jpy" => \App\Components\Currencies\JPY::class
    ],
    "value_as_integer" => false
];

Usage examples

The most common way of displaying currencies is to either have them displayed as clickable buttons or as a form select menu, which is what I'm going to demonstrate below.

First let's create a form select element with all options displayed. We can either use a Currency facade or simply pull currency from within the container using app('currency'):

<form>
    <select id="currency">
        @foreach(app('currency')->options() as $option)
            <option 
                value="/currency/{{ $option->value }}"
                {{ app('currency')->selected($option->value) }}
            >{{ $option->label }}</option>
        @endforeach
    </select>
</form>

Now we need have some JavaScript so that when the change event occurs, the call is made to the given route, where action of the controller sets the new currency and page reloads reflecting that change.

Simply bind change event to the select element using JavaScript:

// vanilla JavaScript
(document.getElementById('currency')).addEventListener('change', function(event) {
    window.location.href = event.target.value;
});

// or using jQuery
$('#currency').on('change', function() {
    window.location.href = $(this).val();
});

Next we need to add the Controller with Action and Route for it:

// app/Http/routes.php

Route::get('currency/{currency_id}', 'CurrencyController@set');
// app/Http/Controllers/CurrencyController.php

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;

use Currency;

class CurrencyController extends Controller
{
    /**
     * Set currency cookie.
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function set($id)
    {
        Currency::set($id);

        return new JsonResponse(['success' => true]);
    }
}

Now if you'd like to display price based on the selected currency, make sure that your model can provider an array of prices for a given item in the following format:

[
    'gbp' => 10.00,
    'eur' => 11.56,
    'usd' => 17.60,
    'jpy' => 18.50
]

// or if you're using prices as integers

[
    'gbp' => 1000,
    'eur' => 1156,
    'usd' => 1760,
    'jpy' => 1850
]

Let's assume our Product model has a prices() method, which will return the array formatted as above. To use it with the currency you can now simply call:

/**
 * Array of prices.
 *
 * @return array
 */
public function prices()
{
    return [
        'gbp' => $this->price_gbp,
        'usd' => $this->price_usd,
        'eur' => $this->price_eur,
        'jpy' => $this->price_jpy
    ];
}

/**
 * Price formatted with the currency symbol.
 *
 * @return string
 */
public function priceDisplay()
{
    return Currency::withPrefix($this->prices(), null, 2);
}

The priceDisplay() method will return the price with the currency symbol i.e. £10.00 (depending on the currently selected currency).

Formatting methods

We have the following methods available on our Currency object:

  • decimal($values, $currency = null, $decimal_points = 2) : gets the value and gives it back in the decimal format.
  • integer($values, $currency = null) : gets the value as integer i.e. 20.53 will become 20, but 2053 will be 2053.
  • withPrefix($values, $currency = null, $decimal_points = null) : gets the value and gives it back with the currency symbol at the beginning.
  • withPostfix($values, $currency = null, $decimal_points = null) : gets the value and gives it back with the currency code at the end.
  • withPrefixAndPostfix($values, $currency = null, $decimal_points = null) : gets the value and gives it back with the currency symbol and code.

Four of the above methods accept 3 arguments (integer method only first 2):

  • $values : either array as above or a single float / int
  • $currency : null by default, which is when the currency will be taken from the cookie - otherwise, you can state what currency you'd like to use.
  • $decimal_points : how many decimal points you'd like it to return the value with.

More methods

  • options() : returns an array of SSD\Currency\Option - each representing one currency. SSD\Currency\Option has 2 properties value and label.
  • selected($currency) : to be used with select option element to make the given option selected="selected" if it is set as current.
  • get() : gets currently selected currency.
  • set($currency) : sets the currency.
  • is($currency) : checks if the currency passed as argument is currently selected.

rikless/laravel-currency 适用场景与选型建议

rikless/laravel-currency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.11k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 rikless/laravel-currency 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rikless/laravel-currency 我们能提供哪些服务?
定制开发 / 二次开发

基于 rikless/laravel-currency 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-26