1
|
|
|
<?php namespace Arcanedev\Currencies\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Contracts\Entities\Rate as RateContract; |
4
|
|
|
use Arcanedev\Support\Collection; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class RateCollection |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Currencies\Entities |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class RateCollection extends Collection |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The `from` currency iso. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $from; |
24
|
|
|
|
25
|
|
|
/* ------------------------------------------------------------------------------------------------ |
26
|
|
|
| Getters & Setters |
27
|
|
|
| ------------------------------------------------------------------------------------------------ |
28
|
|
|
*/ |
29
|
|
|
/** |
30
|
|
|
* Get the `from` currency iso. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
24 |
|
public function getFrom() |
35
|
|
|
{ |
36
|
24 |
|
return $this->from; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the `from` currency iso. |
41
|
|
|
* |
42
|
|
|
* @param string $from |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
24 |
|
protected function setFrom($from) |
47
|
|
|
{ |
48
|
24 |
|
$this->from = $from; |
49
|
|
|
|
50
|
24 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* ------------------------------------------------------------------------------------------------ |
54
|
|
|
| Main Functions |
55
|
|
|
| ------------------------------------------------------------------------------------------------ |
56
|
|
|
*/ |
57
|
|
|
/** |
58
|
|
|
* Load rates. |
59
|
|
|
* |
60
|
|
|
* @param string $from |
61
|
|
|
* @param array $rates |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
24 |
|
public function load($from, array $rates) |
66
|
|
|
{ |
67
|
24 |
|
$this->reset(); |
68
|
24 |
|
$this->setFrom($from); |
69
|
|
|
|
70
|
24 |
|
foreach ($rates as $to => $ratio) { |
71
|
24 |
|
$this->add($to, $ratio); |
72
|
18 |
|
} |
73
|
|
|
|
74
|
24 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add a rate to the collection. |
79
|
|
|
* |
80
|
|
|
* @param string $to |
81
|
|
|
* @param double|int $ratio |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
24 |
|
public function add($to, $ratio) |
86
|
|
|
{ |
87
|
24 |
|
return $this->addRate( |
88
|
24 |
|
Rate::make($this->getFrom(), $to, $ratio) |
89
|
18 |
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add a Rate object to the collection. |
94
|
|
|
* |
95
|
|
|
* @param \Arcanedev\Currencies\Contracts\Entities\Rate $rate |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
24 |
|
public function addRate(RateContract $rate) |
100
|
|
|
{ |
101
|
24 |
|
$this->put($rate->to(), $rate); |
102
|
|
|
|
103
|
24 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get a rate by `to` iso. |
108
|
|
|
* |
109
|
|
|
* @param string $key |
110
|
|
|
* @param null $default |
111
|
|
|
* |
112
|
|
|
* @return \Arcanedev\Currencies\Entities\Rate |
113
|
|
|
*/ |
114
|
24 |
|
public function get($key, $default = null) |
115
|
|
|
{ |
116
|
24 |
|
return parent::get($key, $default); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|