mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-29 13:17:36 +01:00
Add ability to specify arrays as default values
This commit is contained in:
parent
3ce3542032
commit
9e5c95b6aa
@ -39,7 +39,7 @@ abstract class PHPParser_BuilderAbstract implements PHPParser_Builder {
|
||||
*
|
||||
* @param mixed $value The value to normalize
|
||||
*
|
||||
* @return PHPParser_Node The normalized value
|
||||
* @return PHPParser_Node_Expr The normalized value
|
||||
*/
|
||||
protected function normalizeValue($value) {
|
||||
if ($value instanceof PHPParser_Node) {
|
||||
@ -58,6 +58,25 @@ abstract class PHPParser_BuilderAbstract implements PHPParser_Builder {
|
||||
return new PHPParser_Node_Scalar_DNumber($value);
|
||||
} elseif (is_string($value)) {
|
||||
return new PHPParser_Node_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 PHPParser_Node_Expr_ArrayItem(
|
||||
$this->normalizeValue($itemValue)
|
||||
);
|
||||
} else {
|
||||
$lastKey = null;
|
||||
$items[] = new PHPParser_Node_Expr_ArrayItem(
|
||||
$this->normalizeValue($itemValue),
|
||||
$this->normalizeValue($itemKey)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new PHPParser_Node_Expr_Array($items);
|
||||
} else {
|
||||
throw new LogicException('Invalid value');
|
||||
}
|
||||
|
@ -44,6 +44,27 @@ class PHPParser_Tests_Builder_ParamTest extends PHPUnit_Framework_TestCase
|
||||
'Hallo World',
|
||||
new PHPParser_Node_Scalar_String('Hallo World')
|
||||
),
|
||||
array(
|
||||
array(1, 2, 3),
|
||||
new PHPParser_Node_Expr_Array(array(
|
||||
new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(1)),
|
||||
new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(2)),
|
||||
new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(3)),
|
||||
))
|
||||
),
|
||||
array(
|
||||
array('foo' => 'bar', 'bar' => 'foo'),
|
||||
new PHPParser_Node_Expr_Array(array(
|
||||
new PHPParser_Node_Expr_ArrayItem(
|
||||
new PHPParser_Node_Scalar_String('bar'),
|
||||
new PHPParser_Node_Scalar_String('foo')
|
||||
),
|
||||
new PHPParser_Node_Expr_ArrayItem(
|
||||
new PHPParser_Node_Scalar_String('foo'),
|
||||
new PHPParser_Node_Scalar_String('bar')
|
||||
),
|
||||
))
|
||||
),
|
||||
array(
|
||||
new PHPParser_Node_Scalar_DirConst,
|
||||
new PHPParser_Node_Scalar_DirConst
|
||||
|
@ -93,6 +93,27 @@ class PHPParser_Tests_Builder_PropertyTest extends PHPUnit_Framework_TestCase
|
||||
'Hallo World',
|
||||
new PHPParser_Node_Scalar_String('Hallo World')
|
||||
),
|
||||
array(
|
||||
array(1, 2, 3),
|
||||
new PHPParser_Node_Expr_Array(array(
|
||||
new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(1)),
|
||||
new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(2)),
|
||||
new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(3)),
|
||||
))
|
||||
),
|
||||
array(
|
||||
array('foo' => 'bar', 'bar' => 'foo'),
|
||||
new PHPParser_Node_Expr_Array(array(
|
||||
new PHPParser_Node_Expr_ArrayItem(
|
||||
new PHPParser_Node_Scalar_String('bar'),
|
||||
new PHPParser_Node_Scalar_String('foo')
|
||||
),
|
||||
new PHPParser_Node_Expr_ArrayItem(
|
||||
new PHPParser_Node_Scalar_String('foo'),
|
||||
new PHPParser_Node_Scalar_String('bar')
|
||||
),
|
||||
))
|
||||
),
|
||||
array(
|
||||
new PHPParser_Node_Scalar_DirConst,
|
||||
new PHPParser_Node_Scalar_DirConst
|
||||
|
Loading…
x
Reference in New Issue
Block a user