Test Failed
Push — master ( 81711e...bf8774 )
by Sebastian
03:41
created

getPlaceholder()   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_Type_SingleLines} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Formatter_Type_SingleLines
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Abstract safeguard formatter location: this is where the decision
16
 * is made whether a specific placeholder instance needs to be 
17
 * transformed according to the formatter. 
18
 *
19
 * @package Mailcode
20
 * @subpackage Parser
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
abstract class Mailcode_Parser_Safeguard_Formatter_Location
24
{
25
    const ERROR_PLACEHOLDER_NOT_FOUND = 66001;
26
    
27
   /**
28
    * @var Mailcode_Parser_Safeguard_Formatter
29
    */
30
    protected $formatter;
31
    
32
   /**
33
    * @var string
34
    */
35
    protected $append = '';
36
37
   /**
38
    * @var string
39
    */
40
    protected $prepend = '';
41
    
42
   /**
43
    * @var Mailcode_Parser_Safeguard_Placeholder
44
    */
45
    protected $placeholder;
46
    
47
   /**
48
    * @var Mailcode_StringContainer
49
    */
50
    protected $subject;
51
    
52
    public function __construct(Mailcode_Parser_Safeguard_Formatter $formatter, Mailcode_Parser_Safeguard_Placeholder $placeholder)
53
    {
54
        $this->formatter = $formatter;
55
        $this->placeholder = $placeholder;
56
        $this->subject = $formatter->getSubject();
57
        
58
        $this->init();
59
    }
60
    
61
    abstract protected function init() : void; 
62
    
63
    abstract public function requiresAdjustment() : bool;
64
    
65
   /**
66
    * @return int|boolean
67
    */
68
    public function getStartPosition()
69
    {
70
        return $this->subject->getSubstrPosition($this->placeholder->getReplacementText());
71
    }
72
    
73
   /**
74
    * Checks whether the specified position within the string
75
    * is within another command's placeholder (excluding this
76
    * location's placeholder).
77
    * 
78
    * @param int $position
79
    * @return bool
80
    */
81
    public function isWithinCommand(int $position) : bool
82
    {
83
        $placeholders = $this->formatter->getSafeguard()->getPlaceholders();
84
        
85
        $placeholderID = $this->placeholder->getID();
86
        
87
        foreach($placeholders as $placeholder)
88
        {
89
            if($placeholder->getID() === $placeholderID)
90
            {
91
                continue;
92
            }
93
            
94
            $start = $this->subject->getSubstrPosition($placeholder->getReplacementText());
95
            
96
            if($start === false)
97
            {
98
                continue;
99
            }
100
            
101
            $end = $start + $placeholder->getLength();
102
            
103
            if($position >= $start && $position <= $end)
104
            {
105
                return true;
106
            }
107
        }
108
        
109
        return false;
110
    }
111
    
112
   /**
113
    * @return int|boolean
114
    */
115
    public function getEndPosition()
116
    {
117
        $start = $this->getStartPosition();
118
        
119
        if($start !== false)
120
        {
121
            return $start + $this->placeholder->getLength();
122
        }
123
        
124
        return false;
125
    }
126
    
127
    public function getSubject() : Mailcode_StringContainer
128
    {
129
        return $this->subject;
130
    }
131
    
132
    public function getPlaceholder() : Mailcode_Parser_Safeguard_Placeholder
133
    {
134
        return $this->placeholder;
135
    }
136
    
137
   /**
138
    * Replaces the placeholder with the specified replacement text.
139
    * 
140
    * @param string $replacementText
141
    * @throws Mailcode_Exception
142
    * 
143
    * @see Mailcode_Parser_Safeguard_Formatter_Location::ERROR_PLACEHOLDER_NOT_FOUND
144
    */
145
    public function replaceWith(string $replacementText) : void
146
    {
147
        $needle = $this->placeholder->getReplacementText();
148
        
149
        if($this->subject->replaceSubstrings($needle, $replacementText))
150
        {
151
            return;
152
        }
153
        
154
        throw new Mailcode_Exception(
155
            'Could not find the placeholder to replace',
156
            sprintf(
157
                'The placeholder [%s] was not found in the string.',
158
                $needle
159
            ),
160
            self::ERROR_PLACEHOLDER_NOT_FOUND
161
        );
162
    }
163
    
164
    public function format() : void
165
    {
166
        if($this->requiresAdjustment() && (!empty($this->prepend) || !empty($this->append)))
167
        {
168
            $this->replaceWith(sprintf(
169
                '%s%s%s',
170
                $this->prepend,
171
                $this->placeholder->getReplacementText(),
172
                $this->append
173
            ));
174
        }
175
    }
176
}
177