|
1
|
|
|
<?php namespace Arcanedev\Currencies\Services; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Exceptions\ApiException; |
|
4
|
|
|
use Illuminate\Support\Arr; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class CurrencyLayerService |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\Currencies\Services |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class CurrencyLayerService extends AbstractApiService |
|
13
|
|
|
{ |
|
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
*/ |
|
18
|
|
|
/** |
|
19
|
|
|
* The base URL. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $baseUrl = 'http://apilayer.net/api'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The Access Key. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $accessKey; |
|
31
|
|
|
|
|
32
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
33
|
|
|
| Getters & Setters |
|
34
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
35
|
|
|
*/ |
|
36
|
|
|
/** |
|
37
|
|
|
* Set the configs. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $configs |
|
40
|
|
|
*/ |
|
41
|
48 |
|
protected function setConfigs(array $configs) |
|
42
|
|
|
{ |
|
43
|
48 |
|
$this->accessKey = Arr::get($configs, 'access-key'); |
|
44
|
48 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the Access Key. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\ApiException |
|
52
|
|
|
*/ |
|
53
|
24 |
|
private function getAccessKey() |
|
54
|
|
|
{ |
|
55
|
24 |
|
if ( ! $this->accessKey) { |
|
56
|
|
|
throw new ApiException('Currencylayer.com requires an access key.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
24 |
|
return $this->accessKey; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get cache key. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getCacheKey() |
|
68
|
|
|
{ |
|
69
|
|
|
return parent::getCacheKey() . '.currencylayer'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
73
|
|
|
| Main Functions |
|
74
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
75
|
|
|
*/ |
|
76
|
|
|
/** |
|
77
|
|
|
* Make an API request. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $from |
|
80
|
|
|
* @param array $to |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Arcanedev\Currencies\Entities\RateCollection |
|
83
|
|
|
*/ |
|
84
|
24 |
|
protected function request($from, array $to = []) |
|
85
|
|
|
{ |
|
86
|
24 |
|
$response = $this->client->get('/live', [ |
|
87
|
24 |
|
'access_key' => $this->getAccessKey(), |
|
88
|
24 |
|
'source' => $from, |
|
89
|
24 |
|
'currencies' => empty($to) ? null : implode(',', $to), |
|
90
|
18 |
|
]); |
|
91
|
|
|
|
|
92
|
24 |
|
$rates = []; |
|
93
|
24 |
|
$data = json_decode($response, true); |
|
94
|
|
|
|
|
95
|
24 |
|
foreach ($data['quotes'] as $key => $ratio) { |
|
96
|
24 |
|
$rates[substr($key, 3)] = $ratio; |
|
97
|
18 |
|
} |
|
98
|
|
|
|
|
99
|
24 |
|
return $rates; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.