2012-03-10 23:25:26 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
|
|
|
|
abstract class BuilderAbstract implements Builder {
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
|
|
|
* Normalizes a node: Converts builder objects to nodes.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node|Builder $node The node to normalize
|
2012-03-10 23:25:26 +01:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Node The normalized node
|
2012-03-10 23:25:26 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeNode($node) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node instanceof Builder) {
|
2012-03-10 23:25:26 +01:00
|
|
|
return $node->getNode();
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Node) {
|
2012-03-10 23:25:26 +01:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \LogicException('Expected node or builder object');
|
2012-03-10 23:25:26 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 00:06:02 +01:00
|
|
|
/**
|
|
|
|
* Normalizes a name: Converts plain string names to PHPParser_Node_Name.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Name|string $name The name to normalize
|
2012-03-11 00:06:02 +01:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Name The normalized name
|
2012-03-11 00:06:02 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeName($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($name instanceof Name) {
|
2012-03-11 00:06:02 +01:00
|
|
|
return $name;
|
|
|
|
} else {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Name($name);
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes a value: Converts nulls, booleans, integers,
|
2012-11-06 18:28:15 +01:00
|
|
|
* floats, strings and arrays into their respective nodes
|
2012-03-11 00:06:02 +01:00
|
|
|
*
|
|
|
|
* @param mixed $value The value to normalize
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Expr The normalized value
|
2012-03-11 00:06:02 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeValue($value) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($value instanceof Node) {
|
2012-03-11 00:06:02 +01:00
|
|
|
return $value;
|
|
|
|
} elseif (is_null($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Expr\ConstFetch(
|
|
|
|
new Name('null')
|
2012-03-11 00:06:02 +01:00
|
|
|
);
|
|
|
|
} elseif (is_bool($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Expr\ConstFetch(
|
|
|
|
new Name($value ? 'true' : 'false')
|
2012-03-11 00:06:02 +01:00
|
|
|
);
|
|
|
|
} elseif (is_int($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Scalar\LNumber($value);
|
2012-03-11 00:06:02 +01:00
|
|
|
} elseif (is_float($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Scalar\DNumber($value);
|
2012-03-11 00:06:02 +01:00
|
|
|
} elseif (is_string($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Scalar\String($value);
|
2012-03-11 08:53:04 +01:00
|
|
|
} 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) {
|
2014-02-06 14:44:16 +01:00
|
|
|
$items[] = new Expr\ArrayItem(
|
2012-03-11 08:53:04 +01:00
|
|
|
$this->normalizeValue($itemValue)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$lastKey = null;
|
2014-02-06 14:44:16 +01:00
|
|
|
$items[] = new Expr\ArrayItem(
|
2012-03-11 08:53:04 +01:00
|
|
|
$this->normalizeValue($itemValue),
|
|
|
|
$this->normalizeValue($itemKey)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Expr\Array_($items);
|
2012-03-11 00:06:02 +01:00
|
|
|
} else {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \LogicException('Invalid value');
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
|
|
|
* Sets a modifier in the $this->type property.
|
|
|
|
*
|
|
|
|
* @param int $modifier Modifier to set
|
|
|
|
*/
|
|
|
|
protected function setModifier($modifier) {
|
2014-02-06 14:44:16 +01:00
|
|
|
Stmt\Class_::verifyModifier($this->type, $modifier);
|
2012-03-10 23:25:26 +01:00
|
|
|
$this->type |= $modifier;
|
|
|
|
}
|
|
|
|
}
|