Passed
Push — master ( 2788fa...0ad25a )
by Brian
02:37
created

Expressions::decExp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Bmatovu\Ussd\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait Expressions
8
{
9 1
    protected function incExp(?string $exp, int $step = 1): ?string
10
    {
11 1
        if(! $exp) {
12 1
            return $exp;
13
        }
14
15
        return preg_replace_callback('|(\\d+)(?!.*\\d)|', function ($matches) use ($step) {
16
            return $matches[1] + $step;
17
        }, $exp);
18
    }
19
20
    /**
21
     * @see https://stackoverflow.com/q/413071/2732184
22
     * @see https://www.regextester.com/97707
23
     */
24
    protected function translate(string $text, string $pattern = '/[^{\}]+(?=})/'): string
25
    {
26
        preg_match_all($pattern, $text, $matches);
27
28
        if (0 === \count($matches[0])) {
29
            return $text;
30
        }
31
32
        $replace_vars = [];
33
34
        foreach ($matches[0] as $match) {
35
            $var = Str::slug($match, '_');
36
            $replace_vars["{{$match}}"] = $this->cache->get("{$this->prefix}_{$var}", "{{$var}}");
37
        }
38
39
        return strtr($text, $replace_vars);
40
    }
41
}
42