Support::moduleNameFromRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
ccs 8
cts 8
cp 1
crap 1
1
<?php
2
3
namespace kalanis\kw_routed_paths;
4
5
6
/**
7
 * Class Support
8
 * @package kalanis\kw_routed_paths
9
 * Basic class with supporting methods
10
 */
11
class Support
12
{
13
    public const PREFIX_MOD_NORMAL = 'm'; # show in normal mode
14
    public const PREFIX_MOD_SINGLE = 'ms'; # show module as single window
15
    public const PREFIX_USER = 'u'; # this is about that user
16
    public const PREFIX_LANG = 'l'; # this is about that language
17
18
    /**
19
     * @param string $name
20
     * @return string[]
21
     */
22 12
    public static function moduleNameFromRequest(string $name): array
23
    {
24 12
        return array_map(
25 12
            'ucfirst',
26 12
            array_map(
27 12
                [self::class, 'normalizeModuleName'],
28 12
                array_map(
29 12
                    'strtolower',
30 12
                    explode('--', $name)
31
                )
32
            )
33
        );
34
    }
35
36 12
    public static function normalizeModuleName(string $moduleName): string
37
    {
38 12
        return implode('', array_map([self::class, 'moduleNamePart'], explode('-', $moduleName)));
39
    }
40
41 12
    public static function moduleNamePart(string $moduleName): string
42
    {
43 12
        return ucfirst(strtolower($moduleName));
44
    }
45
46
    /**
47
     * @param string[] $path
48
     * @return string
49
     */
50 18
    public static function requestFromModuleName(array $path): string
51
    {
52 18
        return implode('--', array_filter(array_map(
53 18
            [self::class, 'linkModuleName'],
54
            $path
55
        )));
56
    }
57
58 18
    public static function linkModuleName(string $moduleName): string
59
    {
60 18
        if (false != preg_match_all('#([A-Z][a-z0-9]*)#u', $moduleName, $matches)) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match_all('#([A-Z][... $moduleName, $matches) of type integer|null against false; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
61 17
            return implode('-', array_map('mb_strtolower', $matches[1]));
62
        } else {
63 1
            return mb_strtolower($moduleName);
64
        }
65
    }
66
67 20
    public static function prefixWithSeparator(string $prefix): string
68
    {
69 20
        return $prefix . ':';
70
    }
71
}
72