1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\Form\Dto; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
13
|
|
|
use RunOpenCode\ExchangeRate\Model\Rate as ExchangeRate; |
14
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
15
|
|
|
use RunOpenCode\Bundle\ExchangeRate\Validator\Constraints as ExchangeRateAssert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Rate |
19
|
|
|
* |
20
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\Form\Dto |
21
|
|
|
* |
22
|
|
|
* @ExchangeRateAssert\ExchangeRate() |
23
|
|
|
*/ |
24
|
|
|
class Rate implements RateInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
* |
29
|
|
|
* @Assert\NotBlank() |
30
|
|
|
*/ |
31
|
|
|
private $rate; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \DateTime |
35
|
|
|
* |
36
|
|
|
* @Assert\NotBlank() |
37
|
|
|
* @Assert\DateTime() |
38
|
|
|
*/ |
39
|
|
|
private $date; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var float |
43
|
|
|
* |
44
|
|
|
* @Assert\NotBlank() |
45
|
|
|
* @Assert\Type(type="float") |
46
|
|
|
*/ |
47
|
|
|
private $value; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
* |
52
|
|
|
* @Assert\NotBlank() |
53
|
|
|
* @ExchangeRateAssert\BaseCurrency() |
54
|
|
|
*/ |
55
|
|
|
private $baseCurrencyCode; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Rate constructor. |
59
|
|
|
* |
60
|
|
|
* @param null $rate |
61
|
|
|
* @param \DateTime|null $date |
62
|
|
|
* @param null $value |
63
|
|
|
*/ |
64
|
11 |
|
public function __construct($rate = null, \DateTime $date = null, $value = null, $baseCurrencyCode = null) |
65
|
|
|
{ |
66
|
11 |
|
$this->rate = $rate; |
67
|
11 |
|
$this->date = $date; |
68
|
11 |
|
$this->baseCurrencyCode = $baseCurrencyCode; |
69
|
11 |
|
$this->setValue($value); |
70
|
11 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
11 |
|
public function getRate() |
76
|
|
|
{ |
77
|
11 |
|
return $this->rate; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $rate |
82
|
|
|
* @return Rate |
83
|
|
|
*/ |
84
|
8 |
|
public function setRate($rate) |
85
|
|
|
{ |
86
|
8 |
|
$this->rate = $rate; |
87
|
8 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \DateTime |
92
|
|
|
*/ |
93
|
9 |
|
public function getDate() |
94
|
|
|
{ |
95
|
9 |
|
return $this->date; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \DateTime $date |
100
|
|
|
* @return Rate |
101
|
|
|
*/ |
102
|
8 |
|
public function setDate(\DateTime $date) |
103
|
|
|
{ |
104
|
8 |
|
$this->date = $date; |
105
|
8 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return float |
110
|
|
|
*/ |
111
|
9 |
|
public function getValue() |
112
|
|
|
{ |
113
|
9 |
|
return $this->value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param float $value |
118
|
|
|
* @return Rate |
119
|
|
|
*/ |
120
|
11 |
|
public function setValue($value) |
121
|
|
|
{ |
122
|
11 |
|
$this->value = (is_numeric($value)) ? (float) $value : $value; |
123
|
11 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
1 |
|
public function getBaseCurrencyCode() |
130
|
|
|
{ |
131
|
1 |
|
return $this->baseCurrencyCode; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $baseCurrencyCode |
136
|
|
|
* @return Rate |
137
|
|
|
*/ |
138
|
4 |
|
public function setBaseCurrencyCode($baseCurrencyCode) |
139
|
|
|
{ |
140
|
4 |
|
$this->baseCurrencyCode = $baseCurrencyCode; |
141
|
4 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
9 |
View Code Duplication |
public function getSourceName() |
|
|
|
|
148
|
|
|
{ |
149
|
9 |
|
if (false !== strpos($this->getRate(), '.')) { |
150
|
8 |
|
return explode('.', $this->getRate())[0]; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
9 |
View Code Duplication |
public function getCurrencyCode() |
|
|
|
|
160
|
|
|
{ |
161
|
9 |
|
if (false !== strpos($this->getRate(), '.')) { |
162
|
8 |
|
return explode('.', $this->getRate())[2]; |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
9 |
View Code Duplication |
public function getRateType() |
|
|
|
|
172
|
|
|
{ |
173
|
9 |
|
if (false !== strpos($this->getRate(), '.')) { |
174
|
8 |
|
return explode('.', $this->getRate())[1]; |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
1 |
|
public function getCreatedAt() |
184
|
|
|
{ |
185
|
1 |
|
return null; // unknown |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
1 |
|
public function getModifiedAt() |
192
|
|
|
{ |
193
|
1 |
|
return null; // unknown |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Build \RunOpenCode\ExchangeRate\Model\Rate from DTO object. |
199
|
|
|
* |
200
|
|
|
* @param ExchangeRate|null $exchangeRate Initial rate object that can be used for data population |
201
|
|
|
* @return ExchangeRate |
202
|
|
|
*/ |
203
|
7 |
|
public function toRate(ExchangeRate $exchangeRate = null) |
204
|
|
|
{ |
205
|
7 |
|
list ($sourceName, $rateType, $currencyCode) = explode('.', $this->getRate()); |
206
|
|
|
|
207
|
7 |
|
return new ExchangeRate( |
208
|
|
|
$sourceName, |
209
|
7 |
|
$this->getValue(), |
210
|
|
|
$currencyCode, |
211
|
|
|
$rateType, |
212
|
7 |
|
$this->getDate(), |
213
|
7 |
|
$this->baseCurrencyCode, |
214
|
7 |
|
(null !== $exchangeRate) ? $exchangeRate->getCreatedAt() : new \DateTime('now'), |
215
|
7 |
|
new \DateTime('now') |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Create DTO object from RateInterface. |
221
|
|
|
* |
222
|
|
|
* @param RateInterface $exchangeRate |
223
|
|
|
* @return Rate |
224
|
|
|
*/ |
225
|
4 |
|
public static function fromRateInterface(RateInterface $exchangeRate) |
226
|
|
|
{ |
227
|
4 |
|
$rate = new static(); |
228
|
|
|
|
229
|
|
|
$rate |
230
|
4 |
|
->setBaseCurrencyCode($exchangeRate->getBaseCurrencyCode()) |
231
|
4 |
|
->setDate($exchangeRate->getDate()) |
232
|
4 |
|
->setValue($exchangeRate->getValue()) |
233
|
4 |
|
->setRate(sprintf('%s.%s.%s', $exchangeRate->getSourceName(), $exchangeRate->getRateType(), $exchangeRate->getCurrencyCode())); |
234
|
|
|
|
235
|
4 |
|
return $rate; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.