SpecialChars   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A encodeEscaped() 0 6 1
A encodeAll() 0 8 1
A escape() 0 9 1
A decode() 0 16 2
A getChars() 0 3 1
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Parser\Statement\Tokenizer\SpecialChars}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see \Mailcode\Parser\Statement\Tokenizer\SpecialChars
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Parser\Statement\Tokenizer;
13
14
/**
15
 * Handles the encoding, decoding and escaping of
16
 * Mailcode special characters in a string.
17
 *
18
 * @package Mailcode
19
 * @subpackage Parser
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class SpecialChars
23
{
24
    public const ESCAPE_CHAR = '\\';
25
    public const PLACEHOLDER_QUOTE = '__QUOTE__';
26
    public const PLACEHOLDER_BRACKET_OPEN = '__BRACKET_OPEN__';
27
    public const PLACEHOLDER_BRACKET_CLOSE = '__BRACKET_CLOSE__';
28
29
    /**
30
     * @var array<string,string>
31
     */
32
    private static array $charsEncoded = array(
33
        '"' => self::PLACEHOLDER_QUOTE,
34
        '{' => self::PLACEHOLDER_BRACKET_OPEN,
35
        '}' => self::PLACEHOLDER_BRACKET_CLOSE
36
    );
37
38
    /**
39
     * @var array<string,string>
40
     */
41
    private static array $charsEscaped = array(
42
        '"' => self::ESCAPE_CHAR.'"',
43
        '{' => self::ESCAPE_CHAR.'{',
44
        '}' => self::ESCAPE_CHAR.'}'
45
    );
46
47
    /**
48
     * @return array<string,string>
49
     */
50
    public static function getChars() : array
51
    {
52
        return self::$charsEncoded;
53
    }
54
55
    /**
56
     * Encodes a string for the internal string format,
57
     * which uses placeholders for special characters
58
     * like quotes and brackets.
59
     *
60
     * @param string $subject
61
     * @return string
62
     */
63
    public static function encodeAll(string $subject) : string
64
    {
65
        $subject = self::decode($subject);
66
67
        return str_replace(
68
            array_keys(self::$charsEncoded),
69
            array_values(self::$charsEncoded),
70
            $subject
71
        );
72
    }
73
74
    public static function encodeEscaped(string $subject) : string
75
    {
76
        return str_replace(
77
            array_values(self::$charsEscaped),
78
            array_values(self::$charsEncoded),
79
            $subject
80
        );
81
    }
82
83
    /**
84
     * Escapes all Mailcode special characters in the
85
     * specified string.
86
     *
87
     * @param string $subject
88
     * @return string
89
     */
90
    public static function escape(string $subject) : string
91
    {
92
        // Avoid double-encoding special characters
93
        $subject = self::decode($subject);
94
95
        return str_replace(
96
            array_keys(self::$charsEscaped),
97
            array_values(self::$charsEscaped),
98
            $subject
99
        );
100
    }
101
102
    /**
103
     * Decodes a string (including escaped characters) into
104
     * its raw form without escaped or encoded characters.
105
     *
106
     * @param string $subject
107
     * @return string
108
     */
109
    public static function decode(string $subject) : string
110
    {
111
        $replaces = array();
112
113
        foreach(self::$charsEncoded as $char => $placeholder)
114
        {
115
            $escaped = self::$charsEscaped[$char];
116
117
            $replaces[$escaped] = $char;
118
            $replaces[$placeholder] = $char;
119
        }
120
121
        return str_replace(
122
            array_keys($replaces),
123
            array_values($replaces),
124
            $subject
125
        );
126
    }
127
}
128