Symbol   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSymbolId() 0 3 1
A getShortcut() 0 3 1
A getIndex() 0 3 1
A __construct() 0 4 1
A attributeExists() 0 3 1
A setAttribute() 0 7 2
A getAttribute() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Parser;
6
7
use Remorhaz\UniLex\AttributeListInterface;
8
use Remorhaz\UniLex\Exception;
9
use Remorhaz\UniLex\Stack\StackableSymbolInterface;
10
11
class Symbol implements StackableSymbolInterface, AttributeListInterface
12
{
13
    private $symbolId;
14
15
    private $attributeList = [];
16
17
    private $index;
18
19
    public function __construct(int $index, int $symbolId)
20
    {
21
        $this->index = $index;
22
        $this->symbolId = $symbolId;
23
    }
24
25
    public function getSymbolId(): int
26
    {
27
        return $this->symbolId;
28
    }
29
30
    public function getIndex(): int
31
    {
32
        return $this->index;
33
    }
34
35
    /**
36
     * @param string $name
37
     * @return mixed
38
     * @throws Exception
39
     */
40
    public function getAttribute(string $name)
41
    {
42
        if (!array_key_exists($name, $this->attributeList)) {
43
            throw new Exception("Attribute '{$name}' not defined in node {$this->getIndex()}");
44
        }
45
        return $this->attributeList[$name];
46
    }
47
48
    /**
49
     * @param string $name
50
     * @param $value
51
     * @return $this
52
     * @throws Exception
53
     */
54
    public function setAttribute(string $name, $value)
55
    {
56
        if ($this->attributeExists($name)) {
57
            throw new Exception("Attribute '{$name}' is already defined in node {$this->getIndex()}");
58
        }
59
        $this->attributeList[$name] = $value;
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $name
65
     * @return bool
66
     */
67
    public function attributeExists(string $name): bool
68
    {
69
        return isset($this->attributeList[$name]);
70
    }
71
72
    /**
73
     * @return array|AttributeListShortcut
74
     */
75
    public function getShortcut(): AttributeListShortcut
76
    {
77
        return new AttributeListShortcut($this);
78
    }
79
}
80