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