1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Parser |
7
|
|
|
* @see Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Detects whether the placeholder needs newlines characters |
16
|
|
|
* prepended or appended. |
17
|
|
|
* |
18
|
|
|
* @package Mailcode |
19
|
|
|
* @subpackage Parser |
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Mailcode_Parser_Safeguard_Formatter_SingleLines_Placeholder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $eolLength; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $position; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $subject; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Mailcode_Parser_Safeguard_Formatter_SingleLines |
41
|
|
|
*/ |
42
|
|
|
protected $formatter; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
protected $prepend = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var boolean |
51
|
|
|
*/ |
52
|
|
|
protected $append = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
protected $placeholderLength; |
58
|
|
|
|
59
|
|
|
public function __construct(Mailcode_Parser_Safeguard_Formatter_SingleLines $formatter, string $subject, int $placeholderLength, int $position) |
60
|
|
|
{ |
61
|
|
|
$this->formatter = $formatter; |
62
|
|
|
$this->eolLength = $formatter->getEOLLength(); |
63
|
|
|
$this->position = $position; |
64
|
|
|
$this->placeholderLength = $placeholderLength; |
65
|
|
|
|
66
|
|
|
$this->analyzePrepend($subject); |
67
|
|
|
$this->analyzeAppend($subject); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Whether an EOL character needs to be appended or prepended. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function requiresAdjustment() : bool |
76
|
|
|
{ |
77
|
|
|
return $this->requiresAppend() || $this->requiresPrepend(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function requiresPrepend() : bool |
81
|
|
|
{ |
82
|
|
|
return $this->prepend; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function requiresAppend() : bool |
86
|
|
|
{ |
87
|
|
|
return $this->append; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function analyzePrepend(string $subject) : void |
91
|
|
|
{ |
92
|
|
|
// we're at the beginning of the string |
93
|
|
|
if($this->position == 0) |
94
|
|
|
{ |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$prevPos = $this->position - $this->eolLength; |
99
|
|
|
|
100
|
|
|
if($prevPos < 0) |
101
|
|
|
{ |
102
|
|
|
$prevPos = 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$prev = mb_substr($subject, $prevPos, $this->eolLength); |
106
|
|
|
|
107
|
|
|
if($prev !== $this->formatter->getEOLChar()) |
108
|
|
|
{ |
109
|
|
|
$this->prepend = true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function analyzeAppend(string $subject) : void |
114
|
|
|
{ |
115
|
|
|
$subjectLength = mb_strlen($subject); |
116
|
|
|
|
117
|
|
|
$position = $this->position + $this->placeholderLength; |
118
|
|
|
|
119
|
|
|
// we're at the end of the string |
120
|
|
|
if($position >= $subjectLength) |
121
|
|
|
{ |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$nextPos = $position + $this->eolLength; |
126
|
|
|
|
127
|
|
|
if($nextPos > $subjectLength) |
128
|
|
|
{ |
129
|
|
|
$nextPos = $subjectLength - $this->eolLength; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$next = mb_substr($subject, $nextPos, $this->eolLength); |
133
|
|
|
|
134
|
|
|
if($next !== $this->formatter->getEOLChar()) |
135
|
|
|
{ |
136
|
|
|
$this->append = true; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|