Passed
Push — master ( 40ca60...430c98 )
by Sebastian
02:56
created

hasNext()   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 getID() : string
61
    {
62
        return 'PH'.$this->placeholder->getID().'-IDX'.$this->index.'@'.$this->startPos;
63
    }
64
    
65
    public function getIndex() : int
66
    {
67
        return $this->index;
68
    }
69
    
70
    public function getPlaceholderString() : string
71
    {
72
        return $this->placeholder->getReplacementText();
73
    }
74
    
75
    public function getStartPosition() : int
76
    {
77
        return $this->startPos;
78
    }
79
    
80
    public function getEndPosition() : int
81
    {
82
        return $this->getStartPosition() + $this->getLength();
83
    }
84
    
85
    public function getLength() : int
86
    {
87
        return $this->length;
88
    }
89
    
90
    public function getSubjectString() : string
91
    {
92
        return $this->locator->getSubjectString();
93
    }
94
    
95
    public function getSubjectLength() : int
96
    {
97
        return mb_strlen($this->getSubjectString());
98
    }
99
    
100
    public function getPlaceholder() : Mailcode_Parser_Safeguard_Placeholder
101
    {
102
        return $this->placeholder;
103
    }
104
    
105
    public function hasNext() : bool
106
    {
107
        return $this->getNext() !== null;
108
    }
109
    
110
    public function hasPrevious() : bool
111
    {
112
        return $this->getPrevious() !== null;
113
    }
114
    
115
   /**
116
    * Retrieves the placeholder location right after this one, if any.
117
    * 
118
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location|NULL
119
    */
120
    public function getNext() : ?Mailcode_Parser_Safeguard_Placeholder_Locator_Location
121
    {
122
        return $this->locator->getLocationByIndex(($this->index-1));
123
    }
124
    
125
   /**
126
    * Retrieves the placeholder location right before this one, if any.
127
    * 
128
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location|NULL
129
    */
130
    public function getPrevious() : ?Mailcode_Parser_Safeguard_Placeholder_Locator_Location
131
    {
132
        return $this->locator->getLocationByIndex(($this->index+1));
133
    }
134
    
135
   /**
136
    * Retrieves all placeholder locations that come after this one, if any.
137
    * 
138
    * @return Mailcode_Parser_Safeguard_Placeholder_Locator_Location[]
139
    */
140
    public function getNextAll() : array
141
    {
142
        $locations = $this->locator->getLocations();
143
        
144
        return array_slice($locations, $this->index+1);
145
    }
146
    
147
    public function updatePositionByOffset(int $offset) : void
148
    {
149
        $this->startPos += $offset;
150
    }
151
}
152