1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Billing Boss |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/ranskills/billing-boss-php |
6
|
|
|
* @copyright Copyright (c) 2018 Ransford Ako Okpoti |
7
|
|
|
* @license Refer to the LICENSE distributed with this library |
8
|
|
|
* @since 1.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace BillingBoss; |
12
|
|
|
|
13
|
|
|
use BillingBoss\Exception\RangeException; |
14
|
|
|
use BillingBoss\Exception\RangeOverlapException; |
15
|
|
|
use BillingBoss\Exception\RangeConflictException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A utility class for range notation related functions |
19
|
|
|
* |
20
|
|
|
* @package BillingBoss |
21
|
|
|
* @link https://github.com/ranskills/billing-boss-php |
22
|
|
|
* @copyright Copyright (c) 2018 Ransford Ako Okpoti |
23
|
|
|
* @license Refer to the LICENSE distributed with this library |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
*/ |
26
|
|
|
final class RangeHelper |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $str |
31
|
|
|
* @return array |
32
|
|
|
* @throws RangeException |
33
|
|
|
*/ |
34
|
12 |
|
public static function validate($str): array |
35
|
|
|
{ |
36
|
12 |
|
$matches = []; |
37
|
12 |
|
$numMatches = preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches); |
38
|
12 |
|
if ($numMatches === 0) { |
39
|
1 |
|
throw new RangeConflictException('No range values provided', RangeException::NO_RANGE_FOUND); |
40
|
|
|
} |
41
|
|
|
|
42
|
11 |
|
$numAstericks = substr_count($str, '*'); |
43
|
11 |
|
if ($numAstericks > 1) { |
44
|
1 |
|
throw new RangeConflictException( |
45
|
1 |
|
'More than one * provided in the ranges. There can be only one within a set of ranges to be examined.', |
46
|
1 |
|
RangeException::MULTIPLE_OPEN_ENDED_UPPER_LIMIT |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
10 |
|
$ranges = self::getRangeLimits($str); |
51
|
9 |
|
$overlappingRanges = self::findOverlappingRanges($ranges); |
52
|
|
|
|
53
|
9 |
|
if (count($overlappingRanges) !== 0) { |
54
|
3 |
|
$message = sprintf( |
55
|
3 |
|
'The ranges %s - %s and %s - %s are overlapping and will lead to unexpected results.', |
56
|
3 |
|
$overlappingRanges[0][0], |
57
|
3 |
|
$overlappingRanges[0][1], |
58
|
3 |
|
$overlappingRanges[1][0], |
59
|
3 |
|
$overlappingRanges[1][1] |
60
|
|
|
); |
61
|
|
|
|
62
|
3 |
|
throw new RangeOverlapException( |
63
|
3 |
|
$message, |
64
|
3 |
|
RangeException::NO_RANGE_FOUND, |
65
|
3 |
|
$overlappingRanges |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
return $ranges; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a list of overlapping ranges |
74
|
|
|
* |
75
|
|
|
* @param array $ranges |
76
|
|
|
* @return array A non-empty array if there are overlapping ranges, an empty array otherwise |
77
|
|
|
*/ |
78
|
9 |
|
private static function findOverlappingRanges(array $ranges): array |
79
|
|
|
{ |
80
|
9 |
|
$numRanges = count($ranges); |
81
|
|
|
|
82
|
9 |
|
for ($i = 0; $i < $numRanges; $i++) { |
83
|
9 |
|
for ($j = $i + 1; $j < $numRanges; $j++) { |
84
|
6 |
|
$inRange = self::isInRange($ranges[$i], $ranges[$j][0]) || |
85
|
3 |
|
self::isInRange($ranges[$i], $ranges[$j][1]) || |
86
|
3 |
|
self::isInRange($ranges[$j], $ranges[$i][0]) || |
87
|
6 |
|
self::isInRange($ranges[$j], $ranges[$i][1]); |
88
|
|
|
|
89
|
6 |
|
if ($inRange) { |
90
|
|
|
return [ |
91
|
3 |
|
$ranges[$i], |
92
|
3 |
|
$ranges[$j] |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the ranges expressed in a string. |
103
|
|
|
* |
104
|
|
|
* @param string $str |
105
|
|
|
* @return array |
106
|
|
|
* @throws RangeConflictException |
107
|
|
|
*/ |
108
|
10 |
|
private static function getRangeLimits($str): array |
109
|
|
|
{ |
110
|
10 |
|
$ranges = []; |
111
|
|
|
|
112
|
10 |
|
preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches); |
113
|
|
|
|
114
|
10 |
|
$lowerLimits = $matches[1]; |
115
|
10 |
|
$upperLimits = $matches[2]; |
116
|
10 |
|
$len = count($lowerLimits); |
117
|
|
|
|
118
|
10 |
|
for ($i = 0; $i < $len; $i++) { |
119
|
10 |
|
if (is_numeric($upperLimits[$i]) && floatval($lowerLimits[$i]) > floatval($upperLimits[$i])) { |
120
|
1 |
|
$message = sprintf( |
121
|
1 |
|
'Invalid limits provided. The lower limit (%s) is greater than the upper limit (%s)', |
122
|
1 |
|
$lowerLimits[$i], |
123
|
1 |
|
$upperLimits[$i] |
124
|
|
|
); |
125
|
1 |
|
throw new RangeConflictException( |
126
|
1 |
|
$message, |
127
|
1 |
|
RangeException::LOWER_LIMIT_GREATER_THAN_UPPER_LIMIT |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
10 |
|
$ranges[] = [$lowerLimits[$i], $upperLimits[$i]]; |
132
|
|
|
} |
133
|
|
|
|
134
|
9 |
|
return $ranges; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Checks if a given value is within a specified range |
139
|
|
|
* |
140
|
|
|
* @param array $range An array of two elements with the first and last elements being the lower and upper limits |
141
|
|
|
* for the boundary |
142
|
|
|
* @param string|integer $value |
143
|
|
|
* |
144
|
|
|
* @return bool true if value is within the range, false otherwise |
145
|
|
|
*/ |
146
|
9 |
|
public static function isInRange(array $range, $value): bool |
147
|
|
|
{ |
148
|
9 |
|
if ($value === '*') { |
149
|
3 |
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
9 |
|
if (is_numeric($range[1])) { |
153
|
7 |
|
return $value >= $range[0] && $value <= $range[1]; |
154
|
|
|
} |
155
|
|
|
|
156
|
6 |
|
return $value >= $range[0]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|