| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace PhpParser\Builder; |
| 4 |
|
| 5 |
use PhpParser\Builder; |
| 6 |
use PhpParser\BuilderHelpers; |
| 7 |
use PhpParser\Node; |
| 8 |
use PhpParser\Node\Stmt; |
| 9 |
|
| 10 |
class TraitUse implements Builder |
| 11 |
{ |
| 12 |
protected $traits = []; |
| 13 |
protected $adaptations = []; |
| 14 |
|
| 15 |
/** |
| 16 |
* Creates a trait use builder. |
| 17 |
* |
| 18 |
* @param Node\Name|string ...$traits Names of used traits |
| 19 |
*/ |
| 20 |
public function __construct(...$traits) { |
| 21 |
foreach ($traits as $trait) { |
| 22 |
$this->and($trait); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Adds used trait. |
| 28 |
* |
| 29 |
* @param Node\Name|string $trait Trait name |
| 30 |
* |
| 31 |
* @return $this The builder instance (for fluid interface) |
| 32 |
*/ |
| 33 |
public function and($trait) { |
| 34 |
$this->traits[] = BuilderHelpers::normalizeName($trait); |
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Adds trait adaptation. |
| 40 |
* |
| 41 |
* @param Stmt\TraitUseAdaptation|Builder\TraitUseAdaptation $adaptation Trait adaptation |
| 42 |
* |
| 43 |
* @return $this The builder instance (for fluid interface) |
| 44 |
*/ |
| 45 |
public function with($adaptation) { |
| 46 |
$adaptation = BuilderHelpers::normalizeNode($adaptation); |
| 47 |
|
| 48 |
if (!$adaptation instanceof Stmt\TraitUseAdaptation) { |
| 49 |
throw new \LogicException('Adaptation must have type TraitUseAdaptation'); |
| 50 |
} |
| 51 |
|
| 52 |
$this->adaptations[] = $adaptation; |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the built node. |
| 58 |
* |
| 59 |
* @return Node The built node |
| 60 |
*/ |
| 61 |
public function getNode() : Node { |
| 62 |
return new Stmt\TraitUse($this->traits, $this->adaptations); |
| 63 |
} |
| 64 |
} |
| 65 |
|