1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Money\Models\Currencies\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Money\Models\Currencies\CurrencyTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class HasSerializedCurrenciesTrait. |
9
|
|
|
* |
10
|
|
|
* @property string $currencies |
11
|
|
|
*/ |
12
|
|
|
trait HasSerializedCurrenciesTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var null|CurrencyTrait[] |
16
|
|
|
*/ |
17
|
|
|
protected $currenciesModels = null; |
18
|
|
|
/** |
19
|
|
|
* @var null |
20
|
|
|
*/ |
21
|
|
|
protected $currenciesArray = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param CurrencyTrait $currency |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
public function supportsCurrency($currency) |
29
|
|
|
{ |
30
|
|
|
$currencies = $this->getCurrenciesModels(); |
31
|
|
|
|
32
|
|
|
return isset($currencies[$currency->code]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return null|CurrencyTrait[] |
37
|
|
|
*/ |
38
|
|
|
public function getCurrenciesModels() |
39
|
|
|
{ |
40
|
|
|
if ($this->currenciesModels == null) { |
41
|
|
|
$this->initCurrencies(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->currenciesModels; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function initCurrencies() |
48
|
|
|
{ |
49
|
|
|
$currenciesCodes = $this->getCurrenciesArray(); |
50
|
|
|
$this->currenciesModels = []; |
51
|
|
|
foreach ($currenciesCodes as $code) { |
52
|
|
|
$this->currenciesModels[$code] = $this->initCurrency($code); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return null|array |
58
|
|
|
*/ |
59
|
|
|
public function getCurrenciesArray() |
60
|
|
|
{ |
61
|
|
|
if ($this->currenciesArray === null) { |
62
|
|
|
$this->initCurrenciesArray(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->currenciesArray; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $c |
70
|
|
|
*/ |
71
|
|
|
public function setCurrenciesArray($c) |
72
|
|
|
{ |
73
|
|
|
$this->currenciesArray = $c; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function initCurrenciesArray() |
77
|
|
|
{ |
78
|
|
|
$this->currenciesArray = unserialize($this->currencies); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function serializeCurrencies() |
82
|
|
|
{ |
83
|
|
|
$this->currencies = serialize($this->currenciesArray); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $code |
88
|
|
|
* @return CurrencyTrait |
89
|
|
|
*/ |
90
|
|
|
public function initCurrency($code) |
91
|
|
|
{ |
92
|
|
|
return currencyManager()->getByCode($code); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|