1
|
|
|
<?php namespace Arcanedev\Currencies\Entities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Contracts\Entities\Currency as CurrencyContract; |
4
|
|
|
use Arcanedev\Currencies\Exceptions\CurrencyNotFoundException; |
5
|
|
|
use Arcanedev\Support\Collection; |
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
|
|
|
* Get a currency from the collection by iso code. |
21
|
|
|
* |
22
|
|
|
* @param string $iso |
23
|
|
|
* @param mixed|null $default |
24
|
|
|
* |
25
|
|
|
* @return \Arcanedev\Currencies\Contracts\Entities\Currency |
26
|
|
|
*/ |
27
|
108 |
|
public function get($iso, $default = null) |
28
|
|
|
{ |
29
|
108 |
|
return parent::get($iso, $default); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Load the currencies. |
34
|
|
|
* |
35
|
|
|
* @param array $currencies |
36
|
|
|
* @param bool $includeNonIso |
37
|
|
|
* |
38
|
|
|
* @return self |
39
|
|
|
*/ |
40
|
612 |
|
public function load(array $currencies, $includeNonIso = false) |
41
|
|
|
{ |
42
|
612 |
|
$this->reset(); |
43
|
|
|
|
44
|
612 |
|
foreach ($currencies as $group => $items) { |
45
|
612 |
|
if ($group !== 'iso' && ! $includeNonIso) continue; |
46
|
|
|
|
47
|
612 |
|
$this->loadMany($items, ($group === 'iso')); |
48
|
459 |
|
} |
49
|
|
|
|
50
|
612 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add a currency to the collection. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param array $attributes |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
612 |
|
public function add($key, array $attributes) |
62
|
|
|
{ |
63
|
612 |
|
$key = strtoupper($key); |
64
|
612 |
|
$attributes = compact('key') + $attributes; |
65
|
|
|
|
66
|
612 |
|
return $this->addOne(Currency::make($key, $attributes)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add a Currency object to the collection. |
71
|
|
|
* |
72
|
|
|
* @param \Arcanedev\Currencies\Contracts\Entities\Currency $currency |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
612 |
|
public function addOne(CurrencyContract $currency) |
77
|
|
|
{ |
78
|
612 |
|
$this->put($currency->key, $currency); |
|
|
|
|
79
|
|
|
|
80
|
612 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if the currency exists. |
85
|
|
|
* |
86
|
|
|
* @param string $iso |
87
|
|
|
* |
88
|
|
|
* @return \Arcanedev\Currencies\Contracts\Entities\Currency |
89
|
|
|
* |
90
|
|
|
* @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException |
91
|
|
|
*/ |
92
|
96 |
|
public function findOrFails($iso) |
93
|
|
|
{ |
94
|
96 |
|
$iso = strtoupper($iso); |
95
|
|
|
|
96
|
96 |
|
if ( ! $this->has($iso)) { |
97
|
24 |
|
throw new CurrencyNotFoundException("The Currency with the ISO code [$iso] not found"); |
98
|
|
|
} |
99
|
|
|
|
100
|
72 |
|
return $this->get($iso); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* ------------------------------------------------------------------------------------------------ |
104
|
|
|
| Other Functions |
105
|
|
|
| ------------------------------------------------------------------------------------------------ |
106
|
|
|
*/ |
107
|
|
|
/** |
108
|
|
|
* Add many currencies. |
109
|
|
|
* |
110
|
|
|
* @param array $items |
111
|
|
|
* @param bool $isIso |
112
|
|
|
*/ |
113
|
612 |
|
protected function loadMany($items, $isIso = false) |
114
|
|
|
{ |
115
|
612 |
|
foreach ($items as $key => $attributes) { |
116
|
612 |
|
$attributes = ['is_iso' => $isIso] + $attributes; |
117
|
|
|
|
118
|
612 |
|
$this->add($key, $attributes); |
119
|
459 |
|
} |
120
|
612 |
|
} |
121
|
|
|
} |
122
|
|
|
|
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: