| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser\Builder; |
| 4: | |
| 5: | use PhpParser; |
| 6: | use PhpParser\BuilderHelpers; |
| 7: | use PhpParser\Modifiers; |
| 8: | use PhpParser\Node; |
| 9: | use PhpParser\Node\Identifier; |
| 10: | use PhpParser\Node\Name; |
| 11: | use PhpParser\Node\Stmt; |
| 12: | use PhpParser\Node\ComplexType; |
| 13: | |
| 14: | class Property implements PhpParser\Builder { |
| 15: | protected string $name; |
| 16: | |
| 17: | protected int $flags = 0; |
| 18: | |
| 19: | protected ?Node\Expr $default = null; |
| 20: | |
| 21: | protected array $attributes = []; |
| 22: | |
| 23: | protected ?Node $type = null; |
| 24: | |
| 25: | protected array $attributeGroups = []; |
| 26: | |
| 27: | protected array $hooks = []; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | public function __construct(string $name) { |
| 35: | $this->name = $name; |
| 36: | } |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public function makePublic() { |
| 44: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC); |
| 45: | |
| 46: | return $this; |
| 47: | } |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function makeProtected() { |
| 55: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED); |
| 56: | |
| 57: | return $this; |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public function makePrivate() { |
| 66: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE); |
| 67: | |
| 68: | return $this; |
| 69: | } |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public function makeStatic() { |
| 77: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC); |
| 78: | |
| 79: | return $this; |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function makeReadonly() { |
| 88: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY); |
| 89: | |
| 90: | return $this; |
| 91: | } |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public function makeAbstract() { |
| 99: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::ABSTRACT); |
| 100: | |
| 101: | return $this; |
| 102: | } |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | public function makeFinal() { |
| 110: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL); |
| 111: | |
| 112: | return $this; |
| 113: | } |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | public function makePrivateSet() { |
| 121: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE_SET); |
| 122: | |
| 123: | return $this; |
| 124: | } |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | public function makeProtectedSet() { |
| 132: | $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED_SET); |
| 133: | |
| 134: | return $this; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | public function setDefault($value) { |
| 145: | $this->default = BuilderHelpers::normalizeValue($value); |
| 146: | |
| 147: | return $this; |
| 148: | } |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | public function setDocComment($docComment) { |
| 158: | $this->attributes = [ |
| 159: | 'comments' => [BuilderHelpers::normalizeDocComment($docComment)] |
| 160: | ]; |
| 161: | |
| 162: | return $this; |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | public function setType($type) { |
| 173: | $this->type = BuilderHelpers::normalizeType($type); |
| 174: | |
| 175: | return $this; |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | public function addAttribute($attribute) { |
| 186: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 187: | |
| 188: | return $this; |
| 189: | } |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | public function addHook(Node\PropertyHook $hook) { |
| 197: | $this->hooks[] = $hook; |
| 198: | |
| 199: | return $this; |
| 200: | } |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | public function getNode(): PhpParser\Node { |
| 208: | if ($this->flags & Modifiers::ABSTRACT && !$this->hooks) { |
| 209: | throw new PhpParser\Error('Only hooked properties may be declared abstract'); |
| 210: | } |
| 211: | |
| 212: | return new Stmt\Property( |
| 213: | $this->flags !== 0 ? $this->flags : Modifiers::PUBLIC, |
| 214: | [ |
| 215: | new Node\PropertyItem($this->name, $this->default) |
| 216: | ], |
| 217: | $this->attributes, |
| 218: | $this->type, |
| 219: | $this->attributeGroups, |
| 220: | $this->hooks |
| 221: | ); |
| 222: | } |
| 223: | } |
| 224: | |