Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

MapTransform::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Utility;
4
5
/**
6
 * Class MapTransform
7
 * @package Nip\Router\Utility
8
 */
9
class MapTransform
10
{
11
    /**
12
     * @param $map
13
     * @return string|string[]|null
14
     */
15 9
    public static function run($map)
16
    {
17 9
        $map = str_replace(':controller/:action', '{controller}/{action?index}', $map);
18 9
        if (self::needToRun($map)) {
19 1
            $map = self::transform($map);
20
        }
21
22 9
        return $map;
23
    }
24
25
    /**
26
     * @param $string
27
     * @return bool
28
     */
29 9
    protected static function needToRun($string)
30
    {
31 9
        return strpos($string, ':') !== false;
32
    }
33
34
    /**
35
     * @param $string
36
     * @return string|string[]|null
37
     */
38 1
    protected static function transform($string)
39
    {
40 1
        $return = preg_replace('/:([a-z]+)/', '{${0}}', $string);
41 1
        return str_replace('{:', '{', $return);
42
    }
43
}