1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-27 12:24:39 +01:00
PHP-Parser/lib/PhpParser/BuilderFactory.php

176 lines
4.5 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
2012-03-10 17:56:56 +01:00
namespace PhpParser;
use PhpParser\Node\Arg;
2017-04-24 21:42:59 +02:00
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
2015-03-10 16:05:55 +01:00
use PhpParser\Node\Stmt\Use_;
class BuilderFactory
2012-03-10 17:56:56 +01:00
{
2014-12-11 17:24:10 +01:00
/**
* Creates a namespace builder.
*
2015-03-10 16:05:55 +01:00
* @param null|string|Node\Name $name Name of the namespace
2014-12-11 17:24:10 +01:00
*
* @return Builder\Namespace_ The created namespace builder
*/
public function namespace($name) : Builder\Namespace_ {
2014-12-11 17:24:10 +01:00
return new Builder\Namespace_($name);
}
2012-03-11 09:02:52 +01:00
/**
* Creates a class builder.
*
* @param string $name Name of the class
*
* @return Builder\Class_ The created class builder
2012-03-10 17:56:56 +01:00
*/
public function class(string $name) : Builder\Class_ {
return new Builder\Class_($name);
2012-03-10 17:56:56 +01:00
}
2013-05-23 10:31:59 +02:00
/**
* Creates an interface builder.
2013-05-23 10:31:59 +02:00
*
* @param string $name Name of the interface
*
* @return Builder\Interface_ The created interface builder
2013-05-23 10:31:59 +02:00
*/
public function interface(string $name) : Builder\Interface_ {
return new Builder\Interface_($name);
2013-05-23 10:31:59 +02:00
}
/**
* Creates a trait builder.
*
* @param string $name Name of the trait
*
* @return Builder\Trait_ The created trait builder
*/
public function trait(string $name) : Builder\Trait_ {
return new Builder\Trait_($name);
}
2012-03-10 23:25:26 +01:00
/**
* Creates a method builder.
*
* @param string $name Name of the method
*
* @return Builder\Method The created method builder
2012-03-10 23:25:26 +01:00
*/
2017-04-28 21:40:59 +02:00
public function method(string $name) : Builder\Method {
return new Builder\Method($name);
2012-03-10 23:25:26 +01:00
}
2012-03-11 00:06:02 +01:00
/**
* Creates a parameter builder.
*
* @param string $name Name of the parameter
*
* @return Builder\Param The created parameter builder
2012-03-11 00:06:02 +01:00
*/
2017-04-28 21:40:59 +02:00
public function param(string $name) : Builder\Param {
return new Builder\Param($name);
2012-03-11 00:06:02 +01:00
}
2012-03-11 08:42:13 +01:00
/**
* Creates a property builder.
*
* @param string $name Name of the property
*
* @return Builder\Property The created property builder
2012-03-11 08:42:13 +01:00
*/
2017-04-28 21:40:59 +02:00
public function property(string $name) : Builder\Property {
return new Builder\Property($name);
2012-03-11 08:42:13 +01:00
}
2012-03-11 09:02:52 +01:00
/**
* Creates a function builder.
*
* @param string $name Name of the function
*
* @return Builder\Function_ The created function builder
2012-03-11 09:02:52 +01:00
*/
public function function(string $name) : Builder\Function_ {
return new Builder\Function_($name);
2012-03-11 09:02:52 +01:00
}
2015-03-10 16:05:55 +01:00
/**
* Creates a namespace/class use builder.
*
* @param string|Node\Name Name to alias
*
* @return Builder\Use_ The create use builder
*/
public function use($name) : Builder\Use_ {
2015-03-10 16:05:55 +01:00
return new Builder\Use_($name, Use_::TYPE_NORMAL);
}
2017-04-24 21:28:03 +02:00
/**
* Creates node a for a literal value.
*
2017-04-24 21:42:59 +02:00
* @param Expr|bool|null|int|float|string|array $value $value
2017-04-24 21:28:03 +02:00
*
2017-04-24 21:42:59 +02:00
* @return Expr
2017-04-24 21:28:03 +02:00
*/
2017-04-28 21:40:59 +02:00
public function val($value) : Expr {
2017-04-24 21:28:03 +02:00
return BuilderHelpers::normalizeValue($value);
}
/**
* Normalizes an argument list.
*
* Creates Arg nodes for all arguments and converts literal values to expressions.
*
* @param array $args List of arguments to normalize
*
* @return Arg[]
*/
2017-04-28 21:40:59 +02:00
public function args(array $args) : array {
$normalizedArgs = [];
foreach ($args as $arg) {
if ($arg instanceof Arg) {
$normalizedArgs[] = $arg;
} else {
$normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
}
}
return $normalizedArgs;
}
2017-04-24 21:42:59 +02:00
/**
* Creates nested Concat nodes from a list of expressions.
*
* @param Expr|string ...$exprs Expressions or literal strings
*
* @return Concat
*/
2017-04-28 21:40:59 +02:00
public function concat(...$exprs) : Concat {
2017-04-24 21:42:59 +02:00
$numExprs = count($exprs);
if ($numExprs < 2) {
throw new \LogicException('Expected at least two expressions');
}
$lastConcat = $this->normalizeStringExpr($exprs[0]);
for ($i = 1; $i < $numExprs; $i++) {
$lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
}
return $lastConcat;
}
private function normalizeStringExpr($expr) {
if ($expr instanceof Expr) {
return $expr;
}
if (is_string($expr)) {
return new String_($expr);
}
throw new \LogicException('Expected string or Expr');
}
}