Util   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 97.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
eloc 92
dl 0
loc 151
ccs 91
cts 93
cp 0.9785
rs 9.68
c 1
b 0
f 0

5 Methods

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