Skip to content

Commit ad21706

Browse files
authored
Merge pull request #121 from florianv/new
Updated the documentation for the new version
2 parents b220749 + 9bf4057 commit ad21706

File tree

2 files changed

+133
-40
lines changed

2 files changed

+133
-40
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ a [Symfony Bundle](https://github.com/florianv/FlorianvSwapBundle) and a [Larave
1212
## QuickStart
1313

1414
```bash
15-
$ composer require florianv/swap php-http/message php-http/guzzle6-adapter ^1.0
15+
$ composer require php-http/curl-client nyholm/psr7 php-http/message florianv/swap
1616
```
1717

1818
```php
@@ -85,12 +85,11 @@ Here is the list of the currently implemented services:
8585
| [Central Bank of the Czech Republic](https://www.cnb.cz) | * | CZK | Yes |
8686
| [Central Bank of Russia](https://cbr.ru) | * | RUB | Yes |
8787
| [WebserviceX](http://www.webservicex.net) | * | * | No |
88-
| [Google](https://www.google.com/finance) | * | * | No |
8988
| [Cryptonator](https://www.cryptonator.com) | * Crypto (Limited standard currencies) | * Crypto (Limited standard currencies) | No |
9089
| [CurrencyDataFeed](https://currencydatafeed.com) | * (free but limited or paid) | * (free but limited or paid) | No |
9190
| [Open Exchange Rates](https://openexchangerates.org) | USD (free), * (paid) | * | Yes |
9291
| [Xignite](https://www.xignite.com) | * | * | Yes |
93-
| [CurrencyConverterApi](https://www.currencyconverterapi.com) | * | * | Yes (free but limited or paid) |
92+
| [Currency Converter API](https://www.currencyconverterapi.com) | * | * | Yes (free but limited or paid) |
9493
| Array | * | * | Yes |
9594

9695
## Integrations

doc/readme.md

+131-37
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
* [Installation](#installation)
55
* [Configuration](#configuration)
66
* [Usage](#usage)
7+
* [Retrieving Rates](#retrieving-rates)
8+
* [Rate Provider](#rate-provider)
79
* [Cache](#cache)
810
* [Rates Caching](#rates-caching)
9-
* [Cache Options](#cache-options)
11+
* [Query Cache Options](#query-cache-options)
1012
* [Requests Caching](#requests-caching)
11-
* [Service](#service)
12-
* [Creating a Service](#creating-a-service)
13-
* [Supported Services](#supported-services)
14-
* [Sponsors](#sponsors)
13+
* [Creating a Service](#creating-a-service)
14+
* [Standard Service](#standard-service)
15+
* [Historical Service](#historical-service)
16+
* [Supported Services](#supported-services)
17+
* [Sponsors](#sponsors)
1518

1619
## Installation
1720

@@ -20,10 +23,10 @@ which provides the http layer used to send requests to exchange rate services.
2023
This gives you the flexibility to choose what HTTP client and PSR-7 implementation you want to use.
2124

2225
Read more about the benefits of this and about what different HTTP clients you may use in the [HTTPlug documentation](http://docs.php-http.org/en/latest/httplug/users.html).
23-
Below is an example using [Guzzle 6](http://docs.guzzlephp.org/en/latest/index.html):
26+
Below is an example using the curl client:
2427

2528
```bash
26-
composer require florianv/swap php-http/message php-http/guzzle6-adapter
29+
composer require php-http/curl-client nyholm/psr7 php-http/message florianv/swap
2730
```
2831

2932
## Configuration
@@ -51,6 +54,8 @@ The complete list of all supported services is available [here](#supported-servi
5154

5255
## Usage
5356

57+
### Retrieving Rates
58+
5459
In order to get rates, you can use the `latest()` or `historical()` methods on `Swap`:
5560

5661
```php
@@ -69,42 +74,85 @@ $rate = $swap->historical('EUR/USD', (new \DateTime())->modify('-15 days'));
6974

7075
> Currencies are expressed as their [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217) code.
7176
77+
### Rate provider
78+
79+
When using the chain service, it can be useful to know which service provided the rate.
80+
81+
You can use the `getProviderName()` function on a rate that gives you the name of the service that returned it:
82+
83+
```php
84+
$name = $rate->getProviderName();
85+
```
86+
87+
For example, if Fixer returned the rate, it will be identical to `fixer`.
88+
7289
## Cache
7390

7491
### Rates Caching
7592

76-
`Swap` provides a [PSR-6 Caching Interface](http://www.php-fig.org/psr/psr-6) integration allowing you to cache rates during a given time using the adapter of your choice.
93+
`Exchanger` provides a [PSR-16 Simple Cache](http://www.php-fig.org/psr/psr-16) integration allowing you to cache rates during a given time using the adapter of your choice.
7794

78-
The following example uses the Apcu cache from [php-cache.com](http://php-cache.com) PSR-6 implementation.
95+
The following example uses the `Predis` cache from [php-cache.com](http://php-cache.com) PSR-6 implementation installable using `composer require cache/predis-adapter`.
96+
97+
You will also need to install a "bridge" that allows to adapt the PSR-6 adapters to PSR-16 using `composer require cache/simple-cache-bridge` (https://github.com/php-cache/simple-cache-bridge).
7998

8099
```bash
81-
$ composer require cache/apcu-adapter
100+
$ composer require cache/predis-adapter
82101
```
83102

84103
```php
85-
use Cache\Adapter\Apcu\ApcuCachePool;
104+
use Cache\Adapter\Predis\PredisCachePool;
105+
use Cache\Bridge\SimpleCache\SimpleCacheBridge;
106+
107+
$client = new \Predis\Client('tcp:/127.0.0.1:6379');
108+
$psr6pool = new PredisCachePool($client);
109+
$simpleCache = new SimpleCacheBridge($psr6pool);
86110

87-
$swap = (new Builder(['cache_ttl' => 60]))
88-
->useCacheItemPool(new ApcuCachePool())
111+
$swap = (new Builder(['cache_ttl' => 3600, 'cache_key_prefix' => 'myapp-']))
112+
->useSimpleCache($simpleCache)
89113
->build();
90114
```
91115

92-
All rates will now be cached in Apcu during 60 seconds.
116+
All rates will now be cached in Redis during 3600 seconds, and cache keys will be prefixed with 'myapp-'
117+
118+
### Query Cache Options
119+
120+
You can override `Swap` caching options per request.
93121

94-
### Cache Options
122+
#### cache_ttl
95123

96-
You can override `Swap` caching per request:
124+
Set cache TTL in seconds. Default: `null` - cache entries permanently
97125

98126
```php
99-
// Overrides the global cache ttl to 60 seconds
127+
// Override the global cache_ttl only for this query
100128
$rate = $swap->latest('EUR/USD', ['cache_ttl' => 60]);
101129
$rate = $swap->historical('EUR/USD', $date, ['cache_ttl' => 60]);
130+
```
131+
132+
#### cache
133+
134+
Disable/Enable caching. Default: `true`
102135

103-
// Disable the cache
136+
```php
137+
// Disable caching for this query
104138
$rate = $swap->latest('EUR/USD', ['cache' => false]);
105139
$rate = $swap->historical('EUR/USD', $date, ['cache' => false]);
106140
```
107141

142+
#### cache_key_prefix
143+
144+
Set the cache key prefix. Default: empty string
145+
146+
There is a limitation of 64 characters for the key length in PSR-6, because of this, key prefix must not exceed 24 characters, as sha1() hash takes 40 symbols.
147+
148+
PSR-6 do not allows characters `{}()/\@:` in key, these characters are replaced with `-`
149+
150+
```php
151+
// Override cache key prefix for this query
152+
$rate = $swap->latest('EUR/USD', ['cache_key_prefix' => 'currencies-special-']);
153+
$rate = $swap->historical('EUR/USD', $date, ['cache_key_prefix' => 'currencies-special-']);
154+
```
155+
108156
### Requests Caching
109157

110158
By default, `Swap` queries the service for each rate you request, but some services like `Fixer` sends a whole file containing
@@ -137,7 +185,7 @@ $cachePlugin = new CachePlugin($pool, $streamFactory);
137185
$client = new PluginClient(new GuzzleClient(), [$cachePlugin]);
138186

139187
$swap = (new Builder())
140-
->useHttpClient($client);
188+
->useHttpClient($client)
141189
->add('fixer', ['access_key' => 'your-access-key'])
142190
->add('currency_layer', ['access_key' => 'secret', 'enterprise' => false])
143191
->add('forge', ['api_key' => 'secret'])
@@ -150,25 +198,23 @@ $rate = $swap->latest('EUR/USD');
150198
$rate = $swap->latest('EUR/GBP');
151199
```
152200

153-
## Service
154-
155-
### Creating a Service
201+
## Creating a Service
156202

157203
You want to add a new service to `Swap` ? Great!
158204

159-
First you must check if the service supports retrieval of historical rates. If it's the case, you must extend the `HistoricalService` class,
160-
otherwise use the `Service` class.
205+
If your service must send http requests to retrieve rates, your class must extend the `HttpService` class, otherwise you can extend the more generic `Service` class.
206+
207+
### Standard service
161208

162209
In the following example, we are creating a `Constant` service that returns a constant rate value.
163210

164211
```php
165-
use Exchanger\Service\Service;
166212
use Exchanger\Contract\ExchangeRateQuery;
167-
use Exchanger\ExchangeRate;
213+
use Exchanger\Contract\ExchangeRate;
214+
use Exchanger\Service\HttpService;
168215
use Swap\Service\Registry;
169-
use Swap\Builder;
170216

171-
class ConstantService extends Service
217+
class ConstantService extends HttpService
172218
{
173219
/**
174220
* Gets the exchange rate.
@@ -177,22 +223,22 @@ class ConstantService extends Service
177223
*
178224
* @return ExchangeRate
179225
*/
180-
public function getExchangeRate(ExchangeRateQuery $exchangeQuery)
226+
public function getExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRate
181227
{
182228
// If you want to make a request you can use
183-
$content = $this->request('http://example.com');
229+
// $content = $this->request('http://example.com');
184230

185-
return new ExchangeRate($this->options['value']);
231+
return $this->createInstantRate($exchangeQuery->getCurrencyPair(), $this->options['value']);
186232
}
187233

188234
/**
189235
* Processes the service options.
190236
*
191237
* @param array &$options
192238
*
193-
* @return array
239+
* @return void
194240
*/
195-
public function processOptions(array &$options)
241+
public function processOptions(array &$options): void
196242
{
197243
if (!isset($options['value'])) {
198244
throw new \InvalidArgumentException('The "value" option must be provided.');
@@ -206,11 +252,21 @@ class ConstantService extends Service
206252
*
207253
* @return bool
208254
*/
209-
public function supportQuery(ExchangeRateQuery $exchangeQuery)
255+
public function supportQuery(ExchangeRateQuery $exchangeQuery): bool
210256
{
211257
// For example, our service only supports EUR as base currency
212258
return 'EUR' === $exchangeQuery->getCurrencyPair()->getBaseCurrency();
213259
}
260+
261+
/**
262+
* Gets the name of the exchange rate service.
263+
*
264+
* @return string
265+
*/
266+
public function getName(): string
267+
{
268+
return 'constant';
269+
}
214270
}
215271

216272
// Register the service so it's available using Builder::add()
@@ -225,6 +281,45 @@ $swap = (new Builder())
225281
echo $swap->latest('EUR/USD')->getValue();
226282
```
227283

284+
### Historical service
285+
286+
If your service supports retrieving historical rates, you need to use the `SupportsHistoricalQueries` trait.
287+
288+
You will need to rename the `getExchangeRate` method to `getLatestExchangeRate` and switch its visibility to protected, and implement a new `getHistoricalExchangeRate` method:
289+
290+
```php
291+
use Exchanger\Service\SupportsHistoricalQueries;
292+
293+
class ConstantService extends HttpService
294+
{
295+
use SupportsHistoricalQueries;
296+
297+
/**
298+
* Gets the exchange rate.
299+
*
300+
* @param ExchangeRateQuery $exchangeQuery
301+
*
302+
* @return ExchangeRate
303+
*/
304+
protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRate
305+
{
306+
return $this->createInstantRate($exchangeQuery->getCurrencyPair(), $this->options['value']);
307+
}
308+
309+
/**
310+
* Gets an historical rate.
311+
*
312+
* @param HistoricalExchangeRateQuery $exchangeQuery
313+
*
314+
* @return ExchangeRate
315+
*/
316+
protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRate
317+
{
318+
return $this->createInstantRate($exchangeQuery->getCurrencyPair(), $this->options['value']);
319+
}
320+
}
321+
```
322+
228323
## Supported Services
229324

230325
Here is the complete list of supported services and their possible configurations:
@@ -242,20 +337,19 @@ $swap = (new Builder())
242337
->add('central_bank_of_czech_republic')
243338
->add('russian_central_bank')
244339
->add('webservicex')
245-
->add('google')
246340
->add('cryptonator')
247341
->add('currency_data_feed', ['api_key' => 'secret'])
248342
->add('currency_converter', ['access_key' => 'secret', 'enterprise' => false])
249343
->add('open_exchange_rates', ['app_id' => 'secret', 'enterprise' => false])
250344
->add('xignite', ['token' => 'token'])
251345
->add('array', [
252346
[
253-
'EUR/USD' => new ExchangeRate('1.1'),
347+
'EUR/USD' => 1.1,
254348
'EUR/GBP' => 1.5
255349
],
256350
[
257351
'2017-01-01' => [
258-
'EUR/USD' => new ExchangeRate('1.5')
352+
'EUR/USD' => 1.5
259353
],
260354
'2017-01-03' => [
261355
'EUR/GBP' => 1.3

0 commit comments

Comments
 (0)