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

MapTransform::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
}