Passed
Push — master ( cd2614...13a961 )
by Sebastian
03:27
created

getFormatting()   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} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Formatter
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\ConvertHelper;
15
use function AppUtils\parseVariable;
16
17
/**
18
 * Abstract base class for safeguard formatters: these 
19
 * are used to apply diverse formattings to the string
20
 * being parsed.
21
 *
22
 * @package Mailcode
23
 * @subpackage Parser
24
 * @author Sebastian Mordziol <[email protected]>
25
 */
26
abstract class Mailcode_Parser_Safeguard_Formatter
27
{
28
    const ERROR_INVALID_LOCATION_INSTANCE = 65601;
29
    
30
   /**
31
    * @var Mailcode_Parser_Safeguard_Formatting
32
    */
33
    protected $formatting;
34
    
35
   /**
36
    * @var Mailcode_StringContainer
37
    */
38
    protected $subject;
39
    
40
   /**
41
    * @var string[]
42
    */
43
    protected $log = array();
44
    
45
    public function __construct(Mailcode_Parser_Safeguard_Formatting $formatting)
46
    {
47
        $this->formatting = $formatting;
48
        $this->subject = $formatting->getSubject();
49
        
50
        $this->initFormatting();
51
    }
52
    
53
    public function getID() : string
54
    {
55
        $tokens = explode('_', get_class($this));
56
        
57
        return array_pop($tokens);
58
    }
59
    
60
    public function getFormatting() : Mailcode_Parser_Safeguard_Formatting
61
    {
62
        return $this->formatting;
63
    }
64
    
65
    public function getSubject() : Mailcode_StringContainer
66
    {
67
        return $this->subject;
68
    }
69
    
70
    public function getSafeguard() : Mailcode_Parser_Safeguard
71
    {
72
        return $this->formatting->getSafeguard();
73
    }
74
    
75
    abstract public function getPriority() : int;
76
    
77
    abstract protected function initFormatting() : void;
78
    
79
    protected function createLocation(Mailcode_Parser_Safeguard_Placeholder $placeholder) : Mailcode_Parser_Safeguard_Formatter_Location
80
    {
81
        $class = sprintf('Mailcode\Mailcode_Parser_Safeguard_Formatter_Type_%s_Location', $this->getID());
82
        
83
        $instance = new $class($this, $placeholder);
84
        
85
        if($instance instanceof Mailcode_Parser_Safeguard_Formatter_Location)
86
        {
87
            return $instance;
88
        }
89
        
90
        throw new Mailcode_Exception(
91
            'Invalid location instance created.',
92
            sprintf(
93
                'Expected a class of type [%s], got [%s].',
94
                Mailcode_Parser_Safeguard_Formatter_Location::class,
95
                parseVariable($instance)->enableType()->toString()
96
            ),
97
            self::ERROR_INVALID_LOCATION_INSTANCE
98
        );
99
    }
100
    
101
   /**
102
    * Retrieves all formatter-specific placeholder locations 
103
    * in the subject string.
104
    * 
105
    * @return Mailcode_Parser_Safeguard_Formatter_Location[]
106
    */
107
    protected function resolveLocations() : array
108
    {
109
        $placeholders = $this->formatting->getSafeguard()->getPlaceholders();
110
        
111
        $result = array();
112
        
113
        foreach($placeholders as $placeholder)
114
        {
115
            $result[] = $this->createLocation($placeholder);
116
        }
117
        
118
        return $result;
119
    }
120
    
121
   /**
122
    * Resolves the newline character used in the string.
123
    * 
124
    * @param string $subject
125
    * @return string
126
    */
127
    protected function resolveNewlineChar(string $subject) : string
128
    {
129
        $eol = ConvertHelper::detectEOLCharacter($subject);
130
        
131
        if($eol)
132
        {
133
            $this->log(sprintf(
134
                'Detected EOL character: %s.', 
135
                ConvertHelper::hidden2visible($eol->getCharacter())
136
            ));
137
            
138
            return $eol->getCharacter();
139
        }
140
        
141
        $this->log(sprintf(
142
            'Could not detect EOL character, using default: %s.', 
143
            ConvertHelper::hidden2visible(PHP_EOL)
144
        ));
145
        
146
        return PHP_EOL;
147
    }
148
    
149
    protected function log(string $message) : void
150
    {
151
        $this->log[] = sprintf(
152
            '%s Formatter | %s',
153
            $this->getID(),
154
            $message
155
        );
156
    }
157
 
158
   /**
159
    * @return string[]
160
    */
161
    public function getLog() : array
162
    {
163
        return $this->log;
164
    }
165
}
166