Param.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. class PHPParser_Builder_Param extends PHPParser_BuilderAbstract
  3. {
  4. protected $name;
  5. protected $default;
  6. protected $type;
  7. protected $byRef;
  8. /**
  9. * Creates a parameter builder.
  10. *
  11. * @param string $name Name of the parameter
  12. */
  13. public function __construct($name) {
  14. $this->name = $name;
  15. $this->default = null;
  16. $this->type = null;
  17. $this->byRef = false;
  18. }
  19. /**
  20. * Sets default value for the parameter.
  21. *
  22. * @param mixed $value Default value to use
  23. *
  24. * @return PHPParser_Builder_Param The builder instance (for fluid interface)
  25. */
  26. public function setDefault($value) {
  27. $this->default = $this->normalizeValue($value);
  28. return $this;
  29. }
  30. /**
  31. * Sets type hint for the parameter.
  32. *
  33. * @param string|PHPParser_Node_Name $type Type hint to use
  34. *
  35. * @return PHPParser_Builder_Param The builder instance (for fluid interface)
  36. */
  37. public function setTypeHint($type) {
  38. if ($type === 'array' || $type === 'callable') {
  39. $this->type = $type;
  40. } else {
  41. $this->type = $this->normalizeName($type);
  42. }
  43. return $this;
  44. }
  45. /**
  46. * Make the parameter accept the value by reference.
  47. *
  48. * @return PHPParser_Builder_Param The builder instance (for fluid interface)
  49. */
  50. public function makeByRef() {
  51. $this->byRef = true;
  52. return $this;
  53. }
  54. /**
  55. * Returns the built parameter node.
  56. *
  57. * @return PHPParser_Node_Param The built parameter node
  58. */
  59. public function getNode() {
  60. return new PHPParser_Node_Param(
  61. $this->name, $this->default, $this->type, $this->byRef
  62. );
  63. }
  64. }