PHP code example of sebastiansulinski / laravel-currency

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

    

sebastiansulinski / laravel-currency example snippets



'providers' => [
    ...

    SSD\Currency\CurrencyServiceProvider::class

]

'aliases' => [
    ...

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

php artisan vendor:publish



return [
    "key" => "currency",
    "default" => \SSD\Currency\Currencies\GBP::code(),
    "currencies" => [
        \SSD\Currency\Currencies\GBP::class,
        \SSD\Currency\Currencies\USD::class,
        \SSD\Currency\Currencies\EUR::class,
    ],
    "value_as_integer" => false,
];

 

namespace App\Components\Currencies;

use SSD\Currency\Currencies\BaseCurrency;

class JPY extends BaseCurrency
{
    /**
     * Get symbol.
     *
     * @return string
     */
    public static function symbol(): string
    {
        return '¥';
    }

    /**
     * Get code.
     *
     * @return string
     */
    public static function code(): string
    {
        return 'JPY';
    }
}



return [
    "key" => "currency",
    "default" => \SSD\Currency\Currencies\GBP::code(),
    "currencies" => [
        \SSD\Currency\Currencies\GBP::class,
        \SSD\Currency\Currencies\USD::class,
        \SSD\Currency\Currencies\EUR::class,
        \App\Components\Currencies\JPY::class,
    ],
    "value_as_integer" => false,
];

 

namespace App\Components\Currencies;

use SSD\Currency\Currencies\BaseCurrency;

class PLN extends BaseCurrency
{
    /**
     * Get symbol.
     *
     * @return string
     */
    public static function symbol(): string
    {
        return 'zł';
    }

    /**
     * Get code.
     *
     * @return string
     */
    public static function code(): string
    {
        return 'PLN';
    }

    /**
     * Determine if symbol should be placed after the value.
     *
     * @return bool
     */
    protected static function symbolAfterValue(): bool
    {
        return true;
    }

    /**
     * Determine if there is a space between symbol and the value.
     *
     * @return bool
     */
    protected static function symbolSpace(): bool
    {
        return true;
    }
}

// app/Http/routes.php

Route::get('currency/{currency_id}', 'CurrencyController@set');


// app/Http/Controllers/CurrencyController.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]);
    }
}


[
    '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
]

/**
 * Array of prices.
 *
 * @return array
 */
public function prices()
{
    return [
        'GBP' => $this->price_gbp,
        'EUR' => $this->price_usd,
        'USD' => $this->price_eur,
        'JPY' => $this->price_jpy
    ];
}

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