1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the php-code-generator package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @license Apache-2.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gossi\codegen\parser\visitor\parts; |
11
|
|
|
|
12
|
|
|
use gossi\codegen\model\ValueInterface; |
13
|
|
|
use gossi\codegen\parser\PrettyPrinter; |
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpParser\Node\Const_; |
16
|
|
|
use PhpParser\Node\Expr\Array_; |
17
|
|
|
use PhpParser\Node\Expr\ClassConstFetch; |
18
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
19
|
|
|
use PhpParser\Node\Scalar\DNumber; |
20
|
|
|
use PhpParser\Node\Scalar\LNumber; |
21
|
|
|
use PhpParser\Node\Scalar\MagicConst; |
22
|
|
|
use PhpParser\Node\Scalar\String_; |
23
|
|
|
|
24
|
|
|
trait ValueParserPart { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool[] |
28
|
|
|
*/ |
29
|
|
|
private array $constMap = [ |
|
|
|
|
30
|
7 |
|
'false' => false, |
31
|
7 |
|
'true' => true |
32
|
7 |
|
]; |
33
|
5 |
|
|
34
|
5 |
|
/** |
35
|
|
|
* Parses the value of a node into the model |
36
|
3 |
|
* |
37
|
|
|
* @param ValueInterface $obj |
38
|
|
|
* @param Node $node |
39
|
7 |
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
private function parseValue(ValueInterface $obj, Node $node): void { |
43
|
|
|
$value = $node instanceof Const_ ? $node->value : $node->default; |
44
|
|
|
if ($value !== null) { |
45
|
|
|
$this->isPrimitive($value) ? $obj->setValue($this->getPrimitiveValue($value)) : |
46
|
|
|
$obj->setExpression($this->getExpression($value)); |
47
|
5 |
|
} |
48
|
5 |
|
} |
49
|
4 |
|
|
50
|
4 |
|
/** |
51
|
4 |
|
* Returns whether this node is a primitive value |
52
|
5 |
|
* |
53
|
|
|
* @param Node $node |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
private function isPrimitive(Node $node): bool { |
58
|
|
|
return $node instanceof String_ |
59
|
|
|
|| $node instanceof LNumber |
60
|
|
|
|| $node instanceof DNumber |
61
|
5 |
|
|| $this->isBool($node) |
62
|
5 |
|
|| $this->isNull($node); |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
5 |
|
* Returns the primitive value |
67
|
2 |
|
* |
68
|
|
|
* @param Node $node |
69
|
|
|
* |
70
|
5 |
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
private function getPrimitiveValue(Node $node): mixed { |
73
|
|
|
if ($this->isBool($node)) { |
74
|
|
|
return (bool) $this->getExpression($node); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->isNull($node)) { |
78
|
|
|
return null; |
79
|
5 |
|
} |
80
|
5 |
|
|
81
|
3 |
|
return $node->value; |
82
|
3 |
|
} |
83
|
3 |
|
|
84
|
|
|
/** |
85
|
|
|
* Returns whether this node is a boolean value |
86
|
2 |
|
* |
87
|
|
|
* @param Node $node |
88
|
5 |
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
private function isBool(Node $node): bool { |
92
|
|
|
if ($node instanceof ConstFetch) { |
93
|
|
|
$const = $node->name->parts[0]; |
94
|
|
|
|
95
|
|
|
if (isset($this->constMap[$const])) { |
96
|
5 |
|
return is_bool($this->constMap[$const]); |
97
|
5 |
|
} |
98
|
2 |
|
} |
99
|
2 |
|
|
100
|
|
|
return false; |
101
|
5 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns whether this node is a null value |
105
|
|
|
* |
106
|
|
|
* @param Node $node |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
4 |
|
*/ |
110
|
4 |
|
private function isNull(Node $node): bool { |
111
|
3 |
|
if ($node instanceof ConstFetch) { |
112
|
3 |
|
$const = $node->name->parts[0]; |
113
|
3 |
|
|
114
|
|
|
return $const === 'null'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
3 |
|
|
120
|
2 |
|
/** |
121
|
|
|
* Returns the value from a node |
122
|
|
|
* |
123
|
2 |
|
* @param Node $node |
124
|
1 |
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
2 |
|
private function getExpression(Node $node): mixed { |
128
|
2 |
|
return match (true) { |
129
|
2 |
|
$node instanceof ConstFetch => $this->constMap[$node->name->parts[0]] ?? $node->name->parts[0], |
130
|
|
|
$node instanceof ClassConstFetch => (!$node->class instanceof Node\Name ?: $node->class->parts[0]) . '::' . $node->name, |
131
|
|
|
$node instanceof MagicConst => $node->getName(), |
132
|
|
|
$node instanceof Array_ => (new PrettyPrinter())->prettyPrintExpr($node) |
133
|
|
|
}; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|