Sanitiser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 14
c 4
b 0
f 0
dl 0
loc 46
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A scramble() 0 8 2
A sanitise() 0 3 1
A unsanitise() 0 3 1
A unscramble() 0 9 2
A get_registry() 0 3 1
1
<?php
2
3
namespace Sunnysideup\SanitiseClassName;
4
5
use SilverStripe\Core\Config\Config;
6
7
class Sanitiser
8
{
9
    /**
10
     * you can fill this variable like this:
11
     * ```php
12
     *     Sunnysideup\Ecommerce\Product => 'Product'
13
     * ```.
14
     *
15
     * @var array
16
     */
17
    private static $scramble_registry = [];
18
19
    public static function sanitise(string $className): string
20
    {
21
        return str_replace('\\', '-', $className);
22
    }
23
24
    public static function unsanitise(string $className): string
25
    {
26
        return str_replace('-', '\\', $className);
27
    }
28
29
    public static function scramble(string $className): string
30
    {
31
        $registry = self::get_registry();
32
        if (isset($registry[$className])) {
33
            return $registry[$className];
34
        }
35
36
        return $className;
37
    }
38
39
    public static function unscramble(string $className): string
40
    {
41
        $registry = self::get_registry();
42
        $registry = array_flip($registry);
43
        if ($registry[$className]) {
44
            return $registry[$className];
45
        }
46
47
        return $className;
48
    }
49
50
    protected static function get_registry(): array
51
    {
52
        return Config::inst()->get(self::class, 'scramble_registry');
53
    }
54
}
55