-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
447e5e0
commit 147ffea
Showing
1 changed file
with
25 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,80 @@ | ||
currency-converter-php | ||
====================== | ||
|
||
Currency Converter Class with features of caching and identifying currency from country Code | ||
Currency Converter Library with features of caching and identifying currency from country Code | ||
|
||
## Why Use It | ||
<ul> | ||
<li>Object Oriented and Easy-to-Use</li> | ||
<li>Reliable Rate, Uses Yahoo API</li> | ||
<li>Caching of rate, to avoid connecting to Yahoo again and again</li> | ||
<li>Conversion without curreny code(from country code)</li> | ||
</ul> | ||
|
||
* Object Oriented and Easy-to-Use | ||
* Reliable Rate, Uses Yahoo API | ||
* Caching of rate, to avoid connecting to Yahoo again and again | ||
* Conversion without curreny code(from country code) | ||
|
||
|
||
#### Object Oriented and Easy-to-Use | ||
The source code are fully object oriented and easy to use! You can easily accomodate the source files to your application! | ||
|
||
|
||
#### Reliable Rate, Uses Yahoo API | ||
The class uses Yahoo API, "http://download.finance.yahoo.com/d/quotes.csv?s=USDNPR=X&f=nl1d1t1" for getting rates! | ||
The library uses Yahoo API, "http://download.finance.yahoo.com/d/quotes.csv?s=USDNPR=X&f=nl1d1t1" for getting rates! | ||
|
||
|
||
#### Caching of rate, to avoid connecting to Yahoo again and again | ||
It might not be intelligent to connect to Yahoo for the same rate regularly. This is not tolerable in production state. | ||
So, this class uses caching system to improve performance! You can easily set cache directory(directory where cache exists) and cache expiry. | ||
So, this library uses caching system to improve performance! You can easily set cache directory(directory where cache exists) and cache expiry. | ||
|
||
|
||
#### Conversion without curreny code(from country code) | ||
It might be quiet weird for user to input their currency code or you may have already created application where user enter his/her country. But, you want to know their curreny code! | ||
With this class, you dont have worry about that, because, you can retrieve rates from country code without currency code! | ||
|
||
With this library, you dont have worry about that, because, you can retrieve rates from country code without currency code! | ||
|
||
|
||
## Requirements | ||
<ul> | ||
<li>PHP version 5.3.3 or later</li> | ||
<li>Curl Extension</li> | ||
</ul> | ||
|
||
* PHP version 5.3.3 or later | ||
* Curl Extension | ||
|
||
## Installation | ||
This library depends on composer(for installation and autoloading and whatever). For installation of composer please visit composer.org | ||
|
||
1. Add `ujjwal/currency-converter` to your composer.json and run `php composer.phar self-update` | ||
|
||
## Usage | ||
|
||
#### Initializing Conversion | ||
At first you need to call class! | ||
```php | ||
require_once("path/to/CurrencyConverter.php"); | ||
$CurrencyConverter = new \library\CurrencyConverter(); | ||
``` | ||
|
||
#### Setting "From" And "To" | ||
At first you need to call the Library! | ||
```php | ||
$CurrencyConverter->setFromCurrency("USD"); | ||
//Or you can do | ||
$CurrencyConverter->setFromCountry("US"); | ||
``` | ||
Similiarly, | ||
```php | ||
$CurrencyConverter->setToCurrency("NPR"); | ||
//Or you can do | ||
$CurrencyConverter->setToCountry("NP"); | ||
``` | ||
use CurrencyConverter\CurrencyConverter; | ||
|
||
###### Alternatively, you can set these while calling Class! | ||
```php | ||
$CurrencyConverter=new CurrencyConverter(array("country"=>"US"),array("currency"=>"NPR")); | ||
// or $CurrencyConverter=new CurrencyConverter(array("currency"=>"USD"),array("country"=>"NP")); | ||
// or $CurrencyConverter=new CurrencyConverter(array("currency"=>"USD"),array("currency"=>"NPR")); | ||
// or $CurrencyConverter=new CurrencyConverter(array("country"=>"US"),array("country"=>"NP")); | ||
require_once("vendor/autoload.php"); | ||
$converter = new CurrencyConverter; | ||
``` | ||
First parameter represents "From" credentials and Second parameter represents "To" credentials. | ||
|
||
|
||
## Setting Cache | ||
|
||
#### Enable Cache | ||
Cache is not enabled by default, so you have to enable it first. | ||
```php | ||
$CurrencyConverter->setCachable(true); | ||
//or disable by $CurrencyConverter->setCachable(false); | ||
$converter->setCachable(true); | ||
//or disable by $converter->setCachable(false);; | ||
``` | ||
#### Set Cache Directory | ||
By default, cache directory is currently working directory. So you probably want to change it! | ||
```php | ||
$CurrencyConverter->setCacheDirectory("path/to/your/cache/directory/"); | ||
$converter->setCacheDirectory("path/to/your/cache/directory/"); | ||
``` | ||
If directory does not exists, an Exception is thrown! | ||
|
||
#### Set Cache Timeout | ||
By default, cache timeout is 18000 seconds(5 hours) which means connection to Google is done in every 5 hours and after that a new cache is created! | ||
By default, cache timeout is 18000 seconds(5 hours) which means connection to Yahoo is done in every 5 hours and after that a new cache is created! | ||
You can change if you like: | ||
```php | ||
$CurrencyConverter->setCacheTimeOut("time_in_seconds"); | ||
$converter->setCacheTimeOut("time_in_seconds"); | ||
``` | ||
|
||
### Finally getting conversion rate | ||
```php | ||
$rate=$CurrencyConverter->convert($amount);// $amount is optional and defaults to 1 | ||
$amount = $converter->convert(array('country' => 'US'), array('country' => 'NP'), $amount);// $amount is optional and defaults to 1 | ||
``` | ||
|
||
### Recommended Way | ||
The above way of converting rates is very long. So, I recommend you to create a function which does all the dirty work of setting cache and all you have to do is call the function. | ||
A simple example is shown below:: | ||
```php | ||
function convert_currency(array $from, array $to, $amount=1){ | ||
require_once(__DIR__."/../CurrencyConverter.php"); | ||
$CurrencyConverter = new \library\CurrencyConverter($from, $to); | ||
$CurrencyConverter->setCachable(TRUE); | ||
$CurrencyConverter->setCacheDirectory(__DIR__."/cache/"); | ||
$CurrencyConverter->setCacheTimeOut(10000); | ||
return $CurrencyConverter->convert($amount); | ||
} | ||
$rate = convert_currency(array("currency"=>"USD"), array("country"=>"NP")); | ||
echo $rate; | ||
``` | ||
Ofcourse, you can change it(convert_currency) as per your requirement! |