Sanitiser::get_registry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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