1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\Ussd\Support; |
4
|
|
|
|
5
|
|
|
use Bmatovu\Ussd\Store; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class Util |
10
|
|
|
{ |
11
|
2 |
|
public static function regex($pattern): string |
12
|
|
|
{ |
13
|
2 |
|
return Str::startsWith($pattern, '/') ? $pattern : "/{$pattern}/"; |
14
|
|
|
} |
15
|
11 |
|
public static function compare($key, $condition, $value): bool |
16
|
|
|
{ |
17
|
|
|
switch ($condition) { |
18
|
|
|
// -------------------- Numbers -------------------- |
19
|
11 |
|
case 'lt': |
20
|
1 |
|
return $key < $value; |
21
|
11 |
|
case 'gt': |
22
|
1 |
|
return $key > $value; |
23
|
11 |
|
case 'lte': |
24
|
1 |
|
return $key <= $value; |
25
|
11 |
|
case 'gte': |
26
|
1 |
|
return $key >= $value; |
27
|
11 |
|
case 'eq': |
28
|
6 |
|
return $key == $value; |
29
|
6 |
|
case 'ne': |
30
|
1 |
|
return $key != $value; |
31
|
6 |
|
case 'btn': |
32
|
1 |
|
list($min, $max) = explode(',', $value); |
33
|
1 |
|
return ($min <= $key) && ($key <= $max); |
34
|
|
|
// -------------------- Strings -------------------- |
35
|
6 |
|
case 'str.equals': |
36
|
1 |
|
return $key == $value; |
37
|
6 |
|
case 'str.not_equals': |
38
|
1 |
|
return $key != $value; |
39
|
6 |
|
case 'str.starts': |
40
|
1 |
|
return Str::startsWith($key, $value); |
41
|
6 |
|
case 'str.ends': |
42
|
1 |
|
return Str::endsWith($key, $value); |
43
|
6 |
|
case 'str.contains': |
44
|
1 |
|
$values = explode(',', $value); |
45
|
1 |
|
return Str::contains($key, $values); |
46
|
|
|
// -------------------- Regex -------------------- |
47
|
5 |
|
case 'regex.match': |
48
|
1 |
|
return preg_match(static::regex($value), $key); |
|
|
|
|
49
|
|
|
// -------------------- Arrays -------------------- |
50
|
4 |
|
case 'arr.in': |
51
|
1 |
|
$values = explode(',', $value); |
52
|
1 |
|
return in_array($key, $values); |
53
|
4 |
|
case 'arr.not_in': |
54
|
1 |
|
$values = explode(',', $value); |
55
|
1 |
|
return !in_array($key, $values); |
56
|
|
|
// -------------------- Dates -------------------- |
57
|
3 |
|
case 'date.equals': |
58
|
|
|
// Format: Y-m-d |
59
|
1 |
|
$key = new Carbon($key); |
60
|
1 |
|
$value = new Carbon($value); |
61
|
1 |
|
return $key->toDateString() == $value->toDateString(); |
62
|
3 |
|
case 'date.before': |
63
|
1 |
|
$key = new Carbon($key); |
64
|
1 |
|
$value = new Carbon($value); |
65
|
1 |
|
return $key->isBefore($value); |
66
|
3 |
|
case 'date.after': |
67
|
1 |
|
$key = new Carbon($key); |
68
|
1 |
|
$value = new Carbon($value); |
69
|
1 |
|
return $key->isAfter($value); |
70
|
3 |
|
case 'date.between': |
71
|
1 |
|
$key = new Carbon($key); |
72
|
|
|
|
73
|
1 |
|
list($start, $end) = explode(',', $value); |
74
|
1 |
|
$start = new Carbon($start); |
75
|
1 |
|
$end = new Carbon($end); |
76
|
1 |
|
return $key->between($start, $end); |
77
|
|
|
// -------------------- Time -------------------- |
78
|
2 |
|
case 'time.equals': |
79
|
|
|
// Format: H:i:s |
80
|
1 |
|
$key = new Carbon($key); |
81
|
1 |
|
return $key->toTimeString('second') == $value; |
82
|
2 |
|
case 'time.before': |
83
|
1 |
|
$key = (new Carbon($key))->format('His'); |
84
|
1 |
|
$value = now()->setTimeFromTimeString($value)->format('His'); |
85
|
1 |
|
return $key < $value; |
86
|
2 |
|
case 'time.after': |
87
|
1 |
|
$key = (new Carbon($key))->format('His'); |
88
|
1 |
|
$value = now()->setTimeFromTimeString($value)->format('His'); |
89
|
1 |
|
return $key > $value; |
90
|
2 |
|
case 'time.between': |
91
|
1 |
|
$key = (new Carbon($key))->format('His'); |
92
|
|
|
|
93
|
1 |
|
list($start, $end) = explode(',', $value); |
94
|
1 |
|
$min = now()->setTimeFromTimeString($start)->format('His'); |
95
|
1 |
|
$max = now()->setTimeFromTimeString($end)->format('His'); |
96
|
1 |
|
return ($min <= $key) && ($key <= $max); |
97
|
|
|
default: |
98
|
1 |
|
return $key == $value; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @see https://stackoverflow.com/q/413071/2732184 |
104
|
|
|
* @see https://www.regextester.com/97707 |
105
|
|
|
*/ |
106
|
15 |
|
public static function hydrate(Store $store, string $text, string $pattern = '/[^{{\}\}]+(?=}})/'): string |
107
|
|
|
{ |
108
|
15 |
|
preg_match_all($pattern, $text, $matches); |
109
|
|
|
|
110
|
15 |
|
if (0 === \count($matches[0])) { |
111
|
14 |
|
return $text; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$replace_vars = []; |
115
|
|
|
|
116
|
2 |
|
foreach ($matches[0] as $match) { |
117
|
2 |
|
$var = Str::slug($match, '_'); |
118
|
2 |
|
$replace_vars["{{{$match}}}"] = $store->get($var, "{{$var}}"); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return strtr($text, $replace_vars); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|