Passed
Branch master (43d553)
by Sebastian
02:54
created

ConvertHelper_HiddenConverter::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * File containing the {@see AppUtils\ConvertHelper_HiddenConverter} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage ConvertHelper
7
 * @see AppUtils\ConvertHelper_HiddenConverter
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Can replace any hidden characters (like whitespace or control characters)
16
 * with visible, easily identifiable strings for easy debugging.
17
 *
18
 * @package Application Utils
19
 * @subpackage ConvertHelper
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class ConvertHelper_HiddenConverter
23
{
24
    const CHARS_WHITESPACE = 'whitespace';
25
    
26
    const CHARS_CONTROL = 'control';
27
    
28
    protected $characters = array(
29
        'whitespace' => array(
30
            "\t" => '[TAB]',
31
            "\n" => '[LF]',
32
            "\r" => '[CR]',
33
            " " => '[SPACE]'
34
        ),
35
        'control' => array(
36
            "\x00" => '[NUL]', // Null
37
            "\x01" => '[SOH]', // 1 Start of heading
38
            "\x02" => '[STX]', // 2 Start of text
39
            "\x03" => '[ETX]', // 3 End of text
40
            "\x04" => '[EOT]', // 4 End of transmission
41
            "\x05" => '[ENQ]', // 5 Enquiry
42
            "\x06" => '[ACK]', // 6 Acknowledge
43
            "\x07" => '[BEL]', // 7 Bell
44
            "\x08" => '[BS]', // 8 Backspace
45
            //"\x09" => '[HT]', // 9 Horizontal tabulation (Already in whitespace)
46
            //"\x0A" => '[LF]', // 10 Line feed (Already in whitespace)
47
            "\x0B" => '[VT]', // 11 Vertical tabulation
48
            "\x0C" => '[FF]', // 12 Form feed
49
            //"\x0D" => '[CR]', // 13 Carriage return (Already in whitespace)
50
            "\x0E" => '[SO]', // 14 Shift out
51
            "\x0F" => '[SI]', // 15 Shift in
52
            "\x10" => '[DLE]', // 16 Data link escape
53
            "\x11" => '[DC1]', // 17 Device control 1
54
            "\x12" => '[DC2]', // 18 Device control 2
55
            "\x13" => '[DC3]', // 19 Device control 3
56
            "\x14" => '[DC4]', // 20 Device control 4
57
            "\x15" => '[NAK]', // 21 Negative acknowledge
58
            "\x16" => '[SYN]', // 22 Synchronous idle
59
            "\x17" => '[ETB]', // 23 End of transmission block
60
            "\x18" => '[CAN]', // 24 Cancel
61
            "\x19" => '[EM]', // 25 End of medium
62
            "\x1A" => '[SUB]', // 26 Substitute
63
            "\x1B" => '[ESC]', // 27 Escape
64
            "\x1C" => '[FS]', // 28 File separator
65
            "\x1D" => '[GS]', // 29 Group Separator
66
            "\x1E" => '[RS]', // 30 Record Separator
67
            "\x1F" => '[US]', // 31 Unit Separator
68
            "\x7F" => '[DEL]' // 127 Delete
69
        )
70
    );
71
    
72
   /**
73
    * @var array
74
    */
75
    protected $selected = array();
76
    
77
    public function convert(string $string) : string
78
    {
79
        $chars = $this->resolveSelection();
80
        
81
        return str_replace(array_keys($chars), array_values($chars), $string);
82
    }
83
    
84
   /**
85
    * Selects a character set to replace. Can be called
86
    * several times to add additional sets to the collection.
87
    * 
88
    * @param string $type See the <code>CHAR_XXX</code> constants.
89
    * @return ConvertHelper_HiddenConverter
90
    * 
91
    * @see ConvertHelper_HiddenConverter::CHARS_CONTROL
92
    * @see ConvertHelper_HiddenConverter::CHARS_WHITESPACE
93
    */
94
    public function selectCharacters(string $type) : ConvertHelper_HiddenConverter
95
    {
96
        if(!in_array($type, $this->selected)) {
97
            $this->selected[] = $type;
98
        }
99
        
100
        return $this;
101
    }
102
    
103
   /**
104
    * Resolves the list of characters to make visible.
105
    * 
106
    * @return array
107
    */
108
    protected function resolveSelection() : array
109
    {
110
        $selected = $this->selected;
111
        
112
        if(empty($this->selected)) 
113
        {
114
            $selected = array(
115
                self::CHARS_WHITESPACE,
116
                self::CHARS_CONTROL
117
            );
118
        }
119
        
120
        $result = array();
121
        
122
        foreach($selected as $type) 
123
        {
124
            if(isset($this->characters[$type])) 
125
            {
126
                $result = array_merge($result, $this->characters[$type]);
127
            }
128
        }
129
        
130
        return $result;
131
    }
132
}
133