BuilderAbstract.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. abstract class PHPParser_BuilderAbstract implements PHPParser_Builder {
  3. /**
  4. * Normalizes a node: Converts builder objects to nodes.
  5. *
  6. * @param PHPParser_Node|PHPParser_Builder $node The node to normalize
  7. *
  8. * @return PHPParser_Node The normalized node
  9. */
  10. protected function normalizeNode($node) {
  11. if ($node instanceof PHPParser_Builder) {
  12. return $node->getNode();
  13. } elseif ($node instanceof PHPParser_Node) {
  14. return $node;
  15. }
  16. throw new LogicException('Expected node or builder object');
  17. }
  18. /**
  19. * Normalizes a name: Converts plain string names to PHPParser_Node_Name.
  20. *
  21. * @param PHPParser_Node_Name|string $name The name to normalize
  22. *
  23. * @return PHPParser_Node_Name The normalized name
  24. */
  25. protected function normalizeName($name) {
  26. if ($name instanceof PHPParser_Node_Name) {
  27. return $name;
  28. } else {
  29. return new PHPParser_Node_Name($name);
  30. }
  31. }
  32. /**
  33. * Normalizes a value: Converts nulls, booleans, integers,
  34. * floats, strings and arrays into their respective nodes
  35. *
  36. * @param mixed $value The value to normalize
  37. *
  38. * @return PHPParser_Node_Expr The normalized value
  39. */
  40. protected function normalizeValue($value) {
  41. if ($value instanceof PHPParser_Node) {
  42. return $value;
  43. } elseif (is_null($value)) {
  44. return new PHPParser_Node_Expr_ConstFetch(
  45. new PHPParser_Node_Name('null')
  46. );
  47. } elseif (is_bool($value)) {
  48. return new PHPParser_Node_Expr_ConstFetch(
  49. new PHPParser_Node_Name($value ? 'true' : 'false')
  50. );
  51. } elseif (is_int($value)) {
  52. return new PHPParser_Node_Scalar_LNumber($value);
  53. } elseif (is_float($value)) {
  54. return new PHPParser_Node_Scalar_DNumber($value);
  55. } elseif (is_string($value)) {
  56. return new PHPParser_Node_Scalar_String($value);
  57. } elseif (is_array($value)) {
  58. $items = array();
  59. $lastKey = -1;
  60. foreach ($value as $itemKey => $itemValue) {
  61. // for consecutive, numeric keys don't generate keys
  62. if (null !== $lastKey && ++$lastKey === $itemKey) {
  63. $items[] = new PHPParser_Node_Expr_ArrayItem(
  64. $this->normalizeValue($itemValue)
  65. );
  66. } else {
  67. $lastKey = null;
  68. $items[] = new PHPParser_Node_Expr_ArrayItem(
  69. $this->normalizeValue($itemValue),
  70. $this->normalizeValue($itemKey)
  71. );
  72. }
  73. }
  74. return new PHPParser_Node_Expr_Array($items);
  75. } else {
  76. throw new LogicException('Invalid value');
  77. }
  78. }
  79. /**
  80. * Sets a modifier in the $this->type property.
  81. *
  82. * @param int $modifier Modifier to set
  83. */
  84. protected function setModifier($modifier) {
  85. PHPParser_Node_Stmt_Class::verifyModifier($this->type, $modifier);
  86. $this->type |= $modifier;
  87. }
  88. }