php-parser/lib/PhpParser/Builder/Interface_.php

94 lines
2.4 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
2013-05-23 10:31:59 +02:00
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Interface_ extends Declaration
2013-05-23 10:31:59 +02:00
{
protected $name;
protected $extends = [];
protected $constants = [];
protected $methods = [];
2013-05-23 10:31:59 +02:00
/** @var Node\AttributeGroup[] */
protected $attributeGroups = [];
2013-05-23 10:31:59 +02:00
/**
* Creates an interface builder.
*
* @param string $name Name of the interface
*/
2017-04-28 21:40:59 +02:00
public function __construct(string $name) {
2013-05-23 10:31:59 +02:00
$this->name = $name;
}
/**
* Extends one or more interfaces.
*
2016-02-20 17:06:09 +01:00
* @param Name|string ...$interfaces Names of interfaces to extend
2013-05-23 10:31:59 +02:00
*
* @return $this The builder instance (for fluid interface)
2013-05-23 10:31:59 +02:00
*/
public function extend(...$interfaces) {
foreach ($interfaces as $interface) {
$this->extends[] = BuilderHelpers::normalizeName($interface);
2013-05-23 10:31:59 +02:00
}
return $this;
}
/**
* Adds a statement.
*
* @param Stmt|PhpParser\Builder $stmt The statement to add
2013-05-23 10:31:59 +02:00
*
* @return $this The builder instance (for fluid interface)
2013-05-23 10:31:59 +02:00
*/
public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt);
2013-05-23 10:31:59 +02:00
if ($stmt instanceof Stmt\ClassConst) {
$this->constants[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassMethod) {
// we erase all statements in the body of an interface method
$stmt->stmts = null;
$this->methods[] = $stmt;
} else {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
2013-05-23 10:31:59 +02:00
}
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
2013-05-23 10:31:59 +02:00
/**
* Returns the built interface node.
2013-05-23 10:31:59 +02:00
*
* @return Stmt\Interface_ The built interface node
2013-05-23 10:31:59 +02:00
*/
2017-04-28 21:40:59 +02:00
public function getNode() : PhpParser\Node {
return new Stmt\Interface_($this->name, [
2013-05-23 10:31:59 +02:00
'extends' => $this->extends,
'stmts' => array_merge($this->constants, $this->methods),
'attrGroups' => $this->attributeGroups,
], $this->attributes);
2013-05-23 10:31:59 +02:00
}
2018-01-10 18:04:06 +01:00
}