Passed
Push — master ( eafb24...c53bf4 )
by Sebastian
04:30
created

getEOLChar()   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_Formatter_SingleLines} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Formatter_SingleLines
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Single line formatter: ensures that all commands in the
16
 * subject string are placed on a separate line. This is 
17
 * typically used when using a custom parser for HTML documents,
18
 * to make it easier to identify commands.
19
 *
20
 * @package Mailcode
21
 * @subpackage Parser
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Parser_Safeguard_Formatter_SingleLines extends Mailcode_Parser_Safeguard_Formatter
25
{
26
   /**
27
    * @var string
28
    */
29
    protected $eol;
30
    
31
   /**
32
    * @var int
33
    */
34
    protected $eolLength;
35
    
36
    public function format(string $subject) : string
37
    {
38
        $placeholders = $this->resolvePlaceholderStrings();
39
        
40
        $this->eol = $this->resolveNewlineChar($subject);
41
        $this->eolLength = strlen($this->eol);
42
        
43
        $total = count($placeholders);
44
        
45
        for($i=0; $i < $total; $i++)
46
        {
47
            $subject = $this->process($placeholders[$i], $subject);
48
        }
49
        
50
        return $subject;
51
    }
52
    
53
    public function getEOLChar() : string
54
    {
55
        return $this->eol;
56
    }
57
    
58
    public function getEOLLength() : int
59
    {
60
        return $this->eolLength;
61
    }
62
63
    protected function process(string $placeholder, string $subject) : string
64
    {
65
        $positions = $this->resolvePositions($placeholder, $subject);
66
        $phLength = mb_strlen($placeholder);
67
        $offset = 0;
68
        
69
        foreach($positions as $position)
70
        {
71
            // adjust the position if previous changes made the subject longer
72
            $position += $offset;
73
            
74
            $info = new Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder(
75
                $this, 
76
                $subject, 
77
                $phLength, 
78
                $position
79
            );
80
            
81
            if(!$info->requiresAdjustment())
82
            {
83
                continue;
84
            }
85
            
86
            $adjusted = $this->resolveAdjustedPlaceholder($info, $placeholder);
87
            
88
            // cut the subject string so we can insert the adjusted placeholder
89
            $start = mb_substr($subject, 0, $position);
90
            $end = mb_substr($subject, $position + $phLength);
91
            
92
            // rebuild the subject string from the parts
93
            $subject = $start.$adjusted.$end;
94
            
95
            // the placeholder length has changed, which means subsequent
96
            // positions have to be increased by the added length, which
97
            // we do using the offset.
98
            $offset += mb_strlen($adjusted) - $phLength; 
99
        }
100
        
101
        return $subject;
102
    }
103
    
104
    protected function resolveAdjustedPlaceholder(Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder $info, string $placeholder) : string
105
    {
106
        $prepend = '';
107
        $append = '';
108
        
109
        if($info->requiresPrepend())
110
        {
111
            $prepend = $this->eol;
112
        }
113
        
114
        if($info->requiresAppend())
115
        {
116
            $append = $this->eol;
117
        }
118
        
119
        return $prepend.$placeholder.$append;
120
    }
121
    
122
   /**
123
    * We only use placeholders that contain commands that do
124
    * not generate contents, since these are the only ones
125
    * that may need adjusting.
126
    * 
127
    * @return \Mailcode\Mailcode_Parser_Safeguard_Placeholder[]
128
    */
129
    protected function filterPlaceholders()
130
    {
131
        $placeholders = $this->safeguard->getPlaceholders();
132
        
133
        $result = array();
134
        
135
        foreach($placeholders as $placeholder)
136
        {
137
            if(!$placeholder->getCommand()->generatesContent())
138
            {
139
                $result[] = $placeholder;
140
            }
141
        }
142
        
143
        return $result;
144
    }
145
}
146
147