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

MapTransform   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 9 2
A needToRun() 0 4 1
A transform() 0 5 1
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
}