Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

Token::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Core;
4
5
/**
6
 * Class Token
7
 *
8
 * @package Vanderlee\Comprehend\Core
9
 *
10
 * @property-read Token[] $children
11
 * @property-read string $class
12
 * @property-read string $group
13
 * @property-read string $input
14
 * @property-read int $length
15
 * @property-read int $offset
16
 * @property-read string $name
17
 * @property-read string $text
18
 */
19
class Token implements \JsonSerializable
20
{
21
22
    private $group  = null;
23
    private $name   = null;
24
    private $input  = null;
25
    private $offset = null;
26
    private $length = null;
27
    private $class  = null;
28
29
    /**
30
     * @var Token[]
31
     */
32
    private $children = [];
33
34
    public function __construct($group, $name, &$input, $offset, $length, &$children = [], $class = null)
35
    {
36
        $this->group    = $group;
0 ignored issues
show
Bug introduced by
The property group is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
37
        $this->name     = $name;
0 ignored issues
show
Bug introduced by
The property name is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
38
        $this->input    = $input;
0 ignored issues
show
Bug introduced by
The property input is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
39
        $this->offset   = &$offset;
0 ignored issues
show
Bug introduced by
The property offset is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
40
        $this->length   = $length;
0 ignored issues
show
Bug introduced by
The property length is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
41
        $this->children = $children;
0 ignored issues
show
Bug introduced by
The property children is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
42
        $this->class    = $class;
0 ignored issues
show
Bug introduced by
The property class is declared read-only in Vanderlee\Comprehend\Core\Token.
Loading history...
43
    }
44
45
    public function __get($name)
46
    {
47
        switch ($name) {
48
            case 'text':
49
                return substr($this->input, $this->offset, $this->length);
50
51
            default:
52
                if (property_exists($this, $name)) {
53
                    return $this->$name;
54
                }
55
        }
56
57
        throw new \Exception("Undefined property `{$name}`");
58
    }
59
60
    private function toString($depth = 0)
61
    {
62
        $signature = ($this->group ? $this->group . '::' : '')
63
            . ($this->name ?? $this->class);
64
65
        $output = str_repeat('  ', $depth) . "{$signature} (`{$this->text}`)";
66
67
        foreach ($this->children as $child) {
68
            $output .= PHP_EOL . $child->toString($depth + 1);
69
        }
70
71
        return $output;
72
    }
73
74
    public function __toString()
75
    {
76
        return $this->toString();
77
    }
78
79
    /**
80
     * @return \DOMDocument
81
     */
82
    public function toXml()
83
    {
84
        $document = new \DOMDocument();
85
        $document->appendChild($this->createXmlNode($document));
86
        $document->normalizeDocument();
87
88
        return $document;
89
    }
90
91
    /**
92
     * Create an XML node of this token
93
     *
94
     * @param \DOMDocument $document
95
     * @return \DOMElement
96
     */
97
    private function createXmlNode(\DOMDocument $document)
98
    {
99
        $name  = $this->name ?? $this->class;
100
        $name  = preg_replace('/[^-_a-zA-Z0-9]/', '_', $name);
101
        $group = preg_replace('/[^-_a-zA-Z0-9]/', '_', $this->group);
102
        $value = $this->children ? null : $this->text;
103
104
        if ($this->group !== null) {
105
            $element = $document->createElementNS($this->group, $group . ':' . $name, $value);
106
        } else {
107
            $element = $document->createElement($name, $value);
108
        }
109
110
        foreach ($this->children as $child) {
111
            $element->appendChild($child->createXmlNode($document));
112
        }
113
114
        return $element;
115
    }
116
117
    /**
118
     * Convert object to JSON for json_encode
119
     * Implements /JsonSerializable interface
120
     */
121
    public function jsonSerialize()
122
    {
123
        return $this->toArray();
124
    }
125
126
    /**
127
     * Output this node as an array.
128
     *
129
     * @return array
130
     */
131
    public function toArray()
132
    {
133
        $array = [
134
            'group'    => $this->group,
135
            'name'     => $this->name,
136
            'text'     => $this->text,
137
            'offset'   => $this->offset,
138
            'length'   => $this->length,
139
            'class'    => $this->class,
140
            'children' => [],
141
        ];
142
143
        foreach ($this->children as $child) {
144
            $array['children'][] = $child->toArray();
145
        }
146
147
        return $array;
148
    }
149
150
}