Completed
Push — master ( 7383a6...06d9f7 )
by ARCANEDEV
19s
created

CurrencyManager::currencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\Currencies;
2
3
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract;
4
use Arcanedev\Currencies\Entities\CurrencyCollection;
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class     CurrencyManager
9
 *
10
 * @package  Arcanedev\Currencies
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class CurrencyManager implements CurrencyManagerContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Default currency.
21
     *
22
     * @var string
23
     */
24
    protected $default;
25
26
    /**
27
     * Supported currencies.
28
     *
29
     * @var array
30
     */
31
    protected $supported      = [];
32
33
    /**
34
     * Non ISO Currencies included.
35
     *
36
     * @var bool
37
     */
38
    protected $nonIsoIncluded = false;
39
40
    /**
41
     * The currencies collection.
42
     *
43
     * @var  \Arcanedev\Currencies\Entities\CurrencyCollection
44
     */
45
    protected $currencies;
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Constructor
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * CurrencyManager constructor.
53
     *
54
     * @param  array  $configs
55
     */
56 384
    public function __construct(array $configs)
57
    {
58 384
        $this->default        = Arr::get($configs, 'default', 'USD');
59 384
        $this->supported      = Arr::get($configs, 'supported', ['USD']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...pported', array('USD')) of type * is incompatible with the declared type array of property $supported.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60 384
        $this->nonIsoIncluded = Arr::get($configs, 'include-non-iso', false);
61 384
        $this->currencies     = new CurrencyCollection;
62 384
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  Getters & Setters
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Get the default currency iso code.
70
     *
71
     * @return string
72
     */
73 12
    public function getDefault()
74
    {
75 12
        return $this->default;
76
    }
77
78
    /**
79
     * Get the default currency entity.
80
     *
81
     * @return \Arcanedev\Currencies\Contracts\Entities\Currency
82
     */
83 12
    public function getDefaultCurrency()
84
    {
85 12
        return $this->currencies->get($this->getDefault());
86
    }
87
88
    /**
89
     * Get supported currencies (iso codes).
90
     *
91
     * @return array
92
     */
93 24
    public function getSupported()
94
    {
95 24
        return $this->supported;
96
    }
97
98
    /**
99
     * Get the currencies collection.
100
     *
101
     * @return \Arcanedev\Currencies\Entities\CurrencyCollection
102
     */
103 384
    public function currencies()
104
    {
105 384
        return $this->currencies;
106
    }
107
108
    /* ------------------------------------------------------------------------------------------------
109
     |  Main Functions
110
     | ------------------------------------------------------------------------------------------------
111
     */
112
    /**
113
     * Get a currency from the collection by iso code.
114
     *
115
     * @param  string      $iso
116
     * @param  mixed|null  $default
117
     *
118
     * @return \Arcanedev\Currencies\Entities\Currency
119
     */
120 12
    public function get($iso, $default = null)
121
    {
122 12
        return $this->currencies()->get($iso, $default);
123
    }
124
125
    /**
126
     * Get a currency or fail if not exists.
127
     *
128
     * @param  string  $iso
129
     *
130
     * @return \Arcanedev\Currencies\Entities\Currency
131
     *
132
     * @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException
133
     */
134 24
    public function findOrFail($iso)
135
    {
136 24
        return $this->currencies()->findOrFails($iso);
137
    }
138
139
    /**
140
     * Get the supported currencies collection.
141
     *
142
     * @return \Arcanedev\Currencies\Entities\CurrencyCollection
143
     */
144 12
    public function getSupportedCurrencies()
145
    {
146 12
        $currencies = $this->currencies();
147 12
        $supported  = array_map(function ($iso) use ($currencies) {
148 12
            return $currencies->findOrFails($iso);
149 12
        }, array_combine($this->getSupported(), $this->getSupported()));
150
151 12
        return CurrencyCollection::make($supported);
152
    }
153
154
    /**
155
     * Load the currencies.
156
     *
157
     * @param  array  $currencies
158
     *
159
     * @return self
160
     */
161 384
    public function load(array $currencies)
162
    {
163 384
        $this->currencies()->load($currencies, $this->nonIsoIncluded);
164
165 384
        return $this;
166
    }
167
168
    /**
169
     * Format the amount.
170
     *
171
     * @param  string  $iso
172
     * @param  int     $amount
173
     * @param  int     $decimals
174
     *
175
     * @return string
176
     *
177
     * @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException
178
     */
179
    public function format($iso, $amount, $decimals = 2)
180
    {
181
        return $this->currencies()
182
            ->findOrFails($iso)
183
            ->format($amount, $decimals);
184
    }
185
186
    /* ------------------------------------------------------------------------------------------------
187
     |  Check Functions
188
     | ------------------------------------------------------------------------------------------------
189
     */
190
    /**
191
     * Check if non ISO Currencies included.
192
     *
193
     * @return bool
194
     */
195
    public function isNonIsoIncluded()
196
    {
197
        return $this->nonIsoIncluded;
198
    }
199
}
200