1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, 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\ExchangeRate\Utils; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class RateFilterUtil |
16
|
|
|
* |
17
|
|
|
* Utility to check if rate matches filters. |
18
|
|
|
* |
19
|
|
|
* @package RunOpenCode\ExchangeRate\Utils |
20
|
|
|
*/ |
21
|
|
|
final class RateFilterUtil |
22
|
|
|
{ |
23
|
|
|
use FilterUtilHelper; |
24
|
|
|
|
25
|
|
|
private function __construct() { /* noop */ } |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if rate matches filters. |
29
|
|
|
* |
30
|
|
|
* @param RateInterface $rate Rate to filter. |
31
|
|
|
* @param array $criteria Filter criteria. |
32
|
|
|
* @return bool TRUE if filter criteria is matched. |
33
|
|
|
*/ |
34
|
8 |
|
public static function matches(RateInterface $rate, array $criteria) |
35
|
|
|
{ |
36
|
|
|
return |
37
|
8 |
|
self::matchesArrayCriteria('currencyCode', $rate, $criteria) |
38
|
|
|
&& |
39
|
8 |
|
self::matchesArrayCriteria('sourceName', $rate, $criteria) |
40
|
|
|
&& |
41
|
8 |
|
self::matchesArrayCriteria('rateType', $rate, $criteria) |
42
|
|
|
&& |
43
|
8 |
|
self::matchesDateCriteria('onDate', $rate, $criteria) |
44
|
|
|
&& |
45
|
8 |
|
self::matchesDateCriteria('dateFrom', $rate, $criteria) |
46
|
|
|
&& |
47
|
8 |
|
self::matchesDateCriteria('dateTo', $rate, $criteria) |
48
|
|
|
; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if date criteria is matched. |
53
|
|
|
* |
54
|
|
|
* @param string $key Date criteria key. |
55
|
|
|
* @param RateInterface $rate Rate to check for match. |
56
|
|
|
* @param array $criteria Filter criterias. |
57
|
|
|
* @return bool TRUE if there is a match. |
58
|
|
|
*/ |
59
|
8 |
|
private static function matchesDateCriteria($key, RateInterface $rate, array $criteria) |
60
|
|
|
{ |
61
|
8 |
|
$date = self::extractDateCriteria($key, $criteria); |
62
|
|
|
|
63
|
8 |
|
if ($date === null) { |
64
|
8 |
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
View Code Duplication |
if ($key === 'dateFrom') { |
|
|
|
|
68
|
3 |
|
$rateDate = new \DateTime($rate->getDate()->format(\DateTime::ATOM)); |
69
|
3 |
|
$rateDate->setTime(23, 59, 59); |
70
|
3 |
|
return $date <= $rateDate; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
View Code Duplication |
if ($key === 'dateTo') { |
|
|
|
|
74
|
3 |
|
$rateDate = new \DateTime($rate->getDate()->format(\DateTime::ATOM)); |
75
|
3 |
|
$rateDate->setTime(0, 0, 0); |
76
|
3 |
|
return $date >= $rateDate; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return $date->format('Y-m-d') === $rate->getDate()->format('Y-m-d'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.