Passed
Push — master ( 4344d4...81d6c9 )
by Martijn
02:43
created

Token::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 7
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
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 8
    public function __construct($group, $name, &$input, $offset, $length, &$children = [], $class = null)
35
    {
36 8
        $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 8
        $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 8
        $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 8
        $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 8
        $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 8
        $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 8
        $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 8
    }
44
45 8
    public function __get($name)
46
    {
47
        switch ($name) {
48 8
            case 'text':
49 6
                return substr($this->input, $this->offset, $this->length);
50
51
            default:
52 3
                if (property_exists($this, $name)) {
53 2
                    return $this->$name;
54
                }
55
        }
56
57 1
        throw new \Exception("Undefined property `{$name}`");
58
    }
59
60 1
    private function toString($depth = 0)
61
    {
62 1
        $signature = ($this->group
63 1
                ? $this->group . '::'
64 1
                : '')
65 1
            . ($this->name
66 1
                ? $this->name
67 1
                : $this->class);
68
69 1
        $output = str_repeat('  ', $depth) . "{$signature} (`{$this->text}`)";
70
71 1
        foreach ($this->children as $child) {
72 1
            $output .= PHP_EOL . $child->toString($depth + 1);
73
        }
74
75 1
        return $output;
76
    }
77
78 1
    public function __toString()
79
    {
80 1
        return $this->toString();
81
    }
82
83
    /**
84
     * @return \DOMDocument
85
     */
86 1
    public function toXml()
87
    {
88 1
        $document = new \DOMDocument();
89 1
        $document->appendChild($this->createXmlNode($document));
90 1
        $document->normalizeDocument();
91
92 1
        return $document;
93
    }
94
95
    /**
96
     * Create an XML node of this token
97
     *
98
     * @param \DOMDocument $document
99
     * @return \DOMElement
100
     */
101 1
    private function createXmlNode(\DOMDocument $document)
102
    {
103 1
        $value = $this->children
104 1
            ? null
105 1
            : $this->text;
106 1
        $name  = preg_replace('/[^-_a-zA-Z0-9]/', '_', $this->name
107 1
            ? $this->name
108 1
            : $this->class);
109 1
        $group = preg_replace('/[^-_a-zA-Z0-9]/', '_', $this->group);
110
111 1
        $element = $this->group
112 1
            ? $document->createElementNS($this->group, $group . ':' . $name, $value)
113 1
            : $document->createElement($name, $value);
114
115 1
        foreach ($this->children as $child) {
116 1
            $element->appendChild($child->createXmlNode($document));
117
        }
118
119 1
        return $element;
120
    }
121
122
    /**
123
     * Convert object to JSON for json_encode
124
     * Implements /JsonSerializable interface
125
     */
126 1
    public function jsonSerialize()
127
    {
128 1
        return $this->toArray();
129
    }
130
131
    /**
132
     * Output this node as an array.
133
     *
134
     * @return array
135
     */
136 3
    public function toArray()
137
    {
138
        $array = [
139 3
            'group'    => $this->group,
140 3
            'name'     => $this->name,
141 3
            'text'     => $this->text,
142 3
            'offset'   => $this->offset,
143 3
            'length'   => $this->length,
144 3
            'class'    => $this->class,
145
            'children' => [],
146
        ];
147
148 3
        foreach ($this->children as $child) {
149 2
            $array['children'][] = $child->toArray();
150
        }
151
152 3
        return $array;
153
    }
154
155
}