1
|
|
|
<?php namespace Arcanedev\Currencies\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Exceptions\CurrencyNotFoundException; |
4
|
|
|
use Arcanedev\Support\Collection; |
5
|
|
|
use Arcanedev\Currencies\Contracts\Entities\Currency as CurrencyContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class CurrencyCollection |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Currencies\Entities |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CurrencyCollection extends Collection |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Main Functions |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* @param array $currencies |
21
|
|
|
* @param bool $includeNonIso |
22
|
|
|
* |
23
|
|
|
* @return self |
24
|
|
|
*/ |
25
|
180 |
|
public function load(array $currencies, $includeNonIso = false) |
26
|
|
|
{ |
27
|
180 |
|
$this->reset(); |
28
|
|
|
|
29
|
180 |
|
foreach ($currencies as $group => $items) { |
30
|
180 |
|
if ($group !== 'iso' && ! $includeNonIso) continue; |
31
|
|
|
|
32
|
180 |
|
$this->loadMany($items, ($group === 'iso')); |
33
|
135 |
|
} |
34
|
|
|
|
35
|
180 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a currency to the collection. |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* @param array $attributes |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
180 |
|
public function add($key, array $attributes) |
47
|
|
|
{ |
48
|
180 |
|
$key = strtoupper($key); |
49
|
180 |
|
$attributes = compact('key') + $attributes; |
50
|
|
|
|
51
|
180 |
|
$this->addOne(Currency::make($key, $attributes)); |
52
|
|
|
|
53
|
180 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add a Currency object to the collection. |
58
|
|
|
* |
59
|
|
|
* @param \Arcanedev\Currencies\Contracts\Entities\Currency $currency |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
180 |
|
public function addOne(CurrencyContract $currency) |
64
|
|
|
{ |
65
|
180 |
|
$this->put($currency->key, $currency); |
|
|
|
|
66
|
|
|
|
67
|
180 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check if the currency exists. |
72
|
|
|
* |
73
|
|
|
* @param string $iso |
74
|
|
|
* |
75
|
|
|
* @return \Arcanedev\Currencies\Entities\Currency |
76
|
|
|
* |
77
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException |
78
|
|
|
*/ |
79
|
24 |
|
public function findOrFails($iso) |
80
|
|
|
{ |
81
|
24 |
|
$iso = strtoupper($iso); |
82
|
|
|
|
83
|
24 |
|
if ( ! $this->has($iso)) { |
84
|
12 |
|
throw new CurrencyNotFoundException("The Currency with the ISO code [$iso] not found"); |
85
|
|
|
} |
86
|
|
|
|
87
|
12 |
|
return $this->get($iso); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/* ------------------------------------------------------------------------------------------------ |
91
|
|
|
| Other Functions |
92
|
|
|
| ------------------------------------------------------------------------------------------------ |
93
|
|
|
*/ |
94
|
|
|
/** |
95
|
|
|
* Add many currencies. |
96
|
|
|
* |
97
|
|
|
* @param array $items |
98
|
|
|
* @param bool $isIso |
99
|
|
|
*/ |
100
|
180 |
|
protected function loadMany($items, $isIso = false) |
101
|
|
|
{ |
102
|
180 |
|
foreach ($items as $key => $attributes) { |
103
|
180 |
|
$attributes = ['is_iso' => $isIso] + $attributes; |
104
|
|
|
|
105
|
180 |
|
$this->add($key, $attributes); |
106
|
135 |
|
} |
107
|
180 |
|
} |
108
|
|
|
} |
109
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: