mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-30 04:19:30 +01:00
101 lines
2.9 KiB
PHP
101 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace PhpParser;
|
|
|
|
use PhpParser\Node\Name;
|
|
use PhpParser\Node\Expr;
|
|
use PhpParser\Node\Stmt;
|
|
use PhpParser\Node\Scalar;
|
|
|
|
abstract class BuilderAbstract implements Builder {
|
|
/**
|
|
* Normalizes a node: Converts builder objects to nodes.
|
|
*
|
|
* @param Node|Builder $node The node to normalize
|
|
*
|
|
* @return Node The normalized node
|
|
*/
|
|
protected function normalizeNode($node) {
|
|
if ($node instanceof Builder) {
|
|
return $node->getNode();
|
|
} elseif ($node instanceof Node) {
|
|
return $node;
|
|
}
|
|
|
|
throw new \LogicException('Expected node or builder object');
|
|
}
|
|
|
|
/**
|
|
* Normalizes a name: Converts plain string names to PhpParser\Node\Name.
|
|
*
|
|
* @param Name|string $name The name to normalize
|
|
*
|
|
* @return Name The normalized name
|
|
*/
|
|
protected function normalizeName($name) {
|
|
if ($name instanceof Name) {
|
|
return $name;
|
|
} else {
|
|
return new Name($name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Normalizes a value: Converts nulls, booleans, integers,
|
|
* floats, strings and arrays into their respective nodes
|
|
*
|
|
* @param mixed $value The value to normalize
|
|
*
|
|
* @return Expr The normalized value
|
|
*/
|
|
protected function normalizeValue($value) {
|
|
if ($value instanceof Node) {
|
|
return $value;
|
|
} elseif (is_null($value)) {
|
|
return new Expr\ConstFetch(
|
|
new Name('null')
|
|
);
|
|
} elseif (is_bool($value)) {
|
|
return new Expr\ConstFetch(
|
|
new Name($value ? 'true' : 'false')
|
|
);
|
|
} elseif (is_int($value)) {
|
|
return new Scalar\LNumber($value);
|
|
} elseif (is_float($value)) {
|
|
return new Scalar\DNumber($value);
|
|
} elseif (is_string($value)) {
|
|
return new Scalar\String($value);
|
|
} elseif (is_array($value)) {
|
|
$items = array();
|
|
$lastKey = -1;
|
|
foreach ($value as $itemKey => $itemValue) {
|
|
// for consecutive, numeric keys don't generate keys
|
|
if (null !== $lastKey && ++$lastKey === $itemKey) {
|
|
$items[] = new Expr\ArrayItem(
|
|
$this->normalizeValue($itemValue)
|
|
);
|
|
} else {
|
|
$lastKey = null;
|
|
$items[] = new Expr\ArrayItem(
|
|
$this->normalizeValue($itemValue),
|
|
$this->normalizeValue($itemKey)
|
|
);
|
|
}
|
|
}
|
|
|
|
return new Expr\Array_($items);
|
|
} else {
|
|
throw new \LogicException('Invalid value');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sets a modifier in the $this->type property.
|
|
*
|
|
* @param int $modifier Modifier to set
|
|
*/
|
|
protected function setModifier($modifier) {
|
|
Stmt\Class_::verifyModifier($this->type, $modifier);
|
|
$this->type |= $modifier;
|
|
}
|
|
} |