Immutable Laravel Currency Converter


You need to bring your own database or pool of currency conversion data and implement a data tier for conversions. Here's a sample repository from the project:

<?phpnamespace Gocanto\Converter\Examples;use Gocanto\Converter\CurrencyValue;use Gocanto\Converter\Interfaces\CurrenciesRepositoryInterface;class CurrenciesRepositoryExample 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: return new CurrencyValue('U.S. Dollars', 'USD', '$', 0.731271); }}

Next, here's an example of a currency conversion supported by the data store:

use Gocanto\Converter\Examples\CurrenciesRepositoryExample;use Gocanto\Converter\Converter;use Gocanto\Converter\RoundedNumber;$repository = new CurrenciesRepositoryExample;$converter = new Convert ($repository);$conversion = $converter ->withAmount(RoundedNumber::make(10)) ->withCurrency('SGD') ->convertTo('USD');

You can learn more about this package, get all the installation instructions and find the source code on GitHub gocanto/converter .


KARABAY A, 2019 . Immutable Laravel Currency Converter,

(Accessed July 02, 2019).

  Share this post

Comments (0)


Sign up for the email newsletter to be the first to know about my blog posts