Code Monkey home page Code Monkey logo

currency_converter's Introduction

CurrencyConverter alt text alt text alt text

Currency converter with service to get latest currencies from NBP (Narodowy Bank Polski) and ECB (European Central Bank). Converter has got built-in number speller which convert price into words with specified currency.

Installation

Just clone that repository (remember about NumberSpeller dependency) or just use composer:

composer require plugowski/currency_converter

Usage

Basic usage looks like code below:

<?php
require __DIR__ . '/vendor/autoload.php';

$money = new CurrencyConverter\Money\Money(1, new CurrencyConverter\Currency\Currency('EUR'));

$rateCollection =  new \CurrencyConverter\Exchange\RateCollection();
$rateCollection->add(new \CurrencyConverter\Exchange\Rate('EUR', 4.2636));
$rateCollection->add(new \CurrencyConverter\Exchange\Rate('PLN', 1.0000));

$converter = new CurrencyConverter\Converter($rateCollection);
$converted = $converter->exchange($money, new CurrencyConverter\Currency\Currency('PLN'));

// how many PLN are in 1 EUR
echo $converted->getValue();

Using money factory using magic static methods, just call Money::CURRENCY_CODE(float $value):

$money = Money::EUR(1);

In example above we create exchange rates collection manually, but you can use service to get exchange rates from third party services like NBP or ECB.

Service usage:

<?php
$exchangeService = new CurrencyConverter\Exchange\Service(new CurrencyConverter\Exchange\Repository\NBPRatesRepository());
$converted = $exchangeService->convert(CurrencyConverter\Money\Money::EUR(4), new CurrencyConverter\Currency('PLN'));

MoneyFormatter

For some cases we need show money value as spelled string like. For that case you can use built-in formatter.

<?php
$formatter = \CurrencyConverter\Money\FormatterFactory::create('pl_PL');
$formatter->setMoney(\CurrencyConverter\Money\Money::PLN(20.99));

echo $formatter->spell(); // will return: dwadzieścia złotych dziewięćdziesiąt dziewięć groszy

Of course you are able to change return format, please chect tests for more examples.

Licence

New BSD Licence: https://opensource.org/licenses/BSD-3-Clause

currency_converter's People

Contributors

plugowski 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.