Code Monkey home page Code Monkey logo

exchange's Introduction

Exchange

Build Status Latest stable Downloads this Month Scrutinizer Code Quality Coverage Status

Exchange is PHP script works with currencies. You can convert price, format add VAT or only render exchange rates.

Here is changelog.

Extension for framework

Installation via composer

$ composer require h4kuna/exchange

How to use

Init object Exchange and Cache. Default Driver for read is Cnb, here are others.

For example define own exchange rates:

  • 25 CZK = 1 EUR
  • 20 CZK = 1 USD
use h4kuna\Exchange;

$cache = new Caching\Cache('/temp/dir');
$exchange = new Exchange\Exchange($cache);

$exchange->setDefault('eur');
$exchange->setOutput('usd');

echo $exchange->change(100); // EUR -> USD = 125.0
echo $exchange->change(100, 'czk'); // CZK -> USD = 5.0
echo $exchange->change(100, NULL, 'czk'); // EUR -> CZK = 2500.0
echo $exchange->change(100, 'usd', 'czk'); // USD -> CZK = 2000.0

Change driver and date

Download history exchange rates.

$exchange->setDriver(new Exchange\Driver\Cnb\Day, new \Datetime('2000-12-30'));

If we need read data from database, it is not problem write own Driver.

class MyDatabaseDriver extend \h4kuna\Exchange\Driver\ADriver 
{
    /**
     * Load data for iterator.
     * @param DateTime|NULL $date
     * @return array
     */
    protected function loadFromSource(DateTime $date = NULL)
    {
        // your implementation
        $this->setDate('Y-m-d', (string) $database->selectDate());
        return $database->fetchMyCurrencies();
    }

    /**
     * Modify data before save to cache.
     * @return Exchange\Currency\Property|NULL
     */
    protected function createProperty($row) 
    {
        return new \h4kuna\Exchange\Currency\Property([
            'code' => $row['currency'],
            'home' => $row['rate'],
            'foreign' => 1
        ]);
    }
}

$exchange->setDriver(new \MyDatabaseDriver);

Format output

Define output formats, for more information read this documentation h4kuna/number-format.

$formats = new Exchange\Currency\Formats(new \h4kuna\Number\NumberFormatFactory());

$formats->addFormat('EUR', ['decimalPoint' => '.', 'unit' => '']);

Create Filters for format API.

$filters = new Exchange\Filters($exchange, $formats);

You can define VAT

// VAT 21%
$filters->setVat(new \h4kuna\Number\Tax(21));

Output

// format 100 EUR like default to USD is set above
$filters->format(100); // '125,00 USD'

// count with VAT
$filters->formatVat(100, 'usd', 'eur'); // '96.80 €'
$filters->formatVatTo(100); // '151,25 USD'
$filters->formatTo(100, 'CZK'); // '2 000,00 CZK'

// Other options
$filters->change(100, 'usd', 'eur'); // 80.0
$filters->changeTo(100, 'usd'); // 125.0
$filters->vat(100); // 121.0

Temporary rate

$exchange->addRate('usd', 23.0);
$exchange->change(1, 'usd', 'czk'); // 23.0

$exchange->removeRate('usd');
$exchange->change(1, 'usd', 'czk'); // 20.0

Access and iterator

/* @var $property Exchange\Currenry\Property */
$property = $exchange['eur'];
var_dump($property);


foreach ($exchange as $code => $property) {
    /* @var $property Exchange\Currenry\Property */
    var_dump($property);
}

Limit for currencies

$cache = new Caching\Cache('/temp/dir');
$cache->setAllowedCurrencies(['CZK', 'USD', 'EUR']);
$exchange = new Exchange\Exchange($cache);

// in cache are only this three currencies
foreach ($exchange as $code => $property) {
    /* @var $property Exchange\Currenry\Property */
    var_dump($property);
}

Save state to cookie

Here is prepared object whose keep selected currency.

Run on startup of you project.

$cookieManager = Exchange\Http\CookieManager($exchange);

Set new option

$cookieManager->setCurrency($_GET['currency']);

Cache

Default Cache save data on filesystem you can implement ICache interface and change it.

exchange's People

Contributors

h4kuna avatar spernica avatar foxycode avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.