-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassConstBuilder.php
More file actions
89 lines (70 loc) · 2.11 KB
/
ClassConstBuilder.php
File metadata and controls
89 lines (70 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
*/
declare(strict_types=1);
namespace OpenCodeModeling\CodeAst\Builder;
use OpenCodeModeling\CodeAst\Code\ClassConstGenerator;
use OpenCodeModeling\CodeAst\Code\IdentifierGenerator;
use OpenCodeModeling\CodeAst\NodeVisitor\ClassConstant;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
final class ClassConstBuilder
{
use VisibilityTrait;
/** @var string */
private string $name;
/** @var mixed */
private $value;
private function __construct()
{
}
public static function fromNode(Node\Stmt\ClassConst $node): self
{
$self = new self();
$self->name = $node->consts[0]->name->name;
if ($node->consts[0]->value instanceof Node\Scalar) {
$self->value = $node->consts[0]->value->value;
} else {
// use node expression
$self->value = $node->consts[0]->value;
}
$self->visibility = $node->flags;
return $self;
}
public static function fromScratch(string $name, $value, $visibility = ClassConstGenerator::FLAG_PUBLIC): self
{
$self = new self();
$self->name = $name;
$self->value = $value;
$self->visibility = $visibility;
return $self;
}
public function getName(): string
{
return $this->name;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
public function generate(): NodeVisitor
{
return new ClassConstant(
new IdentifierGenerator(
$this->name,
new ClassConstGenerator($this->name, $this->value, $this->visibility)
)
);
}
public function injectVisitors(NodeTraverser $nodeTraverser): void
{
$nodeTraverser->addVisitor($this->generate());
}
}