Completed
Push — master ( 6a43ea...7edb4c )
by Nikola
45:00
created

RateFilterUtil::matchesDateCriteria()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 3
crap 4
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
    private function __construct() { }
24
25
    /**
26
     * Check if rate matches filters.
27
     *
28
     * @param RateInterface $rate Rate to filter.
29
     * @param array $criteria Filter criteria.
30
     * @return bool TRUE if filter criteria is matched.
31
     */
32 8
    public static function matches(RateInterface $rate, array $criteria)
33
    {
34
        return
35 8
            self::matchesArrayCriteria('currencyCode', $rate, $criteria)
36 4
            &&
37 8
            self::matchesArrayCriteria('sourceName', $rate, $criteria)
38 4
            &&
39 8
            self::matchesArrayCriteria('rateType', $rate, $criteria)
40 4
            &&
41 8
            self::matchesDateCriteria('onDate', $rate, $criteria)
42 4
            &&
43 8
            self::matchesDateCriteria('dateFrom', $rate, $criteria)
44 4
            &&
45 8
            self::matchesDateCriteria('dateTo', $rate, $criteria)
46 4
            ;
47
    }
48
49
    /**
50
     * Check if date criteria is matched.
51
     *
52
     * @param string $key Date criteria key.
53
     * @param RateInterface $rate Rate to check for match.
54
     * @param array $criteria Filter criterias.
55
     * @return bool TRUE if there is a match.
56
     */
57 8
    private static function matchesDateCriteria($key, RateInterface $rate, array $criteria)
58
    {
59 8
        $date = self::extractDateCriteria($key, $criteria);
60
61 8
        if ($date === null) {
62 8
            return true;
63
        }
64
65 2
        if ($key === 'dateFrom') {
66 2
            return $date <= $rate->getDate();
67 2
        } elseif ($key === 'dateTo') {
68 2
            return $date >= $rate->getDate();
69
        }
70
71 2
        return $date->format('Y-m-d') === $rate->getDate()->format('Y-m-d');
72
    }
73
74
    /**
75
     * Check if array|string criteria is matched.
76
     *
77
     * @param string $key Array|string criteria key.
78
     * @param RateInterface $rate Rate to check for match.
79
     * @param array $criteria Filter criterias.
80
     * @return bool TRUE if there is a match.
81
     */
82 8
    private static function matchesArrayCriteria($key, RateInterface $rate, array $criteria)
83
    {
84 8
        $criteria = self::extractArrayCriteria($key, $criteria);
85
86 8
        if (count($criteria) === 0) {
87 8
            return true;
88
        }
89
90 6
        return in_array($rate->{sprintf('get%s', ucfirst($key))}(), $criteria, true);
91
    }
92
93
    /**
94
     * Extract array criteria from criterias.
95
     *
96
     * @param string $key Criteria name.
97
     * @param array $criteria Filter criterias.
98
     * @return array Extracted array criterias.
99
     */
100 8
    private static function extractArrayCriteria($key, array $criteria)
101
    {
102 8
        if (!empty($criteria[$key])) {
103 6
            return array($criteria[$key]);
104 8
        } elseif (!empty($criteria[$key . 's'])) {
105 6
            return $criteria[$key . 's'];
106
        }
107
108 8
        return array();
109
    }
110
111
    /**
112
     * Extract date from filter criterias.
113
     *
114
     * @param string $key Criteria name.
115
     * @param array $criteria Filter criterias.
116
     * @return \DateTime|null Extracted date criteria.
117
     */
118 8
    private static function extractDateCriteria($key, array $criteria)
119
    {
120 8
        return (!empty($criteria[$key])) ? $criteria[$key] : null;
121
    }
122
}
123