TranslationScheme::applySymbolActions()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 4
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Pointer\Parser;
6
7
use Remorhaz\JSON\Pointer\Locator\LocatorBuilderInterface;
8
use Remorhaz\UniLex\Grammar\SDD\TranslationSchemeInterface;
9
use Remorhaz\UniLex\Lexer\Token;
10
use Remorhaz\UniLex\Parser\Production;
11
use Remorhaz\UniLex\Parser\Symbol;
12
13
final class TranslationScheme implements TranslationSchemeInterface
14
{
15
16
    private $locatorBuilder;
17
18 20
    public function __construct(LocatorBuilderInterface $locatorBuilder)
19
    {
20 20
        $this->locatorBuilder = $locatorBuilder;
21 20
    }
22
23 18
    public function applyTokenActions(Symbol $symbol, Token $token): void
24
    {
25 18
        $s = $symbol->getShortcut();
26 18
        $t = $token->getShortcut();
27 18
        switch ($symbol->getSymbolId()) {
28 18
            case SymbolType::T_UNESCAPED:
29 18
            case SymbolType::T_SLASH:
30 15
            case SymbolType::T_TILDE:
31 10
            case SymbolType::T_ZERO:
32 8
            case SymbolType::T_ONE:
33 18
                $s['s.text'] = $t['text'];
34 18
                break;
35
        }
36 18
    }
37
38 14
    public function applyProductionActions(Production $production): void
39
    {
40 14
        $header = $production->getHeaderShortcut();
41 14
        $symbols = $production->getSymbolListShortcut();
42 14
        $hash = "{$production->getHeader()->getSymbolId()}.{$production->getIndex()}";
43
        switch ($hash) {
44 14
            case SymbolType::NT_REFERENCE . '.0':
45
                // [ 0:NT_REFERENCE_PART, 1:NT_REFERENCE ]
46 14
            case SymbolType::NT_ESCAPED . '.0':
47
                // [ 0:T_TILDE, 1:NT_ESCAPED_SYMBOL ]
48 12
                $header['s.text'] = $symbols[1]['s.text'];
49 12
                break;
50
51 14
            case SymbolType::NT_REFERENCE . '.1':
52
                // [ ]
53 13
                $header['s.text'] = $header['i.text'];
54 13
                break;
55
56 14
            case SymbolType::NT_REFERENCE_PART . '.0':
57
                // [ 0:NT_UNESCAPED ]
58 14
            case SymbolType::NT_REFERENCE_PART . '.1':
59
                // [ 0:NT_ESCAPED ]
60 14
            case SymbolType::NT_UNESCAPED . '.0':
61
                // [ 0:T_UNESCAPED ]
62 14
            case SymbolType::NT_UNESCAPED . '.1':
63
                // [ 0:T_ZERO ]
64 14
            case SymbolType::NT_UNESCAPED . '.2':
65
                // [ 0:T_ONE ]
66 12
                $header['s.text'] = $symbols[0]['s.text'];
67 12
                break;
68
69 14
            case SymbolType::NT_ESCAPED_SYMBOL . '.0':
70
                // [ 0:T_ZERO ]
71 6
                $header['s.text'] = '~';
72 6
                break;
73
74 14
            case SymbolType::NT_ESCAPED_SYMBOL . '.1':
75
                // [ 0:T_ONE ]
76 5
                $header['s.text'] = '/';
77 5
                break;
78
        }
79 14
    }
80
81 20
    public function applySymbolActions(Production $production, int $symbolIndex): void
82
    {
83 20
        $header = $production->getHeaderShortcut();
84 20
        $symbols = $production->getSymbolListShortcut();
85 20
        $hash = "{$production->getHeader()->getSymbolId()}.{$production->getIndex()}.{$symbolIndex}";
86
        switch ($hash) {
87 20
            case SymbolType::NT_REFERENCE_LIST . '.0.1':
88
                // [ 0:T_SLASH, 1:NT_REFERENCE, 2:NT_REFERENCE_LIST ]
89 18
                $symbols[1]['i.text'] = '';
90 18
                break;
91
92 20
            case SymbolType::NT_REFERENCE_LIST . '.0.2':
93
                // [ 0:T_SLASH, 1:NT_REFERENCE, 2:NT_REFERENCE_LIST ]
94
                $this
95 13
                    ->locatorBuilder
96 13
                    ->addReference($symbols[1]['s.text']);
97 13
                break;
98
99 20
            case SymbolType::NT_REFERENCE . '.0.1':
100
                // [ 0:NT_REFERENCE_PART, 1:NT_REFERENCE ]
101 12
                $symbols[1]['i.text'] = $header['i.text'] . $symbols[0]['s.text'];
102 12
                break;
103
        }
104 20
    }
105
}
106