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
|
|
|
|
16
|
11 |
|
public static function compare($key, $condition, $value): bool |
17
|
|
|
{ |
18
|
|
|
switch ($condition) { |
19
|
|
|
// -------------------- Numbers -------------------- |
20
|
11 |
|
case 'lt': |
21
|
1 |
|
return $key < $value; |
22
|
11 |
|
case 'gt': |
23
|
1 |
|
return $key > $value; |
24
|
11 |
|
case 'lte': |
25
|
1 |
|
return $key <= $value; |
26
|
11 |
|
case 'gte': |
27
|
1 |
|
return $key >= $value; |
28
|
11 |
|
case 'eq': |
29
|
6 |
|
return $key == $value; |
30
|
6 |
|
case 'ne': |
31
|
1 |
|
return $key != $value; |
32
|
6 |
|
case 'btn': |
33
|
1 |
|
list($min, $max) = explode(',', $value); |
34
|
1 |
|
return ($min <= $key) && ($key <= $max); |
35
|
|
|
// -------------------- Strings -------------------- |
36
|
6 |
|
case 'str.equals': |
37
|
1 |
|
return $key == $value; |
38
|
6 |
|
case 'str.not_equals': |
39
|
1 |
|
return $key != $value; |
40
|
6 |
|
case 'str.starts': |
41
|
1 |
|
return Str::startsWith($key, $value); |
42
|
6 |
|
case 'str.ends': |
43
|
1 |
|
return Str::endsWith($key, $value); |
44
|
6 |
|
case 'str.contains': |
45
|
1 |
|
$values = explode(',', $value); |
46
|
1 |
|
return Str::contains($key, $values); |
47
|
|
|
// -------------------- Regex -------------------- |
48
|
5 |
|
case 'regex.match': |
49
|
1 |
|
return preg_match(static::regex($value), $key); |
|
|
|
|
50
|
|
|
// -------------------- Arrays -------------------- |
51
|
4 |
|
case 'arr.in': |
52
|
1 |
|
$values = explode(',', $value); |
53
|
1 |
|
return in_array($key, $values); |
54
|
4 |
|
case 'arr.not_in': |
55
|
1 |
|
$values = explode(',', $value); |
56
|
1 |
|
return !in_array($key, $values); |
57
|
|
|
// -------------------- Dates -------------------- |
58
|
3 |
|
case 'date.equals': |
59
|
|
|
// Format: Y-m-d |
60
|
1 |
|
$key = new Carbon($key); |
61
|
1 |
|
$value = new Carbon($value); |
62
|
1 |
|
return $key->toDateString() == $value->toDateString(); |
63
|
3 |
|
case 'date.before': |
64
|
1 |
|
$key = new Carbon($key); |
65
|
1 |
|
$value = new Carbon($value); |
66
|
1 |
|
return $key->isBefore($value); |
67
|
3 |
|
case 'date.after': |
68
|
1 |
|
$key = new Carbon($key); |
69
|
1 |
|
$value = new Carbon($value); |
70
|
1 |
|
return $key->isAfter($value); |
71
|
3 |
|
case 'date.between': |
72
|
1 |
|
$key = new Carbon($key); |
73
|
|
|
|
74
|
1 |
|
list($start, $end) = explode(',', $value); |
75
|
1 |
|
$start = new Carbon($start); |
76
|
1 |
|
$end = new Carbon($end); |
77
|
1 |
|
return $key->between($start, $end); |
78
|
|
|
// -------------------- Time -------------------- |
79
|
2 |
|
case 'time.equals': |
80
|
|
|
// Format: H:i:s |
81
|
1 |
|
$key = new Carbon($key); |
82
|
1 |
|
return $key->toTimeString('second') == $value; |
83
|
2 |
|
case 'time.before': |
84
|
1 |
|
$key = (new Carbon($key))->format('His'); |
85
|
1 |
|
$value = now()->setTimeFromTimeString($value)->format('His'); |
86
|
1 |
|
return $key < $value; |
87
|
2 |
|
case 'time.after': |
88
|
1 |
|
$key = (new Carbon($key))->format('His'); |
89
|
1 |
|
$value = now()->setTimeFromTimeString($value)->format('His'); |
90
|
1 |
|
return $key > $value; |
91
|
2 |
|
case 'time.between': |
92
|
1 |
|
$key = (new Carbon($key))->format('His'); |
93
|
|
|
|
94
|
1 |
|
list($start, $end) = explode(',', $value); |
95
|
1 |
|
$min = now()->setTimeFromTimeString($start)->format('His'); |
96
|
1 |
|
$max = now()->setTimeFromTimeString($end)->format('His'); |
97
|
1 |
|
return ($min <= $key) && ($key <= $max); |
98
|
|
|
default: |
99
|
1 |
|
return $key == $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Silence auto-loading warnings. |
105
|
|
|
*/ |
106
|
13 |
|
public static function classExists(string $class, bool $autoload = true): bool |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
13 |
|
return class_exists($class, $autoload); |
110
|
|
|
} catch (\Throwable $th) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
13 |
|
public static function toPath(?string $name, string $suffix = 'Tag'): string |
116
|
|
|
{ |
117
|
13 |
|
$parts = explode('/', str_replace('\\', '/', $name)); |
118
|
|
|
|
119
|
13 |
|
$parts = array_map(function ($part) { |
120
|
13 |
|
return Str::studly($part); |
121
|
13 |
|
}, $parts); |
122
|
|
|
|
123
|
13 |
|
$path = implode('\\', $parts); |
124
|
|
|
|
125
|
13 |
|
return "{$path}{$suffix}"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @see https://stackoverflow.com/q/413071/2732184 |
130
|
|
|
* @see https://www.regextester.com/97707 |
131
|
|
|
*/ |
132
|
16 |
|
public static function hydrate(Store $store, string $text, string $pattern = '/[^{{\}\}]+(?=}})/'): string |
133
|
|
|
{ |
134
|
16 |
|
preg_match_all($pattern, $text, $matches); |
135
|
|
|
|
136
|
16 |
|
if (0 === \count($matches[0])) { |
137
|
15 |
|
return $text; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
$replace_vars = []; |
141
|
|
|
|
142
|
2 |
|
foreach ($matches[0] as $match) { |
143
|
2 |
|
$var = Str::slug($match, '_'); |
144
|
2 |
|
$replace_vars["{{{$match}}}"] = $store->get($var, "{{$var}}"); |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
return strtr($text, $replace_vars); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|