|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace h4kuna\Exchange\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime, |
|
6
|
|
|
h4kuna\Exchange; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Download currency from server. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class ADriver |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** @var \DateTime */ |
|
15
|
|
|
private $date; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Download data from remote source and save. |
|
19
|
|
|
* @param DateTime $date |
|
20
|
|
|
* @param array $allowedCurrencies |
|
21
|
|
|
* @return Exchange\Currency\ListRates |
|
22
|
|
|
*/ |
|
23
|
|
|
public function download(DateTime $date = NULL, array $allowedCurrencies = []) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($allowedCurrencies) { |
|
|
|
|
|
|
26
|
|
|
$allowedCurrencies = array_flip($allowedCurrencies); |
|
27
|
|
|
} |
|
28
|
|
|
$source = $this->loadFromSource($date); |
|
29
|
|
|
$currencies = new Exchange\Currency\ListRates($this->getDate()); |
|
30
|
|
|
foreach ($source as $row) { |
|
31
|
|
|
if (!$row) { |
|
32
|
|
|
continue; |
|
33
|
|
|
} |
|
34
|
|
|
$property = $this->createProperty($row); |
|
35
|
|
|
|
|
36
|
|
|
if (!$property || !$property->rate || ($allowedCurrencies !== [] && !isset($allowedCurrencies[$property->code]))) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
$currencies->addProperty($property); |
|
40
|
|
|
} |
|
41
|
|
|
$currencies->getFirst(); // check if is not empty |
|
42
|
|
|
return $currencies; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function setDate($format, $value) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->date = DateTime::createFromFormat($format, $value); |
|
|
|
|
|
|
48
|
|
|
$this->date->setTime(0, 0, 0); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getName() |
|
52
|
|
|
{ |
|
53
|
|
|
return strtolower(str_replace('\\', '.', static::class)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return DateTime |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDate() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->date; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Load data for iterator. |
|
66
|
|
|
* @param DateTime|NULL $date |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
abstract protected function loadFromSource(DateTime $date = NULL); |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Modify data before save to cache. |
|
73
|
|
|
* @return Exchange\Currency\Property|NULL |
|
74
|
|
|
*/ |
|
75
|
|
|
abstract protected function createProperty($row); |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.