|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Compiler\Ast\Value; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\LeafInterface; |
|
13
|
|
|
use Railt\Parser\Ast\NodeInterface; |
|
14
|
|
|
use Railt\Parser\Ast\Rule; |
|
15
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class InputValueNode |
|
19
|
|
|
*/ |
|
20
|
|
|
class InputValueNode extends Rule implements ValueInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public function toString(): string |
|
26
|
|
|
{ |
|
27
|
|
|
$result = []; |
|
28
|
|
|
|
|
29
|
|
|
foreach ($this->getValues() as $key => $value) { |
|
30
|
|
|
$result[] = $key->getValue() . ': ' . $value->toString(); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return \sprintf('{%s}', \implode(', ', $result)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return iterable|ValueInterface[] |
|
38
|
|
|
*/ |
|
39
|
|
|
public function toPrimitive(): iterable |
|
40
|
|
|
{ |
|
41
|
|
|
$result = []; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($this->getValues() as $key => $value) { |
|
44
|
|
|
$result[$key->getValue()] = $value->toPrimitive(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $result; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return iterable|ValueInterface[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getValues(): iterable |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var RuleInterface $child */ |
|
56
|
|
|
foreach ($this->getChildren() as $child) { |
|
57
|
|
|
yield $this->key($child) => $this->value($child)->getInnerValue(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param RuleInterface $rule |
|
63
|
|
|
* @return LeafInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private function key(RuleInterface $rule): LeafInterface |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var RuleInterface $key */ |
|
68
|
|
|
$key = $rule->first('Key', 1); |
|
69
|
|
|
|
|
70
|
|
|
return $key->getChild(0); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param RuleInterface $rule |
|
75
|
|
|
* @return ValueInterface|NodeInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
private function value(RuleInterface $rule): ValueInterface |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var ValueNode $value */ |
|
80
|
|
|
return $rule->first('Value', 1); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.