{"id":15740837,"url":"https://github.com/tomwright/currencyphp","last_synced_at":"2025-03-31T05:15:38.583Z","repository":{"id":57071517,"uuid":"80734846","full_name":"TomWright/CurrencyPHP","owner":"TomWright","description":"A small package to facilitate currency conversions in PHP.","archived":false,"fork":false,"pushed_at":"2017-10-24T11:56:48.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-06T09:48:14.651Z","etag":null,"topics":["conversion-rate","currency","currency-converter","currency-exchange-rates","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomWright.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-02T14:49:01.000Z","updated_at":"2017-04-13T10:55:33.000Z","dependencies_parsed_at":"2022-08-24T10:40:52.421Z","dependency_job_id":null,"html_url":"https://github.com/TomWright/CurrencyPHP","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2FCurrencyPHP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2FCurrencyPHP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2FCurrencyPHP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2FCurrencyPHP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomWright","download_url":"https://codeload.github.com/TomWright/CurrencyPHP/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246418646,"owners_count":20773938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conversion-rate","currency","currency-converter","currency-exchange-rates","php"],"created_at":"2024-10-04T02:40:58.202Z","updated_at":"2025-03-31T05:15:38.556Z","avatar_url":"https://github.com/TomWright.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CurrencyPHP\n\n[![Build Status](https://travis-ci.org/TomWright/CurrencyPHP.svg?branch=master)](https://travis-ci.org/TomWright/CurrencyPHP)\n[![Latest Stable Version](https://poser.pugx.org/tomwright/currency-php/v/stable)](https://packagist.org/packages/tomwright/currency-php)\n[![Total Downloads](https://poser.pugx.org/tomwright/currency-php/downloads)](https://packagist.org/packages/tomwright/currency-php)\n[![Monthly Downloads](https://poser.pugx.org/tomwright/currency-php/d/monthly)](https://packagist.org/packages/tomwright/currency-php)\n[![Daily Downloads](https://poser.pugx.org/tomwright/currency-php/d/daily)](https://packagist.org/packages/tomwright/currency-php)\n[![License](https://poser.pugx.org/tomwright/currency-php/license.svg)](https://packagist.org/packages/tomwright/currency-php)\n\n## Installation\n\n```\ncomposer install tomwright/currency-php\n```\n\n## Usage\n\nCurrencyPHP is just a basic wrapper. It cannot do conversions out of the box... Saying that... you only need to provide it with the conversion rates.\n\n```php\n\n$rateFetcher = new MyConversionRateFetcher();\n$factory = new CurrencyFactory($rateFetcher);\n\n$gbp = $factory-\u003ecreate('GBP');\n$usd = $factory-\u003ecreate('USD');\n\n$priceInGBP = 100;\n$priceInUSD = $gbp-\u003econvertTo($usd, $priceInGBP);\necho $priceInUSD; // 126\n```\n\n## Rate Fetchers\n\nRate Fetchers are what `CurrencyPHP` uses to get conversion rates. Any Rate Fetcher you create should implement `ConversionRateFetcherInterface`.\n\n### Existing Rate Fetchers\n\n- [Fixer IO](https://github.com/TomWright/CurrencyPHPFixerIORateFetcher)\n- [Yahoo Currency API](https://github.com/TomWright/CurrencyPHPYahooRateFetcher)\n\nIf you have created your own Rate Fetcher and want it included here, please submit a pull request.\n\n### Creating Your Own\n\nThe following Rate Fetcher gives you some fixed exchange rates:\n- GBP to USD\n- USD to GBP\n- GBP to CAD\n- CAD to USD\n\n```php\nclass FixedRateFetcher implements ConversionRateFetcherInterface\n{\n\n    /**\n     * @param Currency $from\n     * @param Currency $to\n     * @return float\n     */\n    public function getConversionRate(Currency $from, Currency $to)\n    {\n        $rates = [\n            [\n                'from' =\u003e 'GBP',\n                'to' =\u003e 'USD',\n                'rate' =\u003e 1.2547,\n            ],\n            [\n                'from' =\u003e 'USD',\n                'to' =\u003e 'GBP',\n                'rate' =\u003e 0.7974,\n            ],\n            [\n                'from' =\u003e 'GBP',\n                'to' =\u003e 'CAD',\n                'rate' =\u003e 1.6612,\n            ],\n            [\n                'from' =\u003e 'CAD',\n                'to' =\u003e 'USD',\n                'rate' =\u003e 0.7539,\n            ],\n        ];\n\n        $result = null;\n\n        foreach ($rates as $rate) {\n            if ($rate['from'] === $from-\u003egetCurrencyCode() \u0026\u0026 $rate['to'] === $to-\u003egetCurrencyCode()) {\n                $result = $rate['rate'];\n            }\n        }\n        return $result;\n    }\n}\n```\n\n### Handling unknown conversion rates\n\n#### One way conversion rates\n\nThe above Rate Fetcher has rates for both GBP to USD, and USD to GBP and this works great... but you'll also notice that it has CAD to USD, but no USD to CAD conversion rates. There is some logic implemented so that you only need to store 1 way conversion rates and it will automatically invert the rate if required.\n\nThanks to this logic, you can run a USD to CAD conversion using the above Rate Fetcher with no problems. The full list of conversion that the above can handle is as follows:\n- GBP to USD\n- USD to GBP\n- GBP to CAD\n- CAD to GBP\n- CAD to USD\n- USD to CAD\n\n#### Missing conversion rates\n\nIf no conversion rate exists at all between the 2 currencies, an `UnhandledConversionRate` Exception will be thrown. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwright%2Fcurrencyphp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomwright%2Fcurrencyphp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwright%2Fcurrencyphp/lists"}