Passed
Push — master ( b87bd2...81711e )
by Sebastian
03:45
created

getMatchedText()   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_Placeholder_Location} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Placeholder_Location
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Placeholder location container: stores information on
16
 * a placeholder's location within a subject string.
17
 *
18
 * @package Mailcode
19
 * @subpackage Parser
20
 * @author Sebastian Mordziol <[email protected]>
21
 * 
22
 * @see Mailcode_Parser_Safeguard_Placeholder::localizeInstances()
23
 */
24
class Mailcode_Parser_Safeguard_Placeholder_Locator_Location
25
{
26
   /**
27
    * @var Mailcode_Parser_Safeguard_Placeholder
28
    */
29
    private $placeholder;
30
    
31
   /**
32
    * @var int
33
    */
34
    private $startPos;
35
    
36
   /**
37
    * @var int
38
    */
39
    private $length;
40
    
41
   /**
42
    * @var int
43
    */
44
    private $index;
45
    
46
   /**
47
    * @var Mailcode_Parser_Safeguard_Placeholder_Locator
48
    */
49
    private $locator;
50
    
51
    public function __construct(Mailcode_Parser_Safeguard_Placeholder_Locator $locator, Mailcode_Parser_Safeguard_Placeholder $placeholder, int $index, int $startPos, int $length)
52
    {
53
        $this->placeholder = $placeholder;
54
        $this->index = $index;
55
        $this->startPos = $startPos;
56
        $this->length = $length;
57
        $this->locator = $locator;
58
    }
59
    
60
    public function getMatchedText() : string
61
    {
62
        return trim(mb_substr($this->getSubjectString(), $this->startPos, $this->length));
63
    }
64
    
65
    public function getID() : string
66
    {
67
        return 'PH'.$this->placeholder->getID().'-IDX'.$this->index;
68
    }
69
    
70
    public function getIndex() : int
71
    {
72
        return $this->index;
73
    }
74
    
75
    public function getPlaceholderString() : string
76
    {
77
        return $this->placeholder->getReplacementText();
78
    }
79
    
80
    public function getStartPosition() : int
81
    {
82
        return $this->startPos;
83
    }
84
    
85
    public function getEndPosition() : int
86
    {
87
        return $this->getStartPosition() + $this->getLength();
88
    }
89
    
90
    public function getLength() : int
91
    {
92
        return $this->length;
93
    }
94
    
95
    public function getSubjectString() : string
96
    {
97
        return $this->locator->getSubjectString();
98
    }
99
    
100
    public function getSubjectLength() : int
101
    {
102
        return mb_strlen($this->getSubjectString());
103
    }
104
    
105
    public function getPlaceholder() : Mailcode_Parser_Safeguard_Placeholder
106
    {
107
        return $this->placeholder;
108
    }
109
    
110
    public function hasNext() : bool
111
    {
112
        return $this->getNext() !== null;
113
    }
114
    
115
    public function hasPrevious() : bool
116
    {
117
        return $this->getPrevious() !== null;
118
    }
119
    
120
   /**
121
    * Retrieves the placeholder location right after this one, if any.
122
    * 
123
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location|NULL
124
    */
125
    public function getNext() : ?Mailcode_Parser_Safeguard_Placeholder_Locator_Location
126
    {
127
        return $this->locator->getLocationByIndex(($this->index-1));
128
    }
129
    
130
   /**
131
    * Retrieves the placeholder location right before this one, if any.
132
    * 
133
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location|NULL
134
    */
135
    public function getPrevious() : ?Mailcode_Parser_Safeguard_Placeholder_Locator_Location
136
    {
137
        return $this->locator->getLocationByIndex(($this->index+1));
138
    }
139
    
140
   /**
141
    * Retrieves all placeholder locations that come after this one, if any.
142
    * 
143
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location[]
144
    */
145
    public function getNextAll() : array
146
    {
147
        $locations = $this->locator->getLocations();
148
        
149
        return array_slice($locations, $this->index+1);
150
    }
151
    
152
   /**
153
    * Called when the location has been moved in the subject string.
154
    * @param int $offset
155
    */
156
    public function updatePositionByOffset(int $offset) : void
157
    {
158
        $this->startPos += $offset;
159
    }
160
}
161