SprintfLocker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 124
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A maskString() 0 2 1
A createNotAllowedMap() 0 17 5
A unlock() 0 4 1
A __construct() 0 4 1
A createReplacementMap() 0 23 5
A lock() 0 5 1
1
<?php
2
3
namespace Matecat\SubFiltering\Filters\Sprintf;
4
5
class SprintfLocker {
6
7
    /**
8
     * Protection tags
9
     */
10
    const PRE_LOCK_TAG  = '_____########';
11
    const POST_LOCK_TAG = '########_____';
12
13
    /**
14
     * @var string|null
15
     */
16
    private ?string $source;
17
18
    /**
19
     * @var string|null
20
     */
21
    private ?string $target;
22
23
    /**
24
     * @var array
25
     */
26
    private array $notAllowedMap;
27
28
    /**
29
     * @var array
30
     */
31
    private array $replacementMap = [];
32
33
    /**
34
     * SprintfLocker constructor.
35
     *
36
     * @param string|null $source
37
     * @param string|null $target
38
     */
39
    public function __construct( ?string $source = null, ?string $target = null ) {
40
        $this->source        = $source;
41
        $this->target        = $target;
42
        $this->notAllowedMap = $this->createNotAllowedMap();
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    private function createNotAllowedMap(): array {
49
        $map = [];
50
51
        $all = include __DIR__ . "/language/all/not_allowed.php";
52
        $map = array_merge( $map, $all );
53
54
        if ( $this->source and file_exists( __DIR__ . "/language/" . $this->source . "/not_allowed.php" ) ) {
55
            $source = include __DIR__ . "/language/" . $this->source . "/not_allowed.php";
56
            $map    = array_merge( $map, $source );
57
        }
58
59
        if ( $this->target and file_exists( __DIR__ . "/language/" . $this->target . "/not_allowed.php" ) ) {
60
            $target = include __DIR__ . "/language/" . $this->target . "/not_allowed.php";
61
            $map    = array_merge( $map, $target );
62
        }
63
64
        return $map;
65
    }
66
67
    /**
68
     * @param string $segment
69
     *
70
     * @return string
71
     */
72
    public function lock( string $segment ): string {
73
        $replacementMap       = $this->createReplacementMap( $segment );
74
        $this->replacementMap = $replacementMap;
75
76
        return str_replace( array_keys( $replacementMap ), array_values( $replacementMap ), $segment );
77
    }
78
79
    /**
80
     * @param string $segment
81
     *
82
     * @return string
83
     */
84
    public function unlock(string $segment ): string {
85
        $replacementMap = $this->replacementMap;
86
87
        return str_replace( array_values( $replacementMap ), array_keys( $replacementMap ), $segment );
88
    }
89
90
    /**
91
     * Create the replacement map
92
     *
93
     * @param string $segment
94
     *
95
     * @return array
96
     */
97
    private function createReplacementMap( string $segment ): array {
98
        $replacementMap = [];
99
100
        foreach ( $this->notAllowedMap as $item => $details ) {
101
102
            $type = $details[ 'type' ];
103
104
            switch ( $type ) {
105
                case 'exact':
106
                    $replacementMap[ $item ] = self::PRE_LOCK_TAG . $this->maskString( $item ) . self::POST_LOCK_TAG;
107
                    break;
108
109
                case 'regex':
110
                    preg_match_all( '/' . $item . '/', $segment, $matches );
111
112
                    foreach ( $matches[ 0 ] as $match ) {
113
                        $replacementMap[ $match ] = self::PRE_LOCK_TAG . $this->maskString( $match ) . self::POST_LOCK_TAG;
114
                    }
115
                    break;
116
            }
117
        }
118
119
        return $replacementMap;
120
    }
121
122
    /**
123
     * @param string $string
124
     *
125
     * @return string
126
     */
127
    private function maskString( string $string ): string {
128
        return str_replace( [ '%', '-', '_' ], '', $string );
129
    }
130
131
}