|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Time string normalizer |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7+ |
|
6
|
|
|
* |
|
7
|
|
|
* @category IntervalParser |
|
8
|
|
|
* @author Ekin H. Bayar <[email protected]> |
|
9
|
|
|
* @version 0.2.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace IntervalParser; |
|
12
|
|
|
|
|
13
|
|
|
class Normalizer |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Used to turn a given non-strtotime-compatible time string into a compatible one |
|
17
|
|
|
* Only modifies the non-strtotime-compatible time strings provided leaving the rest intact |
|
18
|
|
|
* |
|
19
|
|
|
* @var string $pattern |
|
20
|
|
|
*/ |
|
21
|
|
|
public static $pattern = <<<'REGEX' |
|
22
|
|
|
~ |
|
23
|
|
|
# grab the integer part of time string |
|
24
|
|
|
\s? (?<int> \d{1,5}) \s? |
|
25
|
|
|
# match only the shortest abbreviations that aren't supported by strtotime |
|
26
|
|
|
(?<time> |
|
27
|
|
|
(?: s (?=(?:ec(?:ond)?s?)?(?:\b|\d)) |
|
28
|
|
|
| m (?=(?:in(?:ute)?s?)?(?:\b|\d)) |
|
29
|
|
|
| h (?=(?:(?:ou)?rs?)?(?:\b|\d)) |
|
30
|
|
|
| d (?=(?:ays?)?(?:\b|\d)) |
|
31
|
|
|
| w (?=(?:eeks?)?(?:\b|\d)) |
|
32
|
|
|
| mon (?=(?:(?:th)?s?)?(?:\b|\d)) |
|
33
|
|
|
) |
|
34
|
|
|
) |
|
35
|
|
|
[^\d]*?(?=\b|\d) |
|
36
|
|
|
# do only extract start of string |
|
37
|
|
|
| (?<text> .+) |
|
38
|
|
|
~uix |
|
39
|
|
|
REGEX; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Turns any non-strtotime-compatible time string into a compatible one. |
|
43
|
|
|
* If the passed input has trailing data, it won't be lost since within the callback the input is reassembled. |
|
44
|
|
|
* However no leading data is accepted. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $input |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function normalize(string $input): string |
|
50
|
|
|
{ |
|
51
|
4 |
|
$output = preg_replace_callback(self::$pattern, |
|
52
|
4 |
|
function($matches) { |
|
53
|
4 |
|
$int = $matches['int']; |
|
54
|
4 |
|
switch ($matches['time']) { |
|
55
|
4 |
|
case 's': |
|
56
|
3 |
|
$t = ($int == 1) ? ' second ' : ' seconds '; |
|
57
|
3 |
|
break; |
|
58
|
4 |
|
case 'm': |
|
59
|
4 |
|
$t = ($int == 1) ? ' minute ' : ' minutes '; |
|
60
|
4 |
|
break; |
|
61
|
4 |
|
case 'h': |
|
62
|
4 |
|
$t = ($int == 1) ? ' hour ' : ' hours '; |
|
63
|
4 |
|
break; |
|
64
|
4 |
|
case 'd': |
|
65
|
4 |
|
$t = ($int == 1) ? ' day ' : ' days '; |
|
66
|
4 |
|
break; |
|
67
|
4 |
|
case 'w': |
|
68
|
2 |
|
$t = ($int == 1) ? ' week ' : ' weeks '; |
|
69
|
2 |
|
break; |
|
70
|
3 |
|
case 'mon': |
|
71
|
1 |
|
$t = ($int == 1) ? ' month ' : ' months '; |
|
72
|
1 |
|
break; |
|
73
|
3 |
|
case 'y': |
|
74
|
|
|
$t = ($int == 1) ? ' year ' : ' years '; |
|
75
|
4 |
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
$t = $t ?? ''; |
|
79
|
|
|
# rebuild the interval string |
|
|
|
|
|
|
80
|
4 |
|
$time = $int . $t; |
|
81
|
|
|
|
|
82
|
4 |
|
if (isset($matches['text'])) { |
|
83
|
3 |
|
$time .= trim($matches['text']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
return $time; |
|
87
|
|
|
|
|
88
|
4 |
|
}, $input); |
|
89
|
|
|
|
|
90
|
4 |
|
return trim($output); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|