|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\CodeAst\Builder; |
| 12 | + |
| 13 | +use OpenCodeModeling\CodeAst\Code\ClassConstGenerator; |
| 14 | +use OpenCodeModeling\CodeAst\Code\DocBlock\DocBlock; |
| 15 | +use OpenCodeModeling\CodeAst\Code\PropertyGenerator; |
| 16 | +use OpenCodeModeling\CodeAst\NodeVisitor\Property; |
| 17 | +use PhpParser\Node; |
| 18 | +use PhpParser\NodeTraverser; |
| 19 | +use PhpParser\NodeVisitor; |
| 20 | + |
| 21 | +final class ClassPropertyBuilder |
| 22 | +{ |
| 23 | + /** @var string */ |
| 24 | + private $name; |
| 25 | + |
| 26 | + /** @var string */ |
| 27 | + private $type; |
| 28 | + |
| 29 | + /** @var mixed */ |
| 30 | + private $defaultValue; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var int |
| 34 | + */ |
| 35 | + private $visibility; |
| 36 | + |
| 37 | + /** @var bool */ |
| 38 | + private $typed = false; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string|null |
| 42 | + */ |
| 43 | + private $docBlockComment; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string|null |
| 47 | + */ |
| 48 | + private $typeDocBlockHint; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var DocBlock|null |
| 52 | + */ |
| 53 | + private $docBlock; |
| 54 | + |
| 55 | + private function __construct() |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + public static function fromNode(Node\Stmt\Property $node): self |
| 60 | + { |
| 61 | + $self = new self(); |
| 62 | + |
| 63 | + $self->name = $node->props[0]->name->name; |
| 64 | + $self->defaultValue = $node->props[0]->default; |
| 65 | + $self->type = $node->type->toString(); |
| 66 | + $self->visibility = $node->flags; |
| 67 | + |
| 68 | + if ($self->type !== null) { |
| 69 | + $self->typed = true; |
| 70 | + } |
| 71 | + |
| 72 | + return $self; |
| 73 | + } |
| 74 | + |
| 75 | + public static function fromScratch(string $name, $type, bool $typed = true): self |
| 76 | + { |
| 77 | + $self = new self(); |
| 78 | + $self->name = $name; |
| 79 | + $self->type = $type; |
| 80 | + $self->typed = $typed; |
| 81 | + $self->visibility = ClassConstGenerator::FLAG_PRIVATE; |
| 82 | + |
| 83 | + return $self; |
| 84 | + } |
| 85 | + |
| 86 | + public function getName(): string |
| 87 | + { |
| 88 | + return $this->name; |
| 89 | + } |
| 90 | + |
| 91 | + public function getType(): string |
| 92 | + { |
| 93 | + return $this->type; |
| 94 | + } |
| 95 | + |
| 96 | + public function isTyped(): bool |
| 97 | + { |
| 98 | + return $this->typed; |
| 99 | + } |
| 100 | + |
| 101 | + public function setPrivate(): self |
| 102 | + { |
| 103 | + $this->visibility = ClassConstGenerator::FLAG_PRIVATE; |
| 104 | + |
| 105 | + return $this; |
| 106 | + } |
| 107 | + |
| 108 | + public function setProtected(): self |
| 109 | + { |
| 110 | + $this->visibility = ClassConstGenerator::FLAG_PROTECTED; |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + public function setPublic(): self |
| 116 | + { |
| 117 | + $this->visibility = ClassConstGenerator::FLAG_PUBLIC; |
| 118 | + |
| 119 | + return $this; |
| 120 | + } |
| 121 | + |
| 122 | + public function getDocBlockComment(): ?string |
| 123 | + { |
| 124 | + return $this->docBlockComment; |
| 125 | + } |
| 126 | + |
| 127 | + public function setDocBlockComment(?string $docBlockComment): void |
| 128 | + { |
| 129 | + $this->docBlockComment = $docBlockComment; |
| 130 | + } |
| 131 | + |
| 132 | + public function getTypeDocBlockHint(): string |
| 133 | + { |
| 134 | + return $this->typeDocBlockHint; |
| 135 | + } |
| 136 | + |
| 137 | + public function setTypeDocBlockHint(?string $typeDocBlockHint): void |
| 138 | + { |
| 139 | + $this->typeDocBlockHint = $typeDocBlockHint; |
| 140 | + } |
| 141 | + |
| 142 | + public function getDocBlock(): ?DocBlock |
| 143 | + { |
| 144 | + return $this->docBlock; |
| 145 | + } |
| 146 | + |
| 147 | + public function overrideDocBlock(?DocBlock $docBlock): void |
| 148 | + { |
| 149 | + $this->docBlock = $docBlock; |
| 150 | + } |
| 151 | + |
| 152 | + public function generate(): NodeVisitor |
| 153 | + { |
| 154 | + return new Property($this->propertyGenerator()); |
| 155 | + } |
| 156 | + |
| 157 | + private function propertyGenerator(): PropertyGenerator |
| 158 | + { |
| 159 | + $flags = $this->visibility; |
| 160 | + |
| 161 | + $propertyGenerator = new PropertyGenerator($this->name, $this->type, $this->defaultValue, $this->typed, $flags); |
| 162 | + |
| 163 | + $propertyGenerator->setDocBlockComment($this->docBlockComment); |
| 164 | + $propertyGenerator->setTypeDocBlockHint($this->typeDocBlockHint); |
| 165 | + $propertyGenerator->overrideDocBlock($this->docBlock); |
| 166 | + |
| 167 | + return $propertyGenerator; |
| 168 | + } |
| 169 | + |
| 170 | + public function injectVisitors(NodeTraverser $nodeTraverser): void |
| 171 | + { |
| 172 | + $nodeTraverser->addVisitor($this->generate()); |
| 173 | + } |
| 174 | +} |
0 commit comments