Passed
Push — master ( 852e13...6d4e0e )
by Edward
03:37
created

TokenMatcherTemplate.php$0 ➔ isVisitedTransition()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 9
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace Remorhaz\UniLex\Lexer;
4
5
use Remorhaz\UniLex\Exception;
6
use Remorhaz\UniLex\IO\CharBufferInterface;
7
use Remorhaz\UniLex\IO\TokenExtractInterface;
8
9
abstract class TokenMatcherTemplate implements TokenMatcherInterface
10
{
11
12
    private $token;
13
14
    private $mode = self::DEFAULT_MODE;
15
16
    /**
17
     * @return Token
18
     * @throws Exception
19
     */
20
    public function getToken(): Token
21
    {
22
        if (!isset($this->token)) {
23
            throw new Exception("Token is not defined");
24
        }
25
26
        return $this->token;
27
    }
28
29
    protected function createContext(
30
        CharBufferInterface $buffer,
31
        TokenFactoryInterface $tokenFactory
32
    ): TokenMatcherContextInterface {
33
        $onConstruct = function (): void {
34
            unset($this->token);
35
        };
36
        $onSetNewToken = function (int $tokenType) use ($tokenFactory): void {
37
            $this->token = $tokenFactory->createToken($tokenType);
38
        };
39
        $onGetToken = function (): Token {
40
            return $this->getToken();
41
        };
42
        $onSetMode = function (string $mode): void {
43
            $this->mode = $mode;
44
        };
45
        $onGetMode = function (): string {
46
            return $this->mode;
47
        };
48
49
        return new class (
50
            $buffer,
51
            $onConstruct,
52
            $onSetNewToken,
53
            $onGetToken,
54
            $onSetMode,
55
            $onGetMode
56
        ) implements TokenMatcherContextInterface {
57
58
            private $buffer;
59
60
            private $onSetNewToken;
61
62
            private $onGetToken;
63
64
            private $onSetMode;
65
66
            private $onGetMode;
67
68
            public function __construct(
69
                CharBufferInterface $buffer,
70
                callable $onConstruct,
71
                callable $onSetNewToken,
72
                callable $onGetToken,
73
                callable $onSetMode,
74
                callable $onGetMode
75
            ) {
76
                $this->buffer = $buffer;
77
                $this->onSetNewToken = $onSetNewToken;
78
                $this->onGetToken = $onGetToken;
79
                $this->onSetMode = $onSetMode;
80
                $this->onGetMode = $onGetMode;
81
                call_user_func($onConstruct);
82
            }
83
84
            public function setNewToken(int $tokenType): TokenMatcherContextInterface
85
            {
86
                call_user_func($this->onSetNewToken, $tokenType);
87
88
                return $this;
89
            }
90
91
            /**
92
             * @param string $name
93
             * @param        $value
94
             * @return TokenMatcherContextInterface
95
             */
96
            public function setTokenAttribute(string $name, $value): TokenMatcherContextInterface
97
            {
98
                $this
99
                    ->getToken()
100
                    ->setAttribute($name, $value);
101
102
                return $this;
103
            }
104
105
            public function getToken(): Token
106
            {
107
                return call_user_func($this->onGetToken);
108
            }
109
110
            public function getBuffer(): CharBufferInterface
111
            {
112
                return $this->buffer;
113
            }
114
115
            public function getSymbolString(): string
116
            {
117
                $buffer = $this->getBuffer();
118
                if ($buffer instanceof TokenExtractInterface) {
119
                    return $buffer->getTokenAsString();
120
                }
121
                throw new Exception("Extracting strings is not supported by buffer");
122
            }
123
124
            public function getSymbolList(): array
125
            {
126
                $buffer = $this->getBuffer();
127
                if ($buffer instanceof TokenExtractInterface) {
128
                    return $buffer->getTokenAsArray();
129
                }
130
                throw new Exception("Extracting arrays is not supported by buffer");
131
            }
132
133
            public function getMode(): string
134
            {
135
                return call_user_func($this->onGetMode);
136
            }
137
138
            public function setMode(string $mode): TokenMatcherContextInterface
139
            {
140
                call_user_func($this->onSetMode, $mode);
141
142
                return $this;
143
            }
144
        };
145
    }
146
}
147