You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: doc/readme.md
+131-37
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,17 @@
4
4
*[Installation](#installation)
5
5
*[Configuration](#configuration)
6
6
*[Usage](#usage)
7
+
*[Retrieving Rates](#retrieving-rates)
8
+
*[Rate Provider](#rate-provider)
7
9
*[Cache](#cache)
8
10
*[Rates Caching](#rates-caching)
9
-
*[Cache Options](#cache-options)
11
+
*[Query Cache Options](#query-cache-options)
10
12
*[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)
15
18
16
19
## Installation
17
20
@@ -20,10 +23,10 @@ which provides the http layer used to send requests to exchange rate services.
20
23
This gives you the flexibility to choose what HTTP client and PSR-7 implementation you want to use.
21
24
22
25
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):
@@ -51,6 +54,8 @@ The complete list of all supported services is available [here](#supported-servi
51
54
52
55
## Usage
53
56
57
+
### Retrieving Rates
58
+
54
59
In order to get rates, you can use the `latest()` or `historical()` methods on `Swap`:
55
60
56
61
```php
@@ -69,42 +74,85 @@ $rate = $swap->historical('EUR/USD', (new \DateTime())->modify('-15 days'));
69
74
70
75
> Currencies are expressed as their [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217) code.
71
76
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
+
72
89
## Cache
73
90
74
91
### Rates Caching
75
92
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.
77
94
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).
79
98
80
99
```bash
81
-
$ composer require cache/apcu-adapter
100
+
$ composer require cache/predis-adapter
82
101
```
83
102
84
103
```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);
86
110
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)
89
113
->build();
90
114
```
91
115
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.
93
121
94
-
###Cache Options
122
+
#### cache_ttl
95
123
96
-
You can override `Swap` caching per request:
124
+
Set cache TTL in seconds. Default: `null` - cache entries permanently
97
125
98
126
```php
99
-
// Overrides the global cache ttl to 60 seconds
127
+
// Override the global cache_ttl only for this query
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 `-`
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
161
208
162
209
In the following example, we are creating a `Constant` service that returns a constant rate value.
163
210
164
211
```php
165
-
use Exchanger\Service\Service;
166
212
use Exchanger\Contract\ExchangeRateQuery;
167
-
use Exchanger\ExchangeRate;
213
+
use Exchanger\Contract\ExchangeRate;
214
+
use Exchanger\Service\HttpService;
168
215
use Swap\Service\Registry;
169
-
use Swap\Builder;
170
216
171
-
class ConstantService extends Service
217
+
class ConstantService extends HttpService
172
218
{
173
219
/**
174
220
* Gets the exchange rate.
@@ -177,22 +223,22 @@ class ConstantService extends Service
177
223
*
178
224
* @return ExchangeRate
179
225
*/
180
-
public function getExchangeRate(ExchangeRateQuery $exchangeQuery)
226
+
public function getExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRate
// Register the service so it's available using Builder::add()
@@ -225,6 +281,45 @@ $swap = (new Builder())
225
281
echo $swap->latest('EUR/USD')->getValue();
226
282
```
227
283
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
0 commit comments