Test Failed
Push — master ( 956a64...151bf3 )
by Sebastian
02:46
created

Mailcode_Parser_Safeguard_Formatter::getLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 getSubject() : Mailcode_StringContainer
61
    {
62
        return $this->subject;
63
    }
64
    
65
    public function getSafeguard() : Mailcode_Parser_Safeguard
66
    {
67
        return $this->formatting->getSafeguard();
68
    }
69
    
70
    abstract public function getPriority() : int;
71
    
72
    abstract protected function initFormatting() : void;
73
    
74
    protected function createLocation(Mailcode_Parser_Safeguard_Placeholder $placeholder) : Mailcode_Parser_Safeguard_Formatter_Location
75
    {
76
        $class = sprintf('Mailcode\Mailcode_Parser_Safeguard_Formatter_Type_%s_Location', $this->getID());
77
        
78
        $instance = new $class($this, $placeholder);
79
        
80
        if($instance instanceof Mailcode_Parser_Safeguard_Formatter_Location)
81
        {
82
            return $instance;
83
        }
84
        
85
        throw new Mailcode_Exception(
86
            'Invalid location instance created.',
87
            sprintf(
88
                'Expected a class of type [%s], got [%s].',
89
                Mailcode_Parser_Safeguard_Formatter_Location::class,
90
                parseVariable($instance)->enableType()->toString()
91
            ),
92
            self::ERROR_INVALID_LOCATION_INSTANCE
93
        );
94
    }
95
    
96
   /**
97
    * Retrieves all formatter-specific placeholder locations 
98
    * in the subject string.
99
    * 
100
    * @return Mailcode_Parser_Safeguard_Formatter_Location[]
101
    */
102
    protected function resolveLocations() : array
103
    {
104
        $placeholders = $this->formatting->getSafeguard()->getPlaceholders();
105
        
106
        $result = array();
107
        
108
        foreach($placeholders as $placeholder)
109
        {
110
            $result[] = $this->createLocation($placeholder);
111
        }
112
        
113
        return $result;
114
    }
115
    
116
   /**
117
    * Resolves the newline character used in the string.
118
    * 
119
    * @param string $subject
120
    * @return string
121
    */
122
    protected function resolveNewlineChar(string $subject) : string
123
    {
124
        $eol = ConvertHelper::detectEOLCharacter($subject);
125
        
126
        if($eol)
127
        {
128
            $this->log(sprintf(
129
                'Detected EOL character: %s.', 
130
                ConvertHelper::hidden2visible($eol->getCharacter())
131
            ));
132
            
133
            return $eol->getCharacter();
134
        }
135
        
136
        $this->log(sprintf(
137
            'Could not detect EOL character, using default: %s.', 
138
            ConvertHelper::hidden2visible(PHP_EOL)
139
        ));
140
        
141
        return PHP_EOL;
142
    }
143
    
144
    protected function log(string $message) : void
145
    {
146
        $this->log[] = sprintf(
147
            '%s Formatter | %s',
148
            $this->getID(),
149
            $message
150
        );
151
    }
152
 
153
   /**
154
    * @return string[]
155
    */
156
    public function getLog() : array
157
    {
158
        return $this->log;
159
    }
160
}
161