1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 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() { } |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if rate matches filters. |
29
|
|
|
* |
30
|
|
|
* @param RateInterface $rate Rate to filter. |
31
|
|
|
* @param array $criteria Filter criteria. |
32
|
12 |
|
* @return bool TRUE if filter criteria is matched. |
33
|
|
|
*/ |
34
|
|
|
public static function matches(RateInterface $rate, array $criteria) |
35
|
12 |
|
{ |
36
|
6 |
|
return |
37
|
12 |
|
self::matchesArrayCriteria('currencyCode', $rate, $criteria) |
38
|
6 |
|
&& |
39
|
12 |
|
self::matchesArrayCriteria('sourceName', $rate, $criteria) |
40
|
6 |
|
&& |
41
|
12 |
|
self::matchesArrayCriteria('rateType', $rate, $criteria) |
42
|
6 |
|
&& |
43
|
12 |
|
self::matchesDateCriteria('onDate', $rate, $criteria) |
44
|
6 |
|
&& |
45
|
12 |
|
self::matchesDateCriteria('dateFrom', $rate, $criteria) |
46
|
6 |
|
&& |
47
|
|
|
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
|
12 |
|
* @return bool TRUE if there is a match. |
58
|
|
|
*/ |
59
|
12 |
|
private static function matchesDateCriteria($key, RateInterface $rate, array $criteria) |
60
|
|
|
{ |
61
|
12 |
|
$date = self::extractDateCriteria($key, $criteria); |
62
|
12 |
|
|
63
|
|
|
if ($date === null) { |
64
|
|
|
return true; |
65
|
2 |
|
} |
66
|
2 |
|
|
67
|
2 |
|
if ($key === 'dateFrom') { |
68
|
2 |
|
return $date <= $rate->getDate(); |
69
|
|
|
} elseif ($key === 'dateTo') { |
70
|
|
|
return $date >= $rate->getDate(); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
return $date->format('Y-m-d') === $rate->getDate()->format('Y-m-d'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|