RouteName::getPrefix()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Knp\RadBundle\Routing\Conventional\Generator;
4
5
use Knp\RadBundle\Routing\Conventional\Config;
6
use Knp\RadBundle\Routing\Conventional\Generator;
7
8
class RouteName implements Generator
9
{
10
    /**
11
     * recursively build a route name
12
     *
13
     * @return string
14
     **/
15
    public function generate(Config $config)
16
    {
17
        $parts = array();
18
        if ($config->parent) {
19
            $parent = $config->parent->isRepresentant() ? $config->parent->parent : $config->parent;
20
            $parts[] = $this->generate($parent);
21
        }
22
23
        $parts[] = $this->getPrefix($config);
24
25
        return implode('_', array_filter($parts));
26
    }
27
28
    /**
29
     * normalize name, removing root prefix (i.e: App) for nested routes
30
     *
31
     * @return string
32
     **/
33
    private function getPrefix(Config $config)
34
    {
35
        $parts = explode(':', $config->name);
36
        if ($config->parent && 2 === count($parts)) {
37
            // nested route that contains 'App:*
38
            array_shift($parts);
39
        }
40
41
        return strtolower(str_replace(array('/', '\\', ':'), '_', implode('_', $parts)));
42
    }
43
}
44