Passed
Push — master ( 151bf3...8309cc )
by Sebastian
02:26
created

getReplacementLength()   A

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
 * File containing the {@see Mailcode_Parser_Safeguard} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Command safeguarder: used to replace the mailcode commands
16
 * in a string with placeholders, to allow safe text transformation
17
 * and filtering operations on strings, without risking to break
18
 * any of the contained commands (if any).
19
 *
20
 * @package Mailcode
21
 * @subpackage Parser
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Parser_Safeguard_Placeholder
25
{
26
    const ERROR_PLACEHOLDER_TOO_LONG = 47901;
27
    
28
   /**
29
    * @var int
30
    */
31
    protected $id;
32
    
33
   /**
34
    * @var Mailcode_Parser_Safeguard
35
    */
36
    protected $safeguard;
37
38
   /**
39
    * @var Mailcode_Commands_Command
40
    */
41
    protected $command;
42
    
43
   /**
44
    * @var string
45
    */
46
    protected $replacement = '';
47
    
48
    public function __construct(int $id, Mailcode_Commands_Command $command, Mailcode_Parser_Safeguard $safeguard)
49
    {
50
        $this->id = $id;
51
        $this->command = $command;
52
        $this->safeguard = $safeguard;
53
    }
54
    
55
    public function getID() : int
56
    {
57
        return $this->id;
58
    }
59
    
60
    public function getLength() : int
61
    {
62
        return mb_strlen($this->getOriginalText());
63
    }
64
    
65
    public function getReplacementLength() : int
66
    {
67
        return strlen($this->getReplacementText());
68
    }
69
    
70
    public function getReplacementText() : string
71
    {
72
        if(!empty($this->replacement))
73
        {
74
            return $this->replacement;
75
        }
76
        
77
        // prepend and append the delimiter characters
78
        $format = sprintf(
79
            '%1$s%2$s%1$s',
80
            $this->safeguard->getDelimiter(),
81
            '%s'
82
        );
83
        
84
        // the length of the placeholder, without the ID
85
        $length = strlen($format) - 2; // - 2 for the %s
86
        
87
        // to total amount of zeroes to pad with to obtain the total length
88
        $padLength = $this->getLength() - $length;
89
        
90
        if($padLength < 0) 
91
        {
92
            $padLength = 0;
93
        }
94
        
95
        // create the zero-padded ID to fill the format string with 
96
        $paddedID  = str_pad((string)$this->id, $padLength, '0');
97
        
98
        $this->replacement = sprintf($format, $paddedID);
99
        
100
        return $this->replacement;
101
    }
102
    
103
    public function getOriginalText() : string
104
    {
105
        return $this->command->getMatchedText();
106
    }
107
    
108
    public function getNormalizedText() : string
109
    {
110
        return $this->command->getNormalized();
111
    }
112
    
113
    public function getHighlightedText() : string
114
    {
115
        return $this->command->getHighlighted();
116
    }
117
    
118
    public function getCommand() : Mailcode_Commands_Command
119
    {
120
        return $this->command;
121
    }
122
    
123
   /**
124
    * Serializes the placeholder's information into 
125
    * an array with the following keys:
126
    * 
127
    *   - originalText
128
    *   - replacementText
129
    *   - normalizedText
130
    *   - length
131
    *   - id
132
    * 
133
    * @return array<string,string|integer>
134
    */
135
    public function serialize() : array
136
    {
137
        return array(
138
            'originalText' => $this->getOriginalText(),
139
            'replacementText' => $this->getReplacementText(),
140
            'normalizedText' => $this->getNormalizedText(),
141
            'length' => $this->getLength(),
142
            'id' => $this->getID()
143
        );
144
    }
145
}
146