Passed
Push — master ( 9253d7...e42fcd )
by Sebastian
03:09
created

getTokens()   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
declare(strict_types=1);
4
5
namespace Mailcode;
6
7
abstract class Mailcode_Parser_Statement_Tokenizer_Process
8
{
9
    /**
10
     * @var Mailcode_Parser_Statement_Tokenizer
11
     */
12
    protected $tokenizer;
13
14
    /**
15
     * @var string
16
     */
17
    protected $tokenized;
18
19
    /**
20
     * @var Mailcode_Parser_Statement_Tokenizer_Token[]
21
     */
22
    protected $tokensTemporary = array();
23
24
    /**
25
     * @var string
26
     */
27
    protected $delimiter = '§§';
28
29
    /**
30
     * Mailcode_Parser_Statement_Tokenizer_Process constructor.
31
     * @param Mailcode_Parser_Statement_Tokenizer $tokenizer
32
     * @param string $tokenized
33
     * @param Mailcode_Parser_Statement_Tokenizer_Token[] $tokens
34
     */
35
    public function __construct(Mailcode_Parser_Statement_Tokenizer $tokenizer, string $tokenized, array $tokens)
36
    {
37
        $this->tokenizer = $tokenizer;
38
        $this->tokenized = $tokenized;
39
        $this->tokensTemporary = $tokens;
40
    }
41
42
    public function getStatement() : string
43
    {
44
        return $this->tokenized;
45
    }
46
47
    /**
48
     * @return Mailcode_Parser_Statement_Tokenizer_Token[]
49
     */
50
    public function getTokens() : array
51
    {
52
        return $this->tokensTemporary;
53
    }
54
55
    public function process() : void
56
    {
57
        $this->_process();
58
    }
59
60
    abstract protected function _process() : void;
61
62
    /**
63
     * Registers a token to add in the statement string.
64
     *
65
     * @param string $type
66
     * @param string $matchedText
67
     * @param mixed $subject
68
     */
69
    protected function registerToken(string $type, string $matchedText, $subject=null) : void
70
    {
71
        $this->tokensTemporary[] = $this->createToken($type, $matchedText, $subject);
72
    }
73
74
    /**
75
     * @param string $type
76
     * @param string $matchedText
77
     * @param mixed $subject
78
     * @return Mailcode_Parser_Statement_Tokenizer_Token
79
     */
80
    public function createToken(string $type, string $matchedText, $subject=null) : Mailcode_Parser_Statement_Tokenizer_Token
81
    {
82
        $token = $this->tokenizer->createToken($type, $matchedText, $subject);
83
        $tokenID = $token->getID();
84
85
        $this->tokenized = str_replace(
86
            $matchedText,
87
            $this->delimiter.$tokenID.$this->delimiter,
88
            $this->tokenized
89
        );
90
91
        return $token;
92
    }
93
94
    protected function getTokenByID(string $tokenID) : ?Mailcode_Parser_Statement_Tokenizer_Token
95
    {
96
        foreach($this->tokensTemporary as $token)
97
        {
98
            if($token->getID() === $tokenID)
99
            {
100
                return $token;
101
            }
102
        }
103
104
        return null;
105
    }
106
}