1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RunOpenCode\ExchangeRate\Utils; |
4
|
|
|
|
5
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
6
|
|
|
|
7
|
|
|
final class RateFilter |
8
|
|
|
{ |
9
|
|
|
private function __construct() { } |
10
|
|
|
|
11
|
|
|
public static function matches(RateInterface $rate, array $criteria) |
12
|
|
|
{ |
13
|
|
|
return |
14
|
|
|
self::matchesCurrencyCodes($rate, self::extractCurrencyCodes($criteria)) |
15
|
|
|
&& |
16
|
|
|
self::matchesSources($rate, self::extractSources($criteria)) |
17
|
|
|
&& |
18
|
|
|
self::matchesRateTypes($rate, self::extractRateTypes($criteria)) |
19
|
|
|
&& |
20
|
|
|
self::matchesOnDate($rate, self::extractOnDate($criteria)) |
21
|
|
|
&& |
22
|
|
|
self::matchesDateFrom($rate, self::extractDateFrom($criteria)) |
23
|
|
|
&& |
24
|
|
|
self::matchesDateTo($rate, self::extractDateTo($criteria)) |
25
|
|
|
; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public static function matchesCurrencyCodes(RateInterface $rate, $currencyCodes) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
if (!is_array($currencyCodes)) { |
31
|
|
|
$currencyCodes = array($currencyCodes); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (empty($currencyCodes)) { |
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return in_array($rate->getCurrencyCode(), $currencyCodes); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public static function matchesSources(RateInterface $rate, $sources) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (!is_array($sources)) { |
44
|
|
|
$sources = array($sources); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (empty($sources)) { |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return in_array($rate->getSourceName(), $sources); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public static function matchesRateTypes(RateInterface $rate, $rateTypes) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (!is_array($rateTypes)) { |
57
|
|
|
$rateTypes = array($rateTypes); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($rateTypes)) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return in_array($rate->getRateType(), $rateTypes); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public static function matchesDateFrom(RateInterface $rate, \DateTime $dateFrom = null) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (is_null($dateFrom)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $dateFrom <= $rate->getDate(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public static function matchesDateTo(RateInterface $rate, \DateTime $dateTo = null) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (is_null($dateTo)) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $dateTo >= $rate->getDate(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function matchesOnDate(RateInterface $rate, \DateTime $date = null) |
86
|
|
|
{ |
87
|
|
|
if (is_null($date)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $date->format('Y-m-d') == $rate->getDate()->format('Y-m-d'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private static function extractSources(array $criteria) |
95
|
|
|
{ |
96
|
|
|
return !empty($criteria['currencyCode']) ? array($criteria['currencyCode']) : !empty($criteria['currencyCodes']) ? $criteria['currencyCodes'] : array(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private static function extractCurrencyCodes(array $criteria) |
100
|
|
|
{ |
101
|
|
|
return !empty($criteria['source']) ? array($criteria['source']) : !empty($criteria['sources']) ? $criteria['sources'] : array(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private static function extractRateTypes(array $criteria) |
105
|
|
|
{ |
106
|
|
|
return !empty($criteria['rateType']) ? array($criteria['rateType']) : !empty($criteria['rateTypes']) ? $criteria['rateTypes'] : array(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private static function extractOnDate(array $criteria) |
110
|
|
|
{ |
111
|
|
|
return (!empty($criteria['onDate'])) ? $criteria['onDate'] : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private static function extractDateFrom(array $criteria) |
115
|
|
|
{ |
116
|
|
|
return (!empty($criteria['dateFrom'])) ? $criteria['dateFrom'] : null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private static function extractDateTo(array $criteria) |
120
|
|
|
{ |
121
|
|
|
return (!empty($criteria['dateTo'])) ? $criteria['dateTo'] : null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
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.