| 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\Name; |
| 9: | use PhpParser\Node\Stmt; |
| 10: | |
| 11: | class Class_ extends Declaration |
| 12: | { |
| 13: | protected $name; |
| 14: | |
| 15: | protected $extends = null; |
| 16: | protected $implements = []; |
| 17: | protected $flags = 0; |
| 18: | |
| 19: | protected $uses = []; |
| 20: | protected $constants = []; |
| 21: | protected $properties = []; |
| 22: | protected $methods = []; |
| 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: | |
| 42: | |
| 43: | public function extend($class) { |
| 44: | $this->extends = BuilderHelpers::normalizeName($class); |
| 45: | |
| 46: | return $this; |
| 47: | } |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function implement(...$interfaces) { |
| 57: | foreach ($interfaces as $interface) { |
| 58: | $this->implements[] = BuilderHelpers::normalizeName($interface); |
| 59: | } |
| 60: | |
| 61: | return $this; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function makeAbstract() { |
| 70: | $this->flags = BuilderHelpers::addClassModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT); |
| 71: | |
| 72: | return $this; |
| 73: | } |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public function makeFinal() { |
| 81: | $this->flags = BuilderHelpers::addClassModifier($this->flags, Stmt\Class_::MODIFIER_FINAL); |
| 82: | |
| 83: | return $this; |
| 84: | } |
| 85: | |
| 86: | public function makeReadonly() { |
| 87: | $this->flags = BuilderHelpers::addClassModifier($this->flags, Stmt\Class_::MODIFIER_READONLY); |
| 88: | |
| 89: | return $this; |
| 90: | } |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public function addStmt($stmt) { |
| 100: | $stmt = BuilderHelpers::normalizeNode($stmt); |
| 101: | |
| 102: | $targets = [ |
| 103: | Stmt\TraitUse::class => &$this->uses, |
| 104: | Stmt\ClassConst::class => &$this->constants, |
| 105: | Stmt\Property::class => &$this->properties, |
| 106: | Stmt\ClassMethod::class => &$this->methods, |
| 107: | ]; |
| 108: | |
| 109: | $class = \get_class($stmt); |
| 110: | if (!isset($targets[$class])) { |
| 111: | throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); |
| 112: | } |
| 113: | |
| 114: | $targets[$class][] = $stmt; |
| 115: | |
| 116: | return $this; |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public function addAttribute($attribute) { |
| 127: | $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); |
| 128: | |
| 129: | return $this; |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | public function getNode() : PhpParser\Node { |
| 138: | return new Stmt\Class_($this->name, [ |
| 139: | 'flags' => $this->flags, |
| 140: | 'extends' => $this->extends, |
| 141: | 'implements' => $this->implements, |
| 142: | 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods), |
| 143: | 'attrGroups' => $this->attributeGroups, |
| 144: | ], $this->attributes); |
| 145: | } |
| 146: | } |
| 147: | |