Passed
Push — master ( eafb24...c53bf4 )
by Sebastian
04:30
created

resolvePositions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
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
    abstract public function format(string $subject) : string;
38
    
39
   /**
40
    * Resolves a list of positions of needle in the haystack.
41
    * 
42
    * @param string $needle
43
    * @param string $haystack
44
    * @return int[]
45
    */
46
    protected function resolvePositions(string $needle, string $haystack) : array
47
    {
48
        $lastPos = 0;
49
        $positions = array();
50
        
51
        while (($lastPos = mb_strpos($haystack, $needle, $lastPos))!== false)
52
        {
53
            $positions[] = $lastPos;
54
            $lastPos = $lastPos + mb_strlen($needle);
55
        }
56
        
57
        return $positions;
58
    }
59
    
60
   /**
61
    * Resolves the newline character used in the string.
62
    * 
63
    * @param string $subject
64
    * @return string
65
    */
66
    protected function resolveNewlineChar(string $subject) : string
67
    {
68
        $eol = ConvertHelper::detectEOLCharacter($subject);
69
        
70
        if($eol)
71
        {
72
            return $eol->getCharacter();
73
        }
74
        
75
        return PHP_EOL;
76
    }
77
    
78
   /**
79
    * Resolves the list of placeholder strings that need
80
    * to be formatted. This includes only commands that
81
    * do not generate content.
82
    *  
83
    * @return string[]
84
    */
85
    protected function resolvePlaceholderStrings() : array
86
    {
87
        $placeholders = $this->filterPlaceholders();
88
        
89
        $result = array();
90
        
91
        foreach($placeholders as $placeholder)
92
        {
93
            $result[] = $placeholder->getReplacementText();
94
        }
95
        
96
        return $result;
97
    }
98
    
99
   /**
100
    * @return \Mailcode\Mailcode_Parser_Safeguard_Placeholder[]
101
    */
102
    protected function filterPlaceholders()
103
    {
104
        return $this->safeguard->getPlaceholders();
105
    }
106
}
107