Skip to content

added the currency conversion endpoint #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ catch (ResponseException $e) {
}
```

## Convert Endpoint
Accessing the currency conversion endpoint may be done like so:

```php
use Fadion\Fixerio\Currency; //optional if you want to use currency constants
use Fadion\Fixerio\Convert;

$converter = new Convert();
$converter->key('YOUR_ACCESS_KEY');
$converter->from('USD');
$converter->to('CAD');
$converter->amount('22.50');
$converter = new Convert('USD', 'CAD', '22.50');
$result = $converter->get();
print $result;

```

You may also use currency constants

```php
$converter->from(Currency::USD);
```


## Laravel Usage

Nothing changes for Laravel apart from the Facade. It's just a convenience for a tad shorter way of using the package:
Expand Down
302 changes: 302 additions & 0 deletions src/Convert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
<?php

namespace Fadion\Fixerio;

use DateTime;
use Fadion\Fixerio\Exceptions\ConnectionException;
use Fadion\Fixerio\Exceptions\ResponseException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\TransferException;

class Convert
{
/**
* Guzzle client
* @var GuzzleHttp\Client
*/
private $guzzle;

/**
* URL of fixer.io
* @var string
*/
private $url = "data.fixer.io/api";

/**
* Http or Https
* @var string
*/
private $protocol = 'http';

/**
* Holds whether the response should be
* an object or not
* @var array
*/
private $asObject = false;

/**
* Holds the Fixer.io API key
*
* @var null|string
*/
private $key = null;


/**
* Holds the from currency
*
* @var null|string
*/
private $from = null;

/**
* Holds the to currency
*
* @var null|string
*/
private $to = null;

/**
* Holds the amount to convert
*
* @var null|string
*/
private $amount = null;


/**
* @param $guzzle Guzzle client
*/
public function __construct($guzzle = null)
{
if (isset($guzzle)) {
$this->guzzle = $guzzle;
} else {
$this->guzzle = new GuzzleClient();
}
}

/**
* Sets the from currency
*
* @param string $to
* @return Exchange
*/
public function from($from)
{
$this->from = $from;

return $this;
}


/**
* Sets the to currency
*
* @param string $to
* @return Exchange
*/
public function to($to)
{
$this->to = $to;

return $this;
}


/**
* Sets the amount
*
* @param string $to
* @return Exchange
*/
public function amount($amount)
{
$this->amount = $amount;

return $this;
}

/**
* Sets the protocol to https
*
* @return Exchange
*/
public function secure()
{
$this->protocol = 'https';

return $this;
}

/**
* Sets the API key
*
* @param string $key
* @return Exchange
*/
public function key($key)
{
$this->key = $key;

return $this;
}

/**
* Returns the correctly formatted url
*
* @return string
*/
public function getUrl()
{
return $this->buildUrl($this->url);
}

/**
* Makes the request and returns the response
* with the rates.
*
* @throws ConnectionException if the request is incorrect or times out
* @throws ResponseException if the response is malformed
* @return array
*/
public function get()
{
$url = $this->buildUrl($this->url);

try {
$response = $this->makeRequest($url);

return $this->prepareResponse($response);
}
// The client needs to know only one exception, no
// matter what exception is thrown by Guzzle
catch (TransferException $e) {
throw new ConnectionException($e->getMessage());
}
}

/**
* Makes the request and returns the response
* with the rates, as a Result object
*
* @throws ConnectionException if the request is incorrect or times out
* @throws ResponseException if the response is malformed
* @return Result
*/
public function getResult()
{
$url = $this->buildUrl($this->url);

try {
$response = $this->makeRequest($url);

return $this->prepareResponseResult($response);
}
// The client needs to know only one exception, no
// matter what exception is thrown by Guzzle
catch (TransferException $e) {
throw new ConnectionException($e->getMessage());
}
}

/**
* Alias of get() but returns an object
* response.
*
* @throws ConnectionException if the request is incorrect or times out
* @throws ResponseException if the response is malformed
* @return object
*/
public function getAsObject()
{
$this->asObject = true;

return $this->get();
}

/**
* Forms the correct url from the different parts
*
* @param string $url
* @return string
*/
private function buildUrl($url)
{
$url = $this->protocol . '://' . $url . '/convert';

if ($this->key) {
$url .= '?access_key=' . $this->key;
}

if ($this->from) {
$url .= '&from=' . $this->from;
}

if ($this->to) {
$url .= '&to=' . $this->to;
}

if ($this->amount) {
$url .= '&amount=' . $this->amount;
}

return $url;
}

/**
* Makes the http request
*
* @param string $url
* @return string
*/
private function makeRequest($url)
{
$response = $this->guzzle->request('GET', $url);

return $response->getBody();
}

/**
* @param string $body
* @throws ResponseException if the response is malformed
* @return array
*/
private function prepareResponse($body)
{
$response = json_decode($body, true);

if (isset($response['result'])) {
return ($this->asObject) ? (object) $response['result'] : $response['result'];
} else if (isset($response['error'])) {
throw new ResponseException($response['error']);
// var_dump($response);
// throw new ResponseException("{$response['error']['type']} {$response['error']['info']}");
} else {
throw new ResponseException('Response body is malformed.');
}
}

/**
* @param string $body
* @throws ResponseException if the response is malformed
* @return Result
*/
private function prepareResponseResult($body)
{
$response = json_decode($body, true);

if (isset($response['query']) and isset($response['result'])) {
return new Result(
$response['base'],
new DateTime($response['date']),
$response['rates']
);
} else if (isset($response['error'])) {
throw new ResponseException($response['error']);
} else {
throw new ResponseException('Response body is malformed.');
}
}
}
Loading