Passed
Push — master ( 95d9aa...1515ff )
by Sebastian
05:04 queued 13s
created

Mailcode_Parser_Safeguard_Placeholder::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
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 getReplacementText() : string
66
    {
67
        if(!empty($this->replacement))
68
        {
69
            return $this->replacement;
70
        }
71
        
72
        // prepend and append the delimiter characters
73
        $format = sprintf(
74
            '%1$s%2$s%1$s',
75
            $this->safeguard->getDelimiter(),
76
            '%s'
77
        );
78
        
79
        // the length of the placeholder, without the ID
80
        $length = strlen($format) - 2; // - 2 for the %s
81
        
82
        // to total amount of zeroes to pad with to obtain the total length
83
        $padLength = $this->getLength() - $length;
84
        
85
        if($padLength < 0) 
86
        {
87
            $padLength = 0;
88
        }
89
        
90
        // create the zero-padded ID to fill the format string with 
91
        $paddedID  = str_pad((string)$this->id, $padLength, '0');
92
        
93
        $this->replacement = sprintf($format, $paddedID);
94
        
95
        return $this->replacement;
96
    }
97
    
98
    public function getOriginalText() : string
99
    {
100
        return $this->command->getMatchedText();
101
    }
102
    
103
    public function getNormalizedText() : string
104
    {
105
        return $this->command->getNormalized();
106
    }
107
    
108
    public function getHighlightedText() : string
109
    {
110
        return $this->command->getHighlighted();
111
    }
112
    
113
    public function getCommand() : Mailcode_Commands_Command
114
    {
115
        return $this->command;
116
    }
117
    
118
    public function serialize() : array
119
    {
120
        return array(
121
            'originalText' => $this->getOriginalText(),
122
            'replacementText' => $this->getReplacementText(),
123
            'normalizedText' => $this->getNormalizedText(),
124
            'length' => $this->getLength(),
125
            'id' => $this->getID()
126
        );
127
    }
128
}
129