| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser\Builder; |
| 4: | |
| 5: | use PhpParser; |
| 6: | use PhpParser\BuilderHelpers; |
| 7: | use PhpParser\Node; |
| 8: | use PhpParser\Node\Identifier; |
| 9: | use PhpParser\Node\Name; |
| 10: | use PhpParser\Node\Stmt; |
| 11: | use PhpParser\Node\ComplexType; |
| 12: | |
| 13: | class Property implements PhpParser\Builder |
| 14: | { |
| 15: | protected $name; |
| 16: | |
| 17: | protected $flags = 0; |
| 18: | protected $default = null; |
| 19: | protected $attributes = []; |
| 20: | |
| 21: | |
| 22: | protected $type; |
| 23: | |
| 24: | |
| 25: | protected $attributeGroups = []; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | public function __construct(string $name) { |
| 33: | $this->name = $name; |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function makePublic() { |
| 42: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC); |
| 43: | |
| 44: | return $this; |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | public function makeProtected() { |
| 53: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED); |
| 54: | |
| 55: | return $this; |
| 56: | } |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | public function makePrivate() { |
| 64: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE); |
| 65: | |
| 66: | return $this; |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | public function makeStatic() { |
| 75: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC); |
| 76: | |
| 77: | return $this; |
| 78: | } |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | public function makeReadonly() { |
| 86: | $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_READONLY); |
| 87: | |
| 88: | return $this; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public function setDefault($value) { |
| 99: | $this->default = BuilderHelpers::normalizeValue($value); |
| 100: | |
| 101: | return $this; |
| 102: | } |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public function setDocComment($docComment) { |
| 112: | $this->attributes = [ |
| 113: | 'comments' => [BuilderHelpers::normalizeDocComment($docComment)] |
| 114: | ]; |
| 115: | |
| 116: | return $this; |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public function setType($type) { |
| 127: | $this->type = BuilderHelpers::normalizeType($type); |
| 128: | |
| 129: | return $this; |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | public function addAttribute($attribute) { |
| 140: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 141: | |
| 142: | return $this; |
| 143: | } |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public function getNode() : PhpParser\Node { |
| 151: | return new Stmt\Property( |
| 152: | $this->flags !== 0 ? $this->flags : Stmt\Class_::MODIFIER_PUBLIC, |
| 153: | [ |
| 154: | new Stmt\PropertyProperty($this->name, $this->default) |
| 155: | ], |
| 156: | $this->attributes, |
| 157: | $this->type, |
| 158: | $this->attributeGroups |
| 159: | ); |
| 160: | } |
| 161: | } |
| 162: | |