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

Mailcode_Parser_Safeguard_Formatter::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
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
16
/**
17
 * Abstract base class for safeguard formatters: these 
18
 * are used to apply diverse formattings to the string
19
 * being parsed.
20
 *
21
 * @package Mailcode
22
 * @subpackage Parser
23
 * @author Sebastian Mordziol <[email protected]>
24
 */
25
abstract class Mailcode_Parser_Safeguard_Formatter
26
{
27
   /**
28
    * @var Mailcode_Parser_Safeguard
29
    */
30
    protected $safeguard;
31
    
32
    public function __construct(Mailcode_Parser_Safeguard $safeguard)
33
    {
34
        $this->safeguard = $safeguard;
35
    }
36
    
37
    public function getID() : string
38
    {
39
        $tokens = explode('_', get_class($this));
40
        
41
        return array_pop($tokens);
42
    }
43
    
44
    abstract protected function initFormatting(string $subject) : string;
45
    
46
    abstract public function getReplaceNeedle(Mailcode_Parser_Safeguard_Placeholder $placeholder) : string; 
47
    
48
    protected function resolveReplacementText(Mailcode_Parser_Safeguard_Placeholder_Locator_Location $location) : string
49
    {
50
        $class = sprintf('Mailcode\Mailcode_Parser_Safeguard_Formatter_%s_Location', $this->getID());
51
        
52
        $info = new $class($this, $location);
53
        
54
        return $info->getPlaceholderText();
55
    }
56
    
57
    public function format(string $subject) : string
58
    {
59
        $subject = $this->initFormatting($subject);
60
        
61
        $placeholders = $this->filterPlaceholders();
62
        
63
        $total = count($placeholders);
64
        
65
        for($i=0; $i < $total; $i++)
66
        {
67
            $subject = $this->process($placeholders[$i], $subject);
68
        }
69
        
70
        return $subject;
71
    }
72
    
73
    protected function process(Mailcode_Parser_Safeguard_Placeholder $placeholder, string $subject) : string
74
    {
75
        $locator = $placeholder->createLocator($subject);
76
        $positions = $locator->getLocations();
77
        
78
        foreach($positions as $position)
79
        {
80
            $replace = $this->resolveReplacementText($position);
81
            
82
            if($replace !== $position->getPlaceholderString())
83
            {
84
                $locator->replaceWith($position, $replace);
85
            }
86
        }
87
        
88
        return $locator->getSubjectString();
89
    }
90
    
91
   /**
92
    * Resolves the newline character used in the string.
93
    * 
94
    * @param string $subject
95
    * @return string
96
    */
97
    protected function resolveNewlineChar(string $subject) : string
98
    {
99
        $eol = ConvertHelper::detectEOLCharacter($subject);
100
        
101
        if($eol)
102
        {
103
            return $eol->getCharacter();
104
        }
105
        
106
        return PHP_EOL;
107
    }
108
    
109
   /**
110
    * @return \Mailcode\Mailcode_Parser_Safeguard_Placeholder[]
111
    */
112
    protected function filterPlaceholders()
113
    {
114
        return $this->safeguard->getPlaceholders();
115
    }
116
}
117