Completed
Push — master ( 87f113...1d200b )
by Milan
03:04
created

Exchange::getDefaultFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace h4kuna\Exchange;
4
5
use DateTime;
6
7
/**
8
 * @author Milan Matějček
9
 * @since 2009-06-22 - version 0.5
10
 */
11
class Exchange implements \ArrayAccess, \IteratorAggregate
12
{
13
14
	/** @var Caching\ICache */
15
	private $cache;
16
17
	/** @var \DateTime */
18
	private $date;
19
20
	/** @var Currency\ListRates */
21
	private $listRates;
22
23
	/**
24
	 * Default currency "from" input
25
	 * @var Currency\Property
26
	 */
27
	private $default;
28
29
	/**
30
	 * Display currency "to" output
31
	 * @var Currency\Property
32
	 */
33
	private $output;
34
35
	/** @var Currency\Property[] */
36
	private $tempRates;
37
38
	public function __construct(Caching\ICache $cache)
39
	{
40
		$this->cache = $cache;
41
	}
42
43
	/** @return Currency\Property */
44
	public function getDefault()
45
	{
46
		if ($this->default === NULL) {
47
			$this->default = $this->getListRates()->getFirst();
48
		}
49
		return $this->default;
50
	}
51
52
	/** @return Currency\Property */
53
	public function getOutput()
54
	{
55
		if ($this->output === NULL) {
56
			$this->output = $this->getDefault();
57
		}
58
		return $this->output;
59
	}
60
61
	/**
62
	 * Set default "from" currency.
63
	 * @param string $code
64
	 */
65
	public function setDefault($code)
66
	{
67
		$this->default = $this->offsetGet($code);
68
	}
69
70
	/**
71
	 * @param DateTime $date
72
	 * @return self
73
	 */
74
	public function setDate(DateTime $date)
75
	{
76
		$this->date = $date;
77
		$this->listRates = NULL;
78
		return $this;
79
	}
80
81
	/**
82
	 * @param Driver\ADriver $driver
83
	 * @return self
84
	 */
85
	public function setDriver(Driver\ADriver $driver)
86
	{
87
		$this->listRates = $this->cache->loadListRate($driver, $this->date);
88
		return $this;
89
	}
90
91
	/**
92
	 * Set currency "to".
93
	 * @param string $code
94
	 */
95
	public function setOutput($code)
96
	{
97
		return $this->output = $this->offsetGet($code);
98
	}
99
100
	/**
101
	 * Transfer number by exchange rate.
102
	 * @param float|int|string $price number
103
	 * @param string|NULL
104
	 * @param string $to
105
	 * @return float|int
106
	 */
107
	public function change($price, $from = NULL, $to = NULL)
108
	{
109
		return $this->transfer($price, $from, $to)[0];
110
	}
111
112
	/**
113
	 * @param int|float $price
114
	 * @param string $from
115
	 * @param string $to
116
	 * @return array
117
	 */
118
	public function transfer($price, $from, $to)
119
	{
120
		$to = $to === NULL ? $this->getOutput() : $this->offsetGet($to);
121
		if (((float) $price) === 0.0) {
122
			return [0, $to];
123
		}
124
125
		$from = $from === NULL ? $this->getDefault() : $this->offsetGet($from);
126
127
		if ($to !== $from) {
128
			$toRate = isset($this->tempRates[$to->code]) ? $this->tempRates[$to->code] : $to->rate;
129
			$fromRate = isset($this->tempRates[$from->code]) ? $this->tempRates[$from->code] : $from->rate;
130
			$price *= $fromRate / $toRate;
131
		}
132
133
		return [$price, $to];
134
	}
135
136
	/**
137
	 * Add history rate for rating
138
	 * @param string $code
139
	 * @param float $rate
140
	 * @return self
141
	 */
142
	public function addRate($code, $rate)
143
	{
144
		$property = $this->offsetGet($code);
145
		$this->tempRates[$property->code] = $rate;
146
		return $this;
147
	}
148
149
	/**
150
	 * Remove history rating
151
	 * @param string $code
152
	 * @return self
153
	 */
154
	public function removeRate($code)
155
	{
156
		$property = $this->offsetGet($code);
157
		unset($this->tempRates[$property->code]);
158
		return $this;
159
	}
160
161
	/**
162
	 * Load currency property.
163
	 * @param string|Currency\Property $index
164
	 * @return Currency\Property
165
	 */
166
	public function offsetGet($index)
167
	{
168
		$index = strtoupper($index);
169
		if ($this->getListRates()->offsetExists($index)) {
170
			return $this->getListRates()->offsetGet($index);
171
		}
172
		throw new UnknownCurrencyException('Undefined currency code: "' . $index . '".');
173
	}
174
175
	public function offsetExists($offset)
176
	{
177
		return $this->getListRates()->offsetExists(strtoupper($offset));
178
	}
179
180
	public function offsetSet($offset, $value)
181
	{
182
		return $this->getListRates()->offsetSet($offset, $value);
183
	}
184
185
	public function offsetUnset($offset)
186
	{
187
		return $this->getListRates()->offsetUnset($offset);
188
	}
189
190
	public function getIterator()
191
	{
192
		return $this->getListRates();
193
	}
194
195
	/**
196
	 * @return Currency\ListRates
197
	 */
198
	protected function getListRates()
199
	{
200
		if ($this->listRates === NULL) {
201
			$this->listRates = $this->cache->loadListRate(new Driver\Cnb\Day(), $this->date);
202
		}
203
		return $this->listRates;
204
	}
205
206
}
207