|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Railt package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Railt\SDL\Frontend\Ast; |
|
13
|
|
|
|
|
14
|
|
|
use Phplrt\Contracts\Ast\NodeInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Node |
|
18
|
|
|
* |
|
19
|
|
|
* @property-read string $kind |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class Node implements NodeInterface, \JsonSerializable |
|
22
|
|
|
{ |
|
23
|
|
|
use ResolvableTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Location|null |
|
27
|
|
|
*/ |
|
28
|
|
|
public ?Location $loc = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return \Traversable|NodeInterface[] |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getIterator(): \Traversable |
|
34
|
|
|
{ |
|
35
|
|
|
foreach (\get_object_vars($this) as $property => $value) { |
|
36
|
|
|
if ($value instanceof NodeInterface || \is_array($value)) { |
|
37
|
|
|
yield $property => $value; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getOffset(): int |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->loc->start; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __toString(): string |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
|
|
return (string)\json_encode($this->jsonSerialize(), \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR); |
|
57
|
|
|
} catch (\JsonException $e) { |
|
58
|
|
|
return (string)\var_export($this, true); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function jsonSerialize(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->toArray(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function toArray(): array |
|
74
|
|
|
{ |
|
75
|
|
|
$properties = \array_merge(['kind' => $this->getKind()], \get_object_vars($this)); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($properties['loc'])) { |
|
78
|
|
|
unset($properties['loc']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $properties; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getKind(): string |
|
88
|
|
|
{ |
|
89
|
|
|
$fqn = \str_replace('\\', \DIRECTORY_SEPARATOR, static::class); |
|
90
|
|
|
|
|
91
|
|
|
return \basename($fqn, 'Node'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|