gocanto/converter

unpredictable results

Closed this issue · 6 comments

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

use Gocanto\Converter\CurrencyValue;
use Gocanto\Converter\Converter;
use Gocanto\Converter\Interfaces\CurrenciesRepositoryInterface;
use Gocanto\Converter\RoundedNumber;

class EurCurrencyRepositoryObject implements CurrenciesRepositoryInterface
{
    /**
     * @param string $code
     * @return CurrencyValue
     */
    public function getCurrentRate(string $code) : CurrencyValue
    {
        //here, you need to write any related logic to query your DB. Once you have this info,
        //you will have to create the currency value object with the valid info as so:

        switch ($code){
            case 'EUR' :
                return new CurrencyValue('EURO', 'EUR', '', 1.0);
            case 'USD' :
                return new CurrencyValue('U.S. Dollars', 'USD', '$', 0.731271);
            case 'GBP':
                return new CurrencyValue('Sterlina', 'GBP', 'L', 1.10234);
        }

        return new CurrencyValue('EURO', 'EUR', '',1.0);
    }
}

$EurRepository = new EurCurrencyRepositoryObject;
$converter = new Converter($EurRepository);

// Convert 1000 USD in EUR : EUR 731.271  CORRECT

$conversion = $converter
    ->withAmount(RoundedNumber::make(1000))
    ->withCurrency('USD')
    ->convertTo('EUR');

print "\n".($conversion->getFormattedAmount());


// CONVERT 1000 EUR in USD : USD 1000 NOT CORRECT !!
$conversion = $converter
    ->withAmount(RoundedNumber::make(1000))
    ->withCurrency('EUR')
    ->convertTo('USD');

print "\n".($conversion->getFormattedAmount());

this script is giving unpredictable results and i don't know why:

  • Convert 1000 USD in EUR : EUR 731.271 CORRECT
  • CONVERT 1000 EUR in USD : USD 1000 NOT CORRECT !!

Are you sure you are working with valid rates?

Here is the rate EUR-USD https://www.xe.com/currencyconverter/convert/?Amount=1&From=EUR&To=USD

No, i am not working with real rates. I am using fake rates harcoded in my EurCurrencyRepositoryObject :
1 USD = 0.731271 EUR
1 GBP = 1.10234 EUR

and i am using the converter to convert the currencies based on my fake currency repository:
i try to convert :

  • 1000 USD in EUR : resulting EUR 731.271 CORRECT - based on my repository values
  • 1000 EUR in USD : USD 1000 NOT CORRECT - based on my repository values , i would expect 1367 USD
    What am i doing wrong? Is this behaviour correct ?

Can you try to use valid rates and see whether you get a valid response?

if possible, can you send a PR with a test exposing the issue for me to understand better? , thanks

i think this is not a bug. i believe that this library would calculate exchange rates from cross referenced currencies using pivot currencies. For example fro EUR to GBP to USD and so on !

Yes. This lib just offer the abstraction, but the data needs to be provided by you. Therefore, numbers should be ok as long as you are feeding valid ones.

Thanks for testing :)