2
2
3
3
namespace AmrShawky \Currency ;
4
4
5
- use AmrShawky \Currency \Traits \HttpRequest ;
6
5
use AmrShawky \Currency \Traits \ParamsOverload ;
7
6
use GuzzleHttp \Client ;
8
7
9
- class CurrencyConversion
8
+ class CurrencyConversion extends API
10
9
{
11
- use HttpRequest, ParamsOverload;
12
-
10
+ use ParamsOverload;
13
11
/**
14
12
* @var string
15
13
*/
16
- private $ base_url = 'https://api.exchangerate.host/convert ' ;
14
+ protected $ base_url = 'https://api.exchangerate.host/convert ' ;
17
15
18
16
/**
19
17
* Required base currency
@@ -30,34 +28,55 @@ class CurrencyConversion
30
28
private $ to = null ;
31
29
32
30
/**
33
- * @var float
31
+ * @var null
34
32
*/
35
- private $ amount = 1.00 ;
33
+ private $ places = null ;
36
34
37
35
/**
38
- * @var array
36
+ * @var float
39
37
*/
40
- private $ params = [] ;
38
+ private $ amount = 1.00 ;
41
39
42
40
/**
43
41
* @var array
44
42
*/
45
- private $ query_params = [];
46
-
47
- private $ available_params = [
43
+ protected $ available_params = [
48
44
'round ' ,
49
45
'date ' ,
50
- 'source '
46
+ 'source ' ,
47
+ 'places '
51
48
];
52
49
53
50
/**
54
- * @var
51
+ * CurrencyConversion constructor.
52
+ *
53
+ * @param Client|null $client
55
54
*/
56
- private $ client ;
57
-
58
- public function __construct (Client $ client )
55
+ public function __construct (?Client $ client = null )
59
56
{
60
- $ this ->client = $ client ;
57
+ $ this ->client = $ client ?? new Client ();
58
+
59
+ $ this ->setQueryParams (function () {
60
+ if (!$ this ->from ) {
61
+ throw new \Exception ('Base currency is not specified! ' );
62
+ }
63
+
64
+ if (!$ this ->to ) {
65
+ throw new \Exception ('Target currency is not specified! ' );
66
+ }
67
+
68
+ $ params = [
69
+ 'from ' => $ this ->from ,
70
+ 'to ' => $ this ->to ,
71
+ 'amount ' => $ this ->amount
72
+ ];
73
+
74
+ if ($ this ->places ) {
75
+ $ params ['places ' ] = $ this ->places ;
76
+ }
77
+
78
+ return $ params ;
79
+ });
61
80
}
62
81
63
82
/**
@@ -82,6 +101,17 @@ public function to(string $currency)
82
101
return $ this ;
83
102
}
84
103
104
+ /**
105
+ * @param $places
106
+ *
107
+ * @return $this
108
+ */
109
+ public function round (int $ places )
110
+ {
111
+ $ this ->places = $ places ;
112
+ return $ this ;
113
+ }
114
+
85
115
/**
86
116
* @param float $amount
87
117
*
@@ -94,42 +124,12 @@ public function amount(float $amount)
94
124
}
95
125
96
126
/**
97
- * @throws \Exception
127
+ * @param object $response
98
128
*
99
- * @return float |null
129
+ * @return mixed |null
100
130
*/
101
- public function get ( )
131
+ protected function getResults ( object $ response )
102
132
{
103
- if (!$ this ->from ) {
104
- throw new \Exception ('Base currency is not specified! ' );
105
- }
106
-
107
- if (!$ this ->to ) {
108
- throw new \Exception ('Target currency is not specified! ' );
109
- }
110
-
111
- $ this ->buildQueryParams ();
112
-
113
- $ response = $ this ->request (
114
- $ this ->base_url ,
115
- $ this ->query_params
116
- );
117
-
118
133
return $ response ->result ?? null ;
119
134
}
120
-
121
- private function buildQueryParams ()
122
- {
123
- $ this ->query_params = [
124
- 'from ' => $ this ->from ,
125
- 'to ' => $ this ->to ,
126
- 'amount ' => $ this ->amount
127
- ];
128
-
129
- if (!empty ($ this ->params )) {
130
- foreach ($ this ->params as $ key => $ param ) {
131
- $ this ->query_params [$ key ] = $ param ;
132
- }
133
- }
134
- }
135
135
}
0 commit comments