|
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\Exception\InvalidArgumentException; |
|
13
|
|
|
use RunOpenCode\ExchangeRate\Exception\RuntimeException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class BaseFilterUtil |
|
17
|
|
|
* |
|
18
|
|
|
* Common shared functions for filter utilities. |
|
19
|
|
|
* |
|
20
|
|
|
* @package RunOpenCode\ExchangeRate\Utils |
|
21
|
|
|
*/ |
|
22
|
|
|
trait FilterUtilHelper |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Extract array criteria from criterias. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $key Criteria name. |
|
28
|
|
|
* @param array $criteria Filter criterias. |
|
29
|
|
|
* @return array Extracted array criterias. |
|
30
|
|
|
*/ |
|
31
|
20 |
|
private static function extractArrayCriteria($key, array $criteria) |
|
32
|
|
|
{ |
|
33
|
20 |
|
if (!empty($criteria[$key])) { |
|
34
|
13 |
|
return array($criteria[$key]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
19 |
|
if (!empty($criteria[$key.'s'])) { |
|
38
|
10 |
|
return $criteria[$key.'s']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
15 |
|
return array(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Extract date from filter criterias. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $key Criteria name. |
|
48
|
|
|
* @param array $criteria Filter criterias. |
|
49
|
|
|
* @return \DateTime|null Extracted date criteria. |
|
50
|
|
|
*/ |
|
51
|
11 |
|
private static function extractDateCriteria($key, array $criteria) |
|
52
|
|
|
{ |
|
53
|
11 |
|
$date = (!empty($criteria[$key])) ? $criteria[$key] : null; |
|
54
|
|
|
|
|
55
|
11 |
|
if (is_string($date)) { |
|
56
|
5 |
|
$date = \DateTime::createFromFormat('Y-m-d', $date); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
11 |
|
if (false === $date) { |
|
60
|
1 |
|
throw new InvalidArgumentException(sprintf('Invalid date/time format provided "%s", expected "%s", or instance of \DateTime class.', $criteria[$key], 'Y-m-d')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
10 |
|
return $date; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check if array|string criteria is matched. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $key Array|string criteria key. |
|
70
|
|
|
* @param object $object Object to check for match. |
|
71
|
|
|
* @param array $criteria Filter criterias. |
|
72
|
|
|
* @return bool TRUE if there is a match. |
|
73
|
|
|
*/ |
|
74
|
16 |
|
private static function matchesArrayCriteria($key, $object, array $criteria) |
|
75
|
|
|
{ |
|
76
|
16 |
|
$criteria = self::extractArrayCriteria($key, $criteria); |
|
77
|
|
|
|
|
78
|
16 |
|
if (count($criteria) === 0) { |
|
79
|
12 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
13 |
|
$getter = sprintf('get%s', ucfirst($key)); |
|
83
|
|
|
|
|
84
|
13 |
|
if (!method_exists($object, $getter)) { |
|
85
|
1 |
|
throw new RuntimeException(sprintf('Object instance of "%s" does not have required getter "%s" to be used for filtering.', get_class($object), $getter)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
12 |
|
return in_array($object->{$getter}(), $criteria, true); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|